本帖最后由 sxdxgzr@126.com 于 2013-8-1 23:52 编辑
一、Encoding编码解码using System;
using System.IO;
using System.Text;
namespace StringHandle
{
class Program
{
const string FileName = "权限分组脚本.txt";//仅指定文件名称,查找时默认去应用程序根目录下找文件,找不到则异常
static void Main(string[] args)
{
if (!File.Exists(FileName))
{
Console.WriteLine("{0}文件不存在", FileName);
return;
}
byte[] result = File.ReadAllBytes(FileName);
#region 常用编码器列子
//字节数组转字符串 解码 字节数组=》指定编码的字符
string oldContent = Encoding.Default.GetString(result);//与当前Os紧密关联的代码页
oldContent = Encoding.ASCII.GetString(result); //7为代表一个字符(还有一位不使用) 此时解码部分错误原因编码问题
oldContent = Encoding.Unicode.GetString(result); //16位代表一个字符序列 此时解码错误原因编码问题
oldContent = Encoding.UTF32.GetString(result); //32位代表一个字符序列 此时解码错误原因编码问题
oldContent = Encoding.UTF7.GetString(result); //7位代表一个字符序列 此时解码部分错误原因编码问题
oldContent = Encoding.UTF8.GetString(result); //8位代笔一个字符序列 此时解码部分错误原因编码问题
oldContent = Encoding.GetEncoding("gb2312").GetString(result);//简体中文代码页对应的编码器 此时能正确解码,此编码器于default一致效果。
//字符串转字节数组 字符=》指定编码字符序列
result = Encoding.Default.GetBytes(oldContent); //编码,每个字符编码为一个默认编码器对应的码位(数字表示 位数不定 )
result = Encoding.ASCII.GetBytes(oldContent); //编码,每个字符编码为一个ASCII对应的码位(数字表示 位数8 )
result = Encoding.Unicode.GetBytes(oldContent); //编码,每个字符编码为一个Unicode对应的码位(数字表示 位数16)
oldContent = Encoding.Unicode.GetString(result); //解码,此时能正确解码
result = Encoding.UTF32.GetBytes(oldContent); //编码,每个字符编码为一个UTF32对应的码位(数字表示 位数32)
result = Encoding.UTF7.GetBytes(oldContent); //编码,每个字符编码为一个UTF7对应的码位(数字表示 位数8)
result = Encoding.UTF8.GetBytes(oldContent); //编码,每个字符编码为一个UTF8对应的码位(数字表示 位数8)
result = Encoding.GetEncoding("gb2312").GetBytes(oldContent);//编码,每个字符编码为一个默认编码器对应的码位(数字表示 位数不定 )
#endregion
//获取简体中文编码编码读取的文本内容
string content = File.ReadAllText(FileName,Encoding.GetEncoding("gb2312"));
//输出读取的文本内容
Console.WriteLine(content);
Console.ReadKey();
}
}
}
二、Split.
using System;
class Sample
{
/*
* Split 不管重载版本总得来说:
*
* 入参:1分隔符 字符数组或字符串数组,2返回数组个数 ,3是否返回空字符数组元素
* 返回值:字符串数组,数组元素包含分隔实例的子字符串。
* 算法:1根据分隔符分隔字符串
* 2分隔符相邻或者分隔符出现在实例的开头或结尾则填充一个相邻的空字符元素
* 3若指定了返回数组元素的个数大于分隔产生元素的个数,则返回分隔后的数组元素,否则返回count-1个分隔产生的
* 字符串元素,后面部分返回与实例对应的字符创。
*/
public static void Main()
{
string s1 = ",ONE,,TWO,,,THREE,,";
string s2 = "[stop]" +
"ONE[stop][stop]" +
"TWO[stop][stop][stop]" +
"THREE[stop][stop]";
char[] charSeparators = new char[] { ',' };
string[] stringSeparators = new string[] { "[stop]" };
string[] result;
Console.WriteLine("1) Split a string delimited by characters:\n");
Console.WriteLine("1a )The original string is \"{0}\".", s1);
Console.WriteLine("The delimiter character is '{0}'.\n",
charSeparators[0]);
Console.WriteLine("1b) Split a string delimited by characters and " +
"return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);
Console.WriteLine("1c) Split a string delimited by characters and " +
"return all non-empty elements:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
Console.WriteLine("1d) Split a string delimited by characters and " +
"return 2 elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);
Console.WriteLine("1e) Split a string delimited by characters and " +
"return 2 non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
Console.WriteLine("2) Split a string delimited by another string:\n");
Console.WriteLine("2a) The original string is \"{0}\".", s2);
Console.WriteLine("The delimiter string is \"{0}\".\n", stringSeparators[0]);
Console.WriteLine("2b) Split a string delimited by another string and " +
"return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result);
Console.WriteLine("2c) Split a string delimited by another string and " +
"return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
Console.WriteLine("2d) Split a string delimited by another string and " +
"return 2 elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result);
Console.WriteLine("2e) Split a string delimited by another string and " +
"return 2 non-empty elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
Console.ReadKey();
}
// Display the array of separated strings.
public static void Show(string[] entries)
{
Console.WriteLine("The return value contains these {0} elements:", entries.Length);
foreach (string entry in entries)
{
Console.Write("<{0}>", entry);
}
Console.Write("\n\n");
}
}
三、Trimusing System;
public class TrimTest
{
/*
* Trim() 移除指定字符串的前导和后导空字符 字符串中间部分的空格并不会移除
* trim(char[]) 移除指定字符串中前导和后导的在指定参数中的字符
* 相应的TrimStart,TrimEnd分别移除前导,后导字符*
*
*/
public static void Main()
{
string[] temp = MakeArray();
Console.WriteLine("Concatenating the inital values in the array, we get the string:");
Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);
// trim whitespace from both ends of the elements
for (int i = 0; i < temp.Length; i++)
temp = temp.Trim();
Console.WriteLine("Concatenating the trimmed values in the array, we get the string:");
Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);
// reset the array
temp = MakeArray();
// trim the start of the elements. Passing null trims whitespace only
for (int i = 0; i < temp.Length; i++)
temp = temp.TrimStart(null);
Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:");
Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);
// reset the array
temp = MakeArray();
// trim the end of the elements. Passing null trims whitespace only
for (int i = 0; i < temp.Length; i++)
temp = temp.TrimEnd(null);
Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:");
Console.WriteLine("'{0}'", String.Concat(temp));
}
private static string[] MakeArray()
{
string[] arr = { " please ", " tell ", " me ", " about ", " yourself " };
return arr;
}
}
四 、Pad
using System;
namespace Pad
{
/*
* 第二个参数指定了用于填充的unicode字符
* 其实第一个入参指定了执行后字符串的长度,若小于给定实例的长度则不填充,若大于,则PadLeft左侧填充(右对齐),PadRight右侧填充(左对齐。)
*/
class Program
{
static void Main(string[] args)
{
Console.WriteLine("a b c".PadLeft(6,'$'));
Console.WriteLine("a b c".PadRight(6,'$'));
Console.ReadKey();
}
}
}
其他的substring,indexof,lastindexof,insert,remove,repalce,format比较简单就不贴代码了。希望能帮到大家。
|