Java 是否可以修改startsWith(字符串前缀)以搜索一个字符串中的重复出现字符?
startsWith(字符串前缀)需要两个字符串并进行搜索如果两个字符串都以相同的值开头,是否有方法修改startsWith(字符串前缀)来搜索一个字符串以查找重复出现的字符 比方说Java 是否可以修改startsWith(字符串前缀)以搜索一个字符串中的重复出现字符?,java,string,Java,String,startsWith(字符串前缀)需要两个字符串并进行搜索如果两个字符串都以相同的值开头,是否有方法修改startsWith(字符串前缀)来搜索一个字符串以查找重复出现的字符 比方说 String str = "MANAMA"; 我希望能够使用startsWith()和endsWith()或一些变体来搜索该字符串,以确定字符串的前缀或后缀是否相同。唯一的问题是这些方法搜索两个字符串,看看它们是否有匹配的后缀或前缀 startsWith(字符串前缀)执行以下操作: String str1
String str = "MANAMA";
我希望能够使用startsWith()和endsWith()或一些变体来搜索该字符串,以确定字符串的前缀或后缀是否相同。唯一的问题是这些方法搜索两个字符串,看看它们是否有匹配的后缀或前缀
startsWith(字符串前缀)执行以下操作:
String str1 = "MANAMA";
String str2 = "PANAMA.";
// Sets what characters to search for from above strings
String startStr = "MA";
// Searchs for startStr in strings
boolean starts1 = str1.startsWith(startStr);
boolean starts2 = str2.startsWith(startStr);
// Display the results of the startsWith calls.
System.out.println(starts1);
System.out.println(starts2);
endsWith(字符串后缀)执行以下操作:
是否有一种方法可以修改或使用另一种方法在一个字符串中搜索,而不是在两个字符串中搜索endStr和startStr?不确定我是否正确理解您的问题,但您可以在同一字符串上使用
endsWith()
和startsWith()
。例如:
String line = "MANAMA";
String edge = "MA";
boolean check = line.startsWith(edge) && line.endsWith(edge);
// returns true if the string starts and ends with "MA"
请举一个你想要达到的目标的例子。问题不清楚。你能详细说明一下吗?举个例子?是的,您可以同时调用
endsWith()
和startsWith()
相同字符串上的方法。因此,如果在用户键入字符串之前我不知道字符串是什么,我如何设置方法应搜索的字符?我想我不明白您的问题-您是否指定了要检查的前缀和后缀?
String line = "MANAMA";
String edge = "MA";
boolean check = line.startsWith(edge) && line.endsWith(edge);
// returns true if the string starts and ends with "MA"