Write queries in Java whether a string contains another string and the number of occurrences

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.