Package JAVA;
Import java.awt.List;
Import java.util.ArrayList;
/**
*
* @author Beam small fish
*
*/
public class MyTest {
public static void Main (string[] args) {
Finds whether a string exists in the target string
Boolean isexit = Isexit ("f", "ABFSDFSDKJL;FAS;DLFSLDF;ASDFSDFASZDF");
System.out.println (Isexit);
}
private static Boolean Isexit (String Strson, String strmother) {
Set whether a flag bit exists
Boolean Isflag = false;
Get the length of the mother string
Integer strmotherlength = Strmother.length ();
Get the length of the substring
Integer strsonlength = Strson.length ();
if (strmother==null| | strmotherlength<strsonlength| | Strson.equals ("") | | Strson==null) {
return false;
}
SYSTEM.OUT.PRINTLN ("The input string is:" + strmother);
SYSTEM.OUT.PRINTLN ("Input substring:" + Strson);
Array of stored results
arraylist<boolean> Reasonarr = new arraylist<boolean> ();
Number of occurrences of characters
Integer apptime = 0;
Converting a parent string to a character array
char[] Chararraymother = Strmother.tochararray ();
Traversing the array of strings in substring length
for (int i = 0; I < (chararraymother.length-strsonlength + 1); i++) {
Traversal start is 0, end array length minus substring length
The comparison of the minimum unit traversal with the substring length
Write a comparison of the word method, passed in the parameter has a string array, the current traversal position, substring
Integer flag = compare (Chararraymother,strson,i);
if (flag = = 1) {
Reasonarr.add (TRUE);
}else {
Reasonarr.add (FALSE);
}
}
In this Settlement
for (Boolean Bool:reasonarr) {
if (bool==true) {
Apptime + = 1;
}
}
System.out.println ("character:" + Strson + "in" + Strmother + "appears in the number of times:" + Apptime + "times!) ");
if (apptime>0) {
return true;
}
return isflag;
}
/**
* Methods for dealing with comparisons
* @param chararraymother
* @param Strson
* @param i
* @return
*/
private static Integer compare (char[] chararraymother, String strson, int i) {
The character of the substring length unit that is taken out of the current traverse position.
Create a character bed to hold out the characters
String comstr = "";
for (int j = 0; J < Strson.length (); j + +) {
char C = chararraymother[i+j];
Comstr + = string.valueof (c);
}
System.out.println ("+ (i+1) +" truncated string is: "+ comstr);
if (Strson.equals (COMSTR)) {
return 1;
}
return 0;
}
}
Write queries in Java whether a string contains another string and the number of occurrences