How to find out if a given string is a substring from another string in Java

Hi I have to calculate if a given string is a substring of a larger string. for instance

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

The "contains" method should return true because str contains substr (false otherwise).

I was looking for something like "contains" in the String class but I did not find this. I believe that the only solution is to use pattern matching. If so, would that be the best (cheapest) way to do this?

Thanks!

+6
source share
14 answers

There is a contains() method! It was introduced in Java 1.5. If you are using an earlier version, then it is easy to replace it with the following:

 str.indexOf(substr) != -1 
+22
source
  String str="hello world"; System.out.println(str.contains("world"));//true System.out.println(str.contains("world1"));//false 
+5
source

use indexOf, it will return -1 if there is no match (contains added in 1.5, maybe you use the old jdk?) see the "contains (CharSequence s)" method in the String class in JDK 1.4.2 for details

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

I think there is a String function that only does what you ask: String.indexOf (String).

See this link: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#indexOf(java.lang.String )

So then you can write this function:

 public boolean isSubstring(String super, String sub) { return super.indexOf(sub) >= 0; } 
+1
source

complexity String.indexOf (substr) is O (n2) .. Luixv asked for a cheaper solution. But as far as I know, there is no better algorithm than the current one.

+1
source
  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; } 
+1
source

here is a general method you can use

 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); } 
+1
source
 public static boolean isSubstring(String s1, String s2){ if(s1.length()<s2.length()) return false; if(s1.length()==s2.length()) return s1.equals(s2); for(int i=0;i<=s1.length()-s2.length();i++){ if(s1.charAt(i)==s2.charAt(0)){ int matchLength=1; for(int j=1;j<s2.length();j++){ if(s1.charAt(i+j)!=s2.charAt(j)){ break; } matchLength++; } if(matchLength==s2.length()) return true; } } return false; } 

This checks if s2 is a substring of s1.

0
source

You can use .substring (int beginIndex, int lastIndex) to test this program. An example code looks like this:

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

Go through this method. visit for tricky code

 public static boolean isSubString(String s, String sub) { int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == sub.charAt(count)) { count++; } else { i-=count; count = 0; } if (count == sub.length()) { return true; } } return false; } 
0
source

Consider the following code:

If a substring is present, then it returns the starting index of the substring in the given string

The rest is returned -1

 public static int isSubstring(String str, String pattern) { int str_length = str.length(); int pattern_length = pattern.length(); for (int i = 0; i <= str_length - pattern_length; i++) { int j; for (j = 0; j < pattern_length; j++) if (str.charAt(i + j) != pattern.charAt(j)) break; if (j == pattern_length) return i; } return -1; } 
0
source
  String str1 = "Java8 makes Java more powerful"; String str2 = "Java"; char c; char d; int count=0; boolean match = true; for (int i = 0; i < str1.length(); i++) { c = str1.charAt(i); for (int j = 0; j < str2.length(); j++) { d = str2.charAt(j); if (c == d) { match = true; count++; if(count== str2.length()){ i = str1.length(); break; } i++; c = str1.charAt(i); } else { match = false; } } } if(match == true){ System.out.println("SubString "); } 
0
source

Source: https://habr.com/ru/post/1337137/


All Articles