C#中一些字符串的操作

///
/// 反转一句话中的单词
///
/// 字符串
/// 反转后的字符串
public string ReversalWord( string s)
{
string [] sarray = s.Split( new char [] { ' ' , ' , ' });


// for (int i = 0; i <= (sarray.Length / 2 - 1); i++)
// {
// tempstr = sarray[i];
// sarray[i] = sarray[sarray.Length - 1 - i];
// sarray[sarray.Length - 1 - i] = tempstr;
// }

Array.Reverse(sarray);


return String.Join( " " , sarray);


}


///
/// 反转整个字符串
///
/// 【C#中一些字符串的操作】
///
public string ReversalChar( string s)
{
// 方法1
// string[] newarray = new string[s.Length];
// for (int i = 0; i < (s.Length); i++)
// {
// newarray[i] = s.Substring(i, 1);

// }
// Array.Reverse(newarray);

// return String.Join("", newarray);


// 方法2
char [] c = s.ToCharArray();
// for (int i = 0; i < c.Length/2-1; i++)
// {
// char temc = c[i];
// c[i] = c[c.Length - 1 - i];
// c[c.Length - 1 - i]=temc;
// }
Array.Reverse(c);
string s2 = new string (c);
return s2;

}
protected void Button4_Click( object sender, EventArgs e)
{
this .Label5.Text = ReversalWord( this .TextBox3.Text);

}
protected void Button5_Click( object sender, EventArgs e)
{
this .Label6.Text = ReversalChar( this .TextBox4.Text);
}
protected void Button6_Click( object sender, EventArgs e)
{
string maxchar;
this .Label7.Text = FindMaxChar( this .TextBox4.Text, out maxchar).ToString();
this .Label8.Text = test();
}


///
/// 找出一个字符串中重复字符的个数
///
///
///
public int FindMaxChar( string s, out string maxchar)
{
char [] c = s.ToCharArray();
int maxcount = 0 ;
int maxindex = 0 ;
for ( int i = 0 ; i < c.Length; i ++ )
{

int count = 1 ;

for ( int j = 0 ; j < c.Length; j ++ )
{
if ((i != j) && (c[j] == c[i]))
{
count ++ ;
}
}
if (count > maxcount)
{
maxcount = count;
maxindex = i;
}

}
maxchar = c[maxindex].ToString();


return maxcount;

}


///
/// 过滤数组中重复的数据,会区分大小写
///
///
///
public static string [] FilterRepeat( string [] _StringArray)
{
ArrayList _ArrayList = new ArrayList();
foreach ( string _String in _StringArray)
{
if ( ! _ArrayList.Contains(_String))
{
_ArrayList.Add(_String);
}
}
return ( string [])_ArrayList.ToArray( typeof ( string ));
}
///
/// 过滤字符串中重复字符
///
///
private string test()
{
string str1 = " 1,2,2,3,1,4,5,3,2 " ;
string str3 = "" ;
foreach ( char ch in str1)
{
if (ch == ' , ' || str3.IndexOf(ch) == - 1 )
str3 += ch;
}
return str3;
}

    推荐阅读