Java 菜单驱动程序执行基本字符串操作
字符串是指字符序列。在 Java 中,字符串是对象。为了创建和操作字符串,Java 提供了 String 类。String 类有许多内置方法,可用于不同目的。
我们将使用内置的 String 方法执行一些基本的字符串操作。
replace() 方法:它替换给定字符串中的指定字符。 concat() 方法:它将另一个字符串附加到一个字符串的末尾。 length() 方法:它返回给定字符串的长度。 Equals() 方法:它检查两个字符串是否相等。
在本文中,我们将看到一些基本的字符串操作,例如使用 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) { // 要执行的代码块 }
以下是 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 中执行简单的字符串操作。