如何知道给定字符串是否是Java中另一个字符串的子字符串

嗨,我必须计算一个给定的字符串是否是一个更大的字符串的子字符串。 例如

String str = "Hallo my world"; String substr = "my" 

方法“contains”应返回true,因为str包含substr(否则为false)。

我在String类中寻找类似“contains”的东西,但我找不到它。 我想唯一的解决方案是使用模式匹配。 如果是这种情况会更好(最便宜)的方式吗?

谢谢!

有一个contains()方法! 它是在Java 1.5中引入的。 如果您使用的是早期版本,则可以轻松地将其替换为:

 str.indexOf(substr) != -1 
  String str="hello world"; System.out.println(str.contains("world"));//true System.out.println(str.contains("world1"));//false 
  • 的Javadoc

使用indexOf它会返回-1如果没有匹配(包含在1.5中添加,也许你使用的是较旧的jdk?)请参阅JDK 1.4.2中String类中的“contains(CharSequence s)”方法了解详细信息

  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; } } 
 if (str.indexOf(substr) >= 0) { // do something } 

我认为有一个String函数可以满足您的要求: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; } 

String.indexOf(substr)复杂度是O(n2).. Luixv问了一个更便宜的解决方案..但据我所知,没有比现有算法更好的算法。

  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; } 

这是您可以使用的一般方法

 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); } 
 public static boolean isSubstring(String s1, String s2){ if(s1.length() 

这将检查s2是否为s1的子字符串。

您可以使用.substring(int beginIndex,int lastIndex)来检查此程序。 示例代码如下: –

 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; } }