Split string function (C #)

Source: Internet
Author: User

/// <Summary>
/// String delimiter
/// </Summary>
Public const string separatorstring = "#$ @#$ ";

/// <Summary>
/// Obtain the character number. The string is separated by a delimiter. The POS starts with 1.
/// </Summary>
/// <Param name = "str"> string </param>
/// <Param name = "separator"> delimiter </param>
/// <Param name = "POS"> Number </param>
/// <Returns> returns the nth string </returns>
/// <Example> string STR = Split ("rrrrddrew @ tqwewerewddccc", "@", 1); </example>
Public static string split (string STR, string separator, int POS)
{
Int Len = Str. length;
Int COUNT = Str. Length-Str. Replace (separator, ""). length;
Int bitcount = 0;
String returnvalue = "";
String strvalue = "";
String inseparator = "";
If (COUNT = 0)
{
Return STR;
}
Inseparator = Str. substring (Str. Length-separator. length, separator. Length );
If (inseparator! = Separator)
{
STR + = separator;
}

For (INT I = 0; I <count; I ++)
{
Bitcount = Str. indexof (separator, 0 );
Strvalue = Str. substring (0, bitcount );
STR = Str. Replace (strvalue + separator ,"");
If (I = pos-1)
{
Returnvalue = strvalue;
Break;
}
}
Return returnvalue;
}
/// <Summary>
/// Obtain the nth character. It is a string separated by a delimiter. Pos starts with 1 and is separated by the default string.
/// </Summary>
/// <Param name = "str"> string </param>
/// <Param name = "POS"> Number </param>
/// <Returns> returns the nth string </returns>
/// <Example> string STR = Split ("rrrrddrew @ tqwewerewddccc", 1); </example>
Public static string split (string STR, int POS)
{
Int Len = Str. length;
Int COUNT = Str. Length-Str. Replace (separatorstring, ""). length;
Int bitcount = 0;
String returnvalue = "";
String strvalue = "";
String inseparator = "";
If (COUNT = 0)
{
Return STR;
}
Inseparator = Str. substring (Str. Length-separatorstring. length, separatorstring. Length );
If (inseparator! = Separatorstring)
{
STR + = separatorstring;
}

For (INT I = 0; I <count; I ++)
{
Bitcount = Str. indexof (separatorstring, 0 );
Strvalue = Str. substring (0, bitcount );
STR = Str. Replace (strvalue + separatorstring ,"");
If (I = pos-1)
{
Returnvalue = strvalue;
Break;
}
}
Return returnvalue;
}
/// <Summary>
/// Obtain the split string data. The array starts with 0.
/// </Summary>
/// <Param name = "str"> string </param>
/// <Param name = "separator"> delimiter </param>
/// <Returns> returns the split String Array </returns>
/// <Example> string STR = Split ("rrrrddrew @ tqwewerewddccc @", "@") [1]. tostring () </example>
Public static string [] Split (string STR, string separator)
{
Int Len = Str. length;
Int COUNT = 0;
Int bitcount = 0;
String strvalue = "";
String inseparator = "";
Inseparator = Str. substring (Str. Length-separator. length, separator. Length );
If (inseparator! = Separator)
{
STR + = separator;
}
Count = (Str. Length-Str. Replace (separator, ""). Length)/separator. length;
String [] returnvalue = new string [count];
For (INT I = 0; I <count; I ++)
{
Bitcount = Str. indexof (separator, 0 );
Strvalue = Str. substring (0, bitcount );
STR = Str. Replace (strvalue + separator ,"");
// MessageBox. Show (bitcount. tostring () + ";" + strvalue. tostring () + ";" + Str );
Returnvalue [I] = strvalue;

}
Return returnvalue;
}
/// <Summary>
/// Obtain the split string data. The array starts with 0 and is separated by the default string.
/// </Summary>
/// <Param name = "str"> string </param>
/// <Returns> returns the split String Array </returns>
/// <Example> string STR = Split ("rrrrddrew @ tqwewerewddccc @") [1]. tostring () </example>
Public static string [] Split (string Str)
{
Int Len = Str. length;
Int COUNT = 0;
Int bitcount = 0;
String strvalue = "";
String inseparator = "";
Inseparator = Str. substring (Str. Length-separatorstring. length, separatorstring. Length );
If (inseparator! = Separatorstring)
{
STR + = separatorstring;
}
Count = (Str. Length-Str. Replace (separatorstring, ""). Length)/separatorstring. length;
String [] returnvalue = new string [count];
For (INT I = 0; I <count; I ++)
{
Bitcount = Str. indexof (separatorstring, 0 );
Strvalue = Str. substring (0, bitcount );
STR = Str. Replace (strvalue + separatorstring ,"");
// MessageBox. Show (bitcount. tostring () + ";" + strvalue. tostring () + ";" + Str );
Returnvalue [I] = strvalue;

}
Return returnvalue;
}

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.