Key words
  • substring
  • regionmatch
  • हिन्दी

import java.io.*;
import java.lang.String.*;
//javac search_substring.java
//i like java and java is great
class search_substring{
public static void main(String arguments[])throws IOException

{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
//needs throws IOException
System.out.print("Type some thing : ");
String str1 =br.readLine();
System.out.println("call server");
search_substring_server.serach_string(str1);
System.out.println("Back to Client");
}
}
class search_substring_server {
public static void serach_string(String str1) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("type word to search-> ");
String str_search = br.readLine();
int n1 = str1.length();
int n2 = str_search.length();
boolean foundIt = false;
System.out.print(" search cyccle no :");
for (int i = 0; i <= (n1 - n2); i++) {
System.out.print( i + ", ");
if (str1.regionMatches(i, str_search, 0, n2)) {
foundIt = true;
System.out.println();
System.out.println(" found : " + str1.substring(i, i + n2) + " at " + i);
break;
}
}
if (!foundIt) System.out.println("oops !.");
}
}

 
boolean regionMatches(int toffset, String other, int ooffset, int len) Tests whether the specified region of this string matches the specified region of the String argument.

Region is of length len and begins at the index toffset for this string and ooffset for the other string.

boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) Tests whether the specified region of this string matches the specified region of the String argument.

Region is of length len and begins at the index toffset for this string and ooffset for the other string.

The boolean argument indicates whether case should be ignored; if true, case is ignored when comparing characters.

The example shown below is similar to the previous one, except "break" is remarked so that routine will continue to the end of the string, and report any match while iterating.

import java.io.*;
import java.lang.String.*;
//javac search_substring_b.java
//i like java and java is great
class search_substring_b{
public static void main(String arguments[])throws IOException

{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
//needs throws IOException
System.out.print("Type some thing : ");
String str1 =br.readLine();
System.out.println("call server");
search_substring_server_b.serach_string(str1);
System.out.println("Back to Client");
}
}
class search_substring_server_b {
public static void serach_string(String str1) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("type word to search-> ");
String str_search = br.readLine();
int n1 = str1.length();
int n2 = str_search.length();
boolean foundIt = false;
System.out.print(" search cyccle no :");
for (int i = 0; i <= (n1 - n2); i++) {
System.out.print( i + ", ");
if (str1.regionMatches(i, str_search, 0, n2)) {
foundIt = true;
System.out.println();
System.out.println(" found : " + str1.substring(i, i + n2) + " at " + i);
//break;
}
}
if (!foundIt) System.out.println("oops !.");
}
}