基於選單的Java程式,用於執行基本的字串操作


字串是指一系列字元。在Java中,字串是物件。為了建立和操作字串,Java提供了String類。String類有很多內建方法,用於不同的目的。

我們將使用內建的String方法執行一些基本的字串操作。

replace() Method: It replaces a specified character in the given string.
concat() Method: It appends another string to the end of one string.
length() Method: It returns the length of the given String.
Equals() Method: It checks whether two strings are equal or not.

在本文中,我們將學習一些基本的字串操作,例如使用Java程式語言連線兩個字串、計算字串長度、比較兩個字串。我們將使用switch case實現應用程式。

為了向您展示一些示例:

示例1

假設第一個字串是“Java”,第二個字串是“Python”,那麼連線兩個字串將得到“JavaPython”。這裡將使用concat()方法。

示例2

假設第一個字串是“Java”,第二個字串是“Python”,那麼計算兩個字串的長度將分別得到4和6。這裡將使用length()方法。

示例3

假設第一個字串是“Java”,第二個字串是“Python”,那麼比較兩個字串將得到“這兩個字串不相等”。這裡將使用equals()方法。

示例4

假設字串是“Java”,並將字母“J”替換為“R”,則新字串將為“Rava”。這裡將使用replace()方法。

語法

要執行基本的字串操作,例如連線字串、獲取字串長度、比較字串和替換字串中的特定值,我們分別使用concat()、length()、equals()和replace()方法。concat()方法**將一個字串附加(連線)到另一個字串的末尾**。length()方法**返回指定字串的長度**。空字串的長度為0。equals()方法**比較兩個字串,如果字串相等則返回true,否則返回false**。replace()方法**用另一個新值替換字串中的指定值**。

以下是“for迴圈”的語法:

for (statement 1; statement 2; statement 3) {
   // code block to be executed
}

以下是concat函式的語法

string1.concat(string2)

以下是length函式的語法

string1.length() 

以下是equals函式的語法

string1.equals(string2)

以下是replace函式的語法

string1.replace(‘OldValue’, ‘NewValue’)

演算法

**步驟1** - 宣告一個字串變數並初始化值。

**步驟2** - 顯示選單。

**步驟3** - 提示使用者輸入他們的選擇。

**步驟4** - 使用switch case跳轉到選擇並執行操作。

**步驟5** - 列印結果。

讓我們來看一下程式以更清晰地理解它。

示例

import java.util.*; public class Main{ public static void main(String args[]){ System.out.println("First String"); String s1 = "Hello"; System.out.println("Second String"); String s2 = "World"; mainLoop: while (true) { Scanner inn = new Scanner( System.in ); System.out.println("\n***Menu***"); System.out.println("1. Join Two Strings"); System.out.println("2. Get length of a String"); System.out.println("3. Compare two Strings"); System.out.println("4. Replace a value in String"); System.out.println("5. Terminate the program"); System.out.println("Enter action number (1-5): "); int command; if (inn.hasNextInt()){ command = inn.nextInt(); inn.nextLine(); } else{ System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER."); inn.nextLine(); continue; } switch(command) { case 1: String joinedString = s1.concat(s2); System.out.println("Joined String: " + joinedString); break; case 2: int length1 = s1.length(); System.out.println("Length of first String: " + length1); int length2 = s2.length(); System.out.println("Length of second String: " + length2); break; case 3: boolean result = s1.equals(s2); if(result == true) { System.out.println("Strings first and second are equal"); } else{ System.out.println("Strings first and second are not equal"); } break; case 4: String newString = s2.replace('W', 'Z'); System.out.println("After replacing the new string is: "+newString); break; case 5: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } } }

輸出

First String
Second String

***Menu***
1. Join Two Strings
2. Get length of a String
3. Compare two Strings
4. Replace a value in String
5. Terminate the program
Enter action number (1-5):
2
Length of first String: 5
Length of second String: 5

***Menu***
1. Join Two Strings
2. Get length of a String
3. Compare two Strings
4. Replace a value in String
5. Terminate the program
Enter action number (1-5):
1
Joined String: HelloWorld

***Menu***
1. Join Two Strings
2. Get length of a String
3. Compare two Strings
4. Replace a value in String
5. Terminate the program
Enter action number (1-5):
4
After replacing the new string is: Zorld

***Menu***
1. Join Two Strings
2. Get length of a String
3. Compare two Strings
4. Replace a value in String
5. Terminate the program
Enter action number (1-5):
3
Strings first and second are not equal

***Menu***
1. Join Two Strings
2. Get length of a String
3. Compare two Strings
4. Replace a value in String
5. Terminate the program
Enter action number (1-5):
5
Program terminated

在本文中,我們探討了如何使用選單驅動的方法在Java中執行簡單的字串操作。

更新於:2022年12月27日

4K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告