欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 常用字符串处理方法

程序员文章站 2024-03-07 10:25:15
string fox; fox.tolower()转化成小写字母 fox.toupper()转化成大写字母 fox.trim()删除前后空格 fox.trim(trimch...
string fox;
fox.tolower()转化成小写字母
fox.toupper()转化成大写字母
fox.trim()删除前后空格
fox.trim(trimchars)删除其它字符
fox.trimstart()删除前空格
fox.trimend()删除后空格
fox.padleft(10)增加左边空格,使字串达到某长度。
fox.padright(10)增加右边空格,使字串达到某长度。
fox.padx(10,'-')增加其它字符,使字串达到某长度。x指:left/right
fox.split(' ')将字串分解成数组

system.text.encoding.default.getbytecount(fox);获得字符串长度,一个汉字等于俩字符

//获得汉字的区位码
byte[] array = new byte[2];
array = system.text.encoding.default.getbytes("啊");

int i1 = (short)(array[0] - '\0');
int i2 = (short)(array[1] - '\0');

//unicode解码方式下的汉字码
array = system.text.encoding.unicode.getbytes("啊");
i1 = (short)(array[0] - '\0');
i2 = (short)(array[1] - '\0');

//unicode反解码为汉字
string str = "4a55";
string s1 = str.substring(0,2);
string s2 = str.substring(2,2);

int t1 = convert.toint32(s1,16);
int t2 = convert.toint32(s2,16);

array[0] = (byte)t1;
array[1] = (byte)t2;

string s = system.text.encoding.unicode.getstring(array);

//default方式反解码为汉字
array[0] = (byte)196;
array[1] = (byte)207;
s = system.text.encoding.default.getstring(array);