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中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。