說明: 這些字符串處理小技巧在平時的編程中會普遍遇到, 總結了很多, 這裡只提供一些比較好玩供大家參考.
實現的基本方法都是C#提供的關於字符串處理的常用方法, 此處不再一一說明.
一. 根據標點符號分行某一字符串
輸入: abc,defg,hijklmn,opq,rstuv (測試時按逗號分行, 可自定義分行符)
輸出: abc
defg
hijklmn
opq
rstuv
Code
string oldstr = textBox1.Text.Trim();
string[] newstr = oldstr.Split(',');
for (int i = 0; i < newstr.Length; i++)
{
if (richTextBox1.Text == "")
richTextBox1.Text = newstr[i].ToString();
else
richTextBox1.Text += "\n" + newstr[i].ToString();
}
2. 將字符串顛倒輸出
輸入: ABCDEFG
輸出: GFEDCBA
Code
string str1 = textBox1.Text.Trim();
char[] charstr = str1.ToCharArray();
Array.Reverse(charstr);
string str2 = new string(charstr);
textBox2.Text = str2;
3. 巧截字符串的數字
輸入: A23BCDEFG4Hi678
輸出: 234678
Code
CharEnumerator CEnumerator = textBox1.Text.GetEnumerator();
while (CEnumerator.MoveNext())
{
byte[] array = new byte[1];
array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString());
int asciicode = (short)(array[0]);
if (asciicode >= 48 && asciicode <= 57)
{
textBox2.Text += CEnumerator.Current.ToString();
}
}
4. 找出字符串中某一字符的所有位置
輸入: aBcdaEFGaHIaaaK, 查找字符: a
輸出: 0,4,8,11,12,13
Code
string str = textBox1.Text.Trim();
char[] myChar = str.ToCharArray();
for (int i = 0; i < myChar.Length; i++)
{
if (myChar[i].ToString() == textBox2.Text.Trim())
MessageBox.Show("字符串" + textBox2.Text.Trim() + "在" + textBox1.Text.Trim() + "中的位置為:" + i.ToString() + "\n");
}
5.從字符串分離文件路經, 文件名及擴展名
輸入: C:\gdiplus.dll
輸出: 路徑: C
文件名: gdiplus
擴展名:dll
Code
string strPath = textBox1.Text.Substring(0, textBox1.Text.LastIndexOf("\\"));
string strName=textBox1.Text.Substring(textBox1.Text.LastIndexOf("\\")+1,(textBox1.Text.LastIndexOf(".")-textBox1.Text.LastIndexOf("\\")-1) );
string strEName = textBox1.Text.Substring(textBox1.Text.LastIndexOf(".")+1, (textBox1.Text.Length - textBox1.Text.LastIndexOf(".")-1));
MessageBox.Show("文件路徑:"+strPath +"\n 文件名:"+strName +"\n 文件擴展名:"+strEName ,"信息",MessageBoxButtons.OK,MessageBoxIcon.Information );
6.批量替換某一類字符串
輸入: abcdsfjlsdkfjalsdkabcdefadslkfjlksdafabc
查找: abc
替換: ***
輸出: ***dsfjlsdkfjalsdk***defadslkfjlksdaf***
Code
public int M_int_index = -1;
private int M_int_start;
private int M_int_end;
M_int_index = 0;
while (M_int_index != -1)
{
M_int_start = 0;
M_int_end = richTextBox1.Text.Trim().Length;
M_int_index = richTextBox1.Find(this.textBox1.Text.Trim(), M_int_start, M_int_end, RichTextBoxFinds.None);
if (M_int_index == -1)
{
MessageBox.Show(this, "全部'" + this.textBox1.Text + "'已替換完畢。", "未找到",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
richTextBox1.SelectedText = textBox2.Text;
M_int_index += this.textBox1.Text.Length;
}
}
7.把一個按空格分割的字符串存儲在一個數組中 (此處測試用ArrayList)
輸入: abc def ghiklm opq
輸出: 可按數組下標輸出: 如 arr[1]=def
Code
string str = "abc def ghiklm opq";
string[] strArr = str.Split(' ');
System.Collections.ArrayList mylist = new System.Collections.ArrayList();
foreach (string strArray in strArr)
{
mylist.Add(strArray);
}
listBox1.DataSource = mylist;
8.對字符串進行加密
輸入: abc
輸出: cvJ5W08AdsA=
Code
textBox1.ReadOnly = false;
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Encoding.Unicode.GetBytes(textBox1.Text.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
textBox2.Text = Convert.ToBase64String(MStream.ToArray());
textBox3.Text = "";
textBox3.ReadOnly = true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
9.對字符串進行解密 (與上面例子配合使用)
輸入: cvJ5W08AdsA=
輸出: abc
Code
textBox3.ReadOnly = false;
try
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
byte[] key = Encoding.Unicode.GetBytes(encryptKey);
byte[] data = Convert.FromBase64String(textBox2.Text.Trim());
MemoryStream MStream = new MemoryStream();
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);
CStream.FlushFinalBlock();
textBox3.Text = Encoding.Unicode.GetString(MStream.ToArray());
textBox1.Text = "";
textBox1.ReadOnly = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
10.區別 0, 空字符串, Null, Empty和 Nothing
(1).對於聲明後未賦值的數值類型變量,它們的默認值為0;
(2).對於聲明後未賦值的字符串變量,則缺省值為空字符串"";
(3).Null關鍵字說明變量不包含有效數據,它是將Null值顯式地賦值給變量的結果,也可能是包含Null的表達式之間進行運算的結果。
(4).Empty關鍵字表示未初始化的變量的缺省值。
(5).Nothing關鍵字用於將對象變量從實際對象中分離開來。
補充說明: 一些常用的字符串處理技術如首字母轉化為大寫 , 字符串比較, 添加子串等操作比較簡單, 此處略
文章來源: http://www.cnblogs.com/ziyiFly/archive/2008/09/17/1292488.html