/// <summary>
/// 字符串分割符
/// </summary>
public const string SEPARATORSTRING = "@#$@#$";
/// <summary>
/// 取得字符的第几个字符,通过分割符分割的字符串,pos以1开始
/// </summary>
/// <param name="str">字符串</param>
/// <param name="separator">分割符</param>
/// <param name="pos">第几个</param>
/// <returns>返回第几个字符串</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>
/// 取得字符的第几个字符,通过分割符分割的字符串,pos以1开始,以默认的字符串分割
/// </summary>
/// <param name="str">字符串</param>
/// <param name="pos">第几个</param>
/// <returns>返回第几个字符串</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>
/// 取得分割后的字符串数据,数组以0开始
/// </summary>
/// <param name="str">字符串</param>
/// <param name="separator">分割符</param>
/// <returns>返回分割后的字符串数组</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>
/// 取得分割后的字符串数据,数组以0开始,以默认的字符串分割
/// </summary>
/// <param name="str">字符串</param>
/// <returns>返回分割后的字符串数组</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;
}