1.取字符串长度
string
str
=
"
中国
"
;
int Len = str.Length; // 得到字符串str的长度
int Len = str.Length; // 得到字符串str的长度
2.字符串转为比特码
byte
[] bytStr
=
System.Text.Encoding.Default.GetBytes(str);
// 然后可得到比特长度
len = bytStr.Length;
// 然后可得到比特长度
len = bytStr.Length;
3.字符串相加
System.Text.StringBuilder sb
=
new
System.Text.StringBuilder();
sb.Append( " 中华 " );
sb.Append( " 人民 " );
sb.Append( " 共和国 " );
// 和"+"等效, 但StringBuilder性能更好一些
string str = " 中华 " + " 人民 " + " 共和国 " ;
sb.Append( " 中华 " );
sb.Append( " 人民 " );
sb.Append( " 共和国 " );
// 和"+"等效, 但StringBuilder性能更好一些
string str = " 中华 " + " 人民 " + " 共和国 " ;
4.截取字符串的一部分
string
s1
=
str.SubString(
0
,
2
);
5.查指定位置是否为空字符
string
str
=
"
中华 人民
"
;
Response.Write( char .IsWhiteSpace(str, 2 ));
Response.Write( char .IsWhiteSpace(str, 2 ));
6.查字符是否是标点符号
Response.Write(
char
.IsPunctuation(
'
A
'
));
7.把字符转化为数字,查代码点
Response.Write((
int
)
'
中
'
);
8.把数字转化为字符,查代码代表的字符
Response.Write((
char
)
22269
);
9.清除字符串前后包含的空格
string
str
=
“ 中国
"
;
str = str.Trim();
str = str.Trim();
10.替换字符串:字符串变量.Replace(将原字符串替换为新的字符串)
string
str
=
"
中国
"
;
str = str.Replace( " 国 " , " 央 " ); // 将"国"字替换为"央"字
Response.Write(str);
str = str.Replace( " 国 " , " 央 " ); // 将"国"字替换为"央"字
Response.Write(str);
11.删除字符串最后一个字符的3种方法
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
string
s
=
"
1,2,3,4,5,
"
;
s = s.SubString( 0 , s.Length - 1 );
s = s.ToString().RTrim( ' , ' );
s = s.TrimEnd( ' , ' );
// 如果要删除"5,",则需要这么写
char [] MyChar = { ' 5 ' , ' , ' };
s = s.TrimEnd(MyChar);
// s = "1,2,3,4";
s = string .TrimEnd().Remove( string .Length - 2 , 1 );
s = s.SubString( 0 , s.Length - 1 );
s = s.ToString().RTrim( ' , ' );
s = s.TrimEnd( ' , ' );
// 如果要删除"5,",则需要这么写
char [] MyChar = { ' 5 ' , ' , ' };
s = s.TrimEnd(MyChar);
// s = "1,2,3,4";
s = string .TrimEnd().Remove( string .Length - 2 , 1 );