Several common operations on strings

Source: Internet
Author: User

1. Determine whether a character contains a specific character:

Public void FindStringchar (string A, string B)
{
If (A. IndexOf (B)>-1)
{
Console. WriteLine ("exists! ");
}
Else
{
Console. WriteLine ("does not exist! ");
}
}

For example:

String A = "abceFC (";
String B = "x ";

String C = "B ";

2. determine the number of times the substring in the string appears in the main string

Public int FindSubstringCount (string mainString, string subString)
{
String interString = mainString. Replace ("", ""); // remove space
Int count = 0; // counter
For (int I = 0; I <interString. Length-2; I ++)
{
String findString = interString (I, subString. Length );
If (findString = subString)
{
Count ++;
}
} // End

Return count;
} // End FindSubstringCount ()

For example:

MainString = "abc beefff cadde cabb bbace atbba ";

SubString = "bb ";

This program is slightly rewritten to find the location of the same string in the string, and find the characters after the same string,

Or read the output into the array, and so on.

3. Read the substring from a character.

String mainString = "goodpackga <AB> AB ";
String resultString = string. Empty;

Int end = mainString. IndexOf ("<");
RestultString = mainString. Substring (0, end );

Related Article

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.