we will perform a few basic string operations by using inbuilt string methods.
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来实现应用程序。
展示一些实例给你看−instance-1 的中文翻译为:实例-1suppose the first string is ‘java’ and the second string is ‘python’ then by joining two string will give ‘javapython’. here the concat() method will be used.
instance-2suppose the first string is ‘java’ and the second string is ‘python’ then by counting two string will give its respective length as 4 and 6. here length() method will be used.
instance-3suppose the first string is ‘java’ and the second string is ‘python’ then by comparing two string will give “both strings are not equal”. here equals() method will be used.
instance-4翻译成中文为:实例-4假设字符串为‘java’,将字母‘j’替换为‘r’,那么新的字符串将是rava。在这里将使用replace()方法。
语法to perform basic string operations like joining the string, getting length of the string, comparing the string and replacing a specific value in a string we use concat(), length(), equals() and replace() methods respectively. the concat() method appends (concatenate) a string to the end of another string. the length() method returns the length of a specified string. the length of an empty string is 0. the equals() method compares two strings, and returns true if the strings are equal, and false if not. the replace() method replaces a specified value in a string with another new value.
以下是“for循环”的语法 -
for (statement 1; statement 2; statement 3) { // code block to be executed}
following is the syntax for concat function
string1.concat(string2)
以下是length函数的语法
string1.length()
following is the syntax for equals function
string1.equals(string2)
以下是replace函数的语法
string1.replace(‘oldvalue’, ‘newvalue’)
算法step-1 − declare a string variable and initialize the value.
第二步 - 显示菜单。
step-3 − ask the user to enter their choice.
step-4 − use a switch case to go to the choice and perform the operation.
步骤-5 − 打印结果。
让我们看一下程序,以便更清楚地理解它。
example的中文翻译为:示例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 stringsecond string***menu***1. join two strings2. get length of a string3. compare two strings4. replace a value in string5. terminate the programenter action number (1-5):2length of first string: 5length of second string: 5***menu***1. join two strings2. get length of a string3. compare two strings4. replace a value in string5. terminate the programenter action number (1-5):1joined string: helloworld***menu***1. join two strings2. get length of a string3. compare two strings4. replace a value in string5. terminate the programenter action number (1-5):4after replacing the new string is: zorld***menu***1. join two strings2. get length of a string3. compare two strings4. replace a value in string5. terminate the programenter action number (1-5):3strings first and second are not equal***menu***1. join two strings2. get length of a string3. compare two strings4. replace a value in string5. terminate the programenter action number (1-5):5program terminated
在本文中,我们通过使用菜单驱动的方法,探讨了如何在java中执行简单的字符串操作。
以上就是使用java编写的菜单驱动程序,用于执行基本的字符串操作的详细内容。