The number of times the specified character appears in the search string in C #.

Source: Internet
Author: User

I am still studying. I want to write down some of the knowledge I learned every day. This will deepen my memory and facilitate my review later. Come on!


Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;


Namespace StringOperate

{

/// <Summary>

/// String operation class

/// </Summary>

Class StrOperate

{

Private static int count; // number of times


Public static int Count

{

Get {return StrOperate. count ;}

Set {StrOperate. count = value ;}

}


Private string strContent; // string


Public string StrContent

{

Get {return strContent ;}

Set {strContent = value ;}

}


Private char searchChar; // The characters to be queried


Public char SearchChar

{

Get {return searchChar ;}

Set {searchChar = value ;}

}


/// <Summary>

/// Define the constructor and initialize the data

/// </Summary>

Public StrOperate (string strContent, char searchChar)

{

This. strContent = strContent;

This. searchChar = searchChar;

}


# Region // number of times a specified character appears in the query string


# Region method 1

/// <Summary>

/// In C #, a string is a character array,

/// Cyclically traverse each element in the character array,

/// Determine the number of times the specified Char appears

/// </Summary>

/// <Returns> specify the number of occurrences of a Char </returns>

Public int SearchCharCount_1 ()

{

Count = 0;

// Cyclically traverse each element (character) in string)

For (int I = 0; I <strContent. Length; I ++)

{

// Determine whether it is equal to the specified character

If (strContent [I] = searchChar)

{

// If the value is equal to, the number of times is increased by 1.

Count ++;

}

}

// Return times

Return count;

}

# Endregion


# Region method 2

/// <Summary>

/// Obtain the number of times through the IndexOf () function and Substring () function

/// </Summary>

/// <Returns> specify the number of occurrences of a Char </returns>

Public int SearchCharCount_2 ()

{

Count = 0;

String strCont = strContent;

While (true)

{

// Use the IndexOf () function to obtain the position where the specified character (searchChar) appears for the first time in the current string.

Int indexChar = strCont. IndexOf (searchChar );

// If the position is greater than or equal to 0, this character exists.

If (indexChar> = 0)

{

// Increase the number of times by 1

Count ++;

// String Update (use the Substring () function to extract the string that appears after the specified character for the next search)

StrCont = strCont. Substring (indexChar + 1 );

}

// Otherwise, if the position is smaller than 0, the current string does not contain any character to be searched.

Else

{

// End the loop

Break;

}

}

// Return times

Return count;

}

# Endregion


# Region method 3

/// <Summary>

/// Obtain the number of times through the Split () function

/// </Summary>

/// <Returns> specify the number of occurrences of a Char </returns>

Public int SearchCharCount_3 ()

{

Count = 0;

// Split the string according to the specified characters, so how many characters will be split)

String [] strs = strContent. Split (searchChar );

// For (int I = 0; I <strs. Length; I ++)

//{

// Console. WriteLine ("-" + strs [I] + "-");

//}

Count = strs. Length-1;

Console. WriteLine ("Lenght =" + strs. Length );

Return count;

}

# Endregion


# Region method 4

/// <Summary>

/// Obtain the number of times through the LastIndexOf () function and Substring () function

/// </Summary>

/// <Returns> specify the number of occurrences of a Char </returns>

Public int SearchCharCount_4 ()

{

Count = 0;

String strCont = strContent;

While (true)

{

// Use the IndexOf () function to obtain the position where the specified character (searchChar) appears for the first time in the current string.

Int indexChar = strCont. LastIndexOf (searchChar );

// If the position is greater than or equal to 0, this character exists.

If (indexChar> = 0)

{

// Increase the number of times by 1

Count ++;

// String Update (use the Substring () function to extract the string that appears after the specified character for the next search)

StrCont = strCont. Substring (0, indexChar );

}

// Otherwise, if the position is smaller than 0, the current string does not contain any character to be searched.

Else

{

// End the loop

Break;

}

}

// Return times

Return count;

}

# Endregion


# Region Method 5

/// <Summary>

/// Use the indexOf (), LastIndexOf (), and Substring () Functions

/// </Summary>

/// <Returns> specify the number of occurrences of a Char </returns>

Public int SearchCharCount_5 ()

{

Count = 0;

String strCont = strContent;

While (true)

{

Int indexof = strCont. IndexOf (searchChar );

Int lastIndexof = strCont. LastIndexOf (searchChar );

If (indexof! = LastIndexof)

{

Count + = 2;

StrCont = strCont. Substring (indexof + 1, lastIndexof-1-indexof );

// Bacdefa

// 0123456

}

Else

{

Break;

}

}

Return count;

}

# Endregion


# Region Method 6

/// <Summary>

/// Use string. ToArray () to convert the string into a character array.

/// </Summary>

/// <Returns> specify the number of occurrences of a Char </returns>

Public int SearchCharCount_6 ()

{

Count = 0;

Char [] chars = strContent. ToArray ();

For (int I = 0; I <chars. Length; I ++)

{

If (chars [I] = searchChar)

{

Count ++;

}

}

Return count;

}

# Endregion


# Endregion

}

}





This article is from the "My_Dream" blog, please be sure to keep this http://vitality.blog.51cto.com/7335206/1227159

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.