说明: 这些字符串处理小技巧在平时的编程中会普遍遇到, 总结了很多, 这里只提供一些比较好玩供大家参考.
实现的基本方法都是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关键字用于将对象变量从实际对象中分离开来。
补充说明: 一些常用的字符串处理技术如首字母转化为大写 , 字符串比较, 添加子串等操作比较简单, 此处略