目錄
展示一些實例給你看
Instance-3
實例-1
Syntax
Algorithm-2
Multiple Approaches
Approach-1: By Using Inbuilt substring Method
Example
Output
Approach-2: By Using Inbuilt charAt() Method
首頁 Java java教程 在Java中,將一個單字的最後一個字母大寫,並將第一個字母小寫

在Java中,將一個單字的最後一個字母大寫,並將第一個字母小寫

Aug 20, 2023 am 10:05 AM
java 單字 大寫

在Java中,將一個單字的最後一個字母大寫,並將第一個字母小寫

String是一系列字元值的序列。在Java中,String被視為物件。我們有一個由Java提供的String類,用於建立和操作字串。

We have to convert the first letter of the word to lowercase and last letter of the word to uppercase.

In this article we will see how the first and last letter can be converted into lower and upper case respectively. Let’s explore.

展示一些實例給你看

Instance-1

的中文翻譯為:

實例-1

假設輸入字串是「Hello」

After converting the first letter to lower and last letter to capital, the new string will be “hellO”

Instance-2

的翻譯為:

Instance-2

Suppose the input string is “Java”

After converting the first letter to lower and last letter to capital, the new string will be “javA”

Instance-3

Suppose the input string is “Programming”

將第一個字母轉為小寫,最後一個字母轉為大寫後,新字串將為「programminG」

Syntax

要取得字串的長度,Java的String類別提供了一個length()方法。

Below is the syntax for that −

str.length();
登入後複製

其中,str是字串變數。

要從原始字串中取得子字串,Java String類別提供了substring()方法。

Syntax

Below is the syntax for that −

str.substring(int startIndex);
str.substring(int startIndex, int endIndex);
登入後複製

其中,startIndex是包含的,endIndex是不包含的。

To get a character at a specified index Java String class provides the charAt() method.

Syntax

Below is the syntax for that −

str.charAt(int index)
登入後複製

Where, index refers to the index of the character that you need.

要將不同類型的值轉換為字串值,Java的String類別提供了valueOf()方法。

Syntax

Below is the syntax for that −

String.valueOf(Object obj)
登入後複製

Algorithm

注意 - 這個問題也可以用陣列的概念來解決。但是在這裡,我們嘗試了不使用數組概念來解決問題。同時,我們只使用了String類別的內建方法。

Algorithm-1

  • 步驟 1 - 透過初始化或使用者輸入來取得字串/單字。

  • Step 2 − Get the first and last letter by using the substring() method. Then convert it to lower and upper case by using toLowerCase() method and toUpperCase() method respectively.

  • 第三步 - 使用substring()方法取得中間字元(除第一個和最後一個字元)。

  • 第四步 - 將第一個、中間和最後一個值組合起來,得到最終的字串/單字。

Algorithm-2

  • 步驟 1 - 透過初始化或使用者輸入來取得字串/單字。

  • 第二步 - 透過使用charAt()方法取得第一個和最後一個字母。然後分別使用toLowerCase()方法和toUpperCase()方法轉換為小寫和大寫。

  • 步驟 3 - 使用charAt()方法和for迴圈取得中間字元(除了第一個和最後一個字元)。

  • 第四步 - 然後將第一個、中間和最後一個值組合在一起,得到最終的字串/單字。

Multiple Approaches

We have provided the solution in different approaches.

  • 透過使用內建的substring()方法

  • By Using inbuilt charAt() Method

Let’s see the program along with its output one by one.

Approach-1: By Using Inbuilt substring Method

#Example

In this approach, we will make use of Algorithm-1

import java.util.Scanner;
public class Main{
   public static void main(String[] args) {
   
      //input string
      String str = "Java";
      System.out.println("Original string is: "+str);
      
      //get size of the string 
      int size = str.length();
      
      //get last character and convert it to upper case
      String last = str.substring(size-1,size);
      String lastinUpper = last.toUpperCase();
      
      //get first character and convert it to lower case
      String first = str.substring(0,1);
      String firstinLower = first.toLowerCase();
      
      //get middle parts of the word, except first and last character
      String middle = str.substring(1,size-1);
      
      //combine everything and get the final string
      String result = firstinLower+middle+lastinUpper;
      
      //print result
      System.out.println("Updated string is: "+result);
   }
}
登入後複製

Output

#
Original string is: Java
Updated string is: javA
登入後複製

Approach-2: By Using Inbuilt charAt() Method

Example

在這種方法中,我們將利用Algorithm-2

public class Main{
   public static void main(String[] args) { 
   
      //input String
      String str = "Python";
      System.out.println("Original string: "+str);
      
      //get length of string
      int size = str.length();
      
      //find last character and convert it to upper case
      char last = str.charAt(size-1);
      String finalLast = (String.valueOf(last)).toUpperCase();
      
      //find first character and convert it to lowercase
      char first = str.charAt(0);
      String finalFirst = (String.valueOf(first)).toLowerCase();
      
      //find middle characters
      String middle="";
      for(int i=1; i<size-1; i++){
         String s = String.valueOf(str.charAt(i));
         middle=middle+s;
      }
      
      //find the updated string 
      String result = finalFirst+middle+finalLast;
      System.out.println("Updated string: "+result);
   }
}
登入後複製

Output

#
Original string: Python
Updated string: pythoN
登入後複製

在本文中,我們探討如何使用不同的方法將單字的首字母轉換為小寫,並將單字的最後一個字母轉換為大寫字母。

以上是在Java中,將一個單字的最後一個字母大寫,並將第一個字母小寫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Java 中的完美數 Java 中的完美數 Aug 30, 2024 pm 04:28 PM

Java 完美數指南。這裡我們討論定義,如何在 Java 中檢查完美數?

Java 中的隨機數產生器 Java 中的隨機數產生器 Aug 30, 2024 pm 04:27 PM

Java 隨機數產生器指南。在這裡,我們透過範例討論 Java 中的函數,並透過範例討論兩個不同的生成器。

Java中的Weka Java中的Weka Aug 30, 2024 pm 04:28 PM

Java 版 Weka 指南。這裡我們透過範例討論簡介、如何使用 weka java、平台類型和優點。

Java 中的史密斯數 Java 中的史密斯數 Aug 30, 2024 pm 04:28 PM

Java 史密斯數指南。這裡我們討論定義,如何在Java中檢查史密斯號?帶有程式碼實現的範例。

Java Spring 面試題 Java Spring 面試題 Aug 30, 2024 pm 04:29 PM

在本文中,我們保留了最常被問到的 Java Spring 面試問題及其詳細答案。這樣你就可以順利通過面試。

突破或從Java 8流返回? 突破或從Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

Java 中的時間戳至今 Java 中的時間戳至今 Aug 30, 2024 pm 04:28 PM

Java 中的時間戳記到日期指南。這裡我們也結合範例討論了介紹以及如何在java中將時間戳記轉換為日期。

創造未來:零基礎的 Java 編程 創造未來:零基礎的 Java 編程 Oct 13, 2024 pm 01:32 PM

Java是熱門程式語言,適合初學者和經驗豐富的開發者學習。本教學從基礎概念出發,逐步深入解說進階主題。安裝Java開發工具包後,可透過建立簡單的「Hello,World!」程式來實踐程式設計。理解程式碼後,使用命令提示字元編譯並執行程序,控制台上將輸出「Hello,World!」。學習Java開啟了程式設計之旅,隨著掌握程度加深,可創建更複雜的應用程式。

See all articles