你好,我需要计算一个给定的字符串是否是另一个更大的字符串的子串。 例如:
String str = "Hallo my world";
String substr = "my"
方法“contains”应该返回true,因为str包含substr(否则返回false)。
我在String类中寻找类似“contains”的方法,但没有找到。我想唯一的解决方案是使用模式匹配。如果是这种情况,哪种方法更好(更便宜)呢?
谢谢!
你好,我需要计算一个给定的字符串是否是另一个更大的字符串的子串。 例如:
String str = "Hallo my world";
String substr = "my"
方法“contains”应该返回true,因为str包含substr(否则返回false)。
我在String类中寻找类似“contains”的方法,但没有找到。我想唯一的解决方案是使用模式匹配。如果是这种情况,哪种方法更好(更便宜)呢?
谢谢!
String str="hello world";
System.out.println(str.contains("world"));//true
System.out.println(str.contains("world1"));//false
String s = "AJAYkumarReddy";
String sub = "kumar";
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == sub.charAt(count)) {
count++;
} else {
count = 0;
}
if (count == sub.length()) {
System.out.println("Sub String");
return;
}
}
使用indexOf方法,如果没有匹配的内容,它将返回-1(contains方法是在1.5版本中添加的,也许您正在使用较旧的jdk?)请参见JDK 1.4.2中String类的contains(CharSequence s)方法获取详细信息。
public static boolean isSubstring(String s1, String s2) {
if(s1.length() == s2.length())
return s1.equals(s2);
else if(s1.length() > s2.length())
return s1.contains(s2);
else
return s2.contains(s1);
}
我认为有一个字符串函数可以做到你所要求的:String.indexOf(String)。
请参考此链接:http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#indexOf(java.lang.String)
那么,你可以编写以下函数:
public boolean isSubstring(String super, String sub) {
return super.indexOf(sub) >= 0;
}
public boolean isSubString(String smallStr, String largerStr) {
char[] larger = largerStr.toCharArray();
char[] smaller = smallStr.toCharArray();
int i = 0;
for (int j = 0; j < larger.length; j++) {
if(larger[j] == smaller[i]){
if(i == smaller.length -1){
//done we found that this string is substring
return true;
}
i++;
continue;
}else{
if(i > 0){
//that means we encountered a duplicate character before and if string was substring
// it shouldn't have hit this condition..
if(larger.length - j >= smaller.length){
i = 0;
//reset i here because there are still more characters to check for substring..
}else{
//we don't have enough characters to check for substring.. so done..
return false;
}
}
}
}
return false;
}
if (str.indexOf(substr) >= 0) {
// do something
}
str="foobar"
和 substr="foo"
上失败,因为 indexOf()
将返回 0
。 - Joachim SauerString.indexOf(substr) 的时间复杂度为 O(n2)。Luixv 寻求一种更便宜的解决方案。但据我所知,目前没有比现有算法更好的算法。
public class Test {
public static void main(final String[] args) {
System.out.println("Enter the first String");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String s1 = br.readLine();
System.out.println("Enter the second String");
String s2 = br.readLine();
boolean result = isSubStr(s1, s2);
if (result == true)
System.out.println("The second String is substring of the first String");
else
System.out.println("The second String is not a substring of the first String");
} catch (IOException e) {
System.out.println("Exception Caught: " + e);
}
}
public static boolean isSubStr(String st1, String s2) {
boolean result = false;
String tem_str = "";
int len1 = st1.length();
int i = 0;
int j;
while (i < len1) {
j = i+1;
while (j <=len1) {
tem_str = st1.substring(i, j);
if (tem_str.equalsIgnoreCase(s2)) {
result = true;
break;
}
j++;
}
i++;
}
return result;
}
}
str.indexOf(substr) != -1
。 - biziclop