字符串创建操作

1.取字符串长度

 

string  str  =   " 中国 " ;
int  Len  =  str.Length;   // 得到字符串str的长度

 

 

2.字符串转为比特码

 

byte [] bytStr  =  System.Text.Encoding.Default.GetBytes(str);
// 然后可得到比特长度
len  =  bytStr.Length;

 

 

3.字符串相加

System.Text.StringBuilder sb  =   new  System.Text.StringBuilder();
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 ));

 

 

6.查字符是否是标点符号

 

Response.Write( char .IsPunctuation( ' A ' ));

 

 

7.把字符转化为数字,查代码点

 

Response.Write(( int ) ' ' );

 

 

8.把数字转化为字符,查代码代表的字符

 

Response.Write(( char ) 22269 );

 

 

9.清除字符串前后包含的空格

string  str  =  “ 中国  " ;
str  =  str.Trim();

 

10.替换字符串:字符串变量.Replace(将原字符串替换为新的字符串)

 

string  str  =   " 中国 " ;
str 
=  str.Replace( " " " " );  // 将"国"字替换为"央"字
Response.Write(str);

 

 

11.删除字符串最后一个字符的3种方法

 

ExpandedBlockStart.gif 代码
string  s  =   " 1,2,3,4,5, " ;

=  s.SubString( 0 , s.Length  -   1 );

=  s.ToString().RTrim( ' , ' );

=  s.TrimEnd( ' , ' );
// 如果要删除"5,",则需要这么写
char [] MyChar  =  { ' 5 ' ' , ' };
=  s.TrimEnd(MyChar);
// s = "1,2,3,4";

=   string .TrimEnd().Remove( string .Length  -   2 1 );

 

 

转载于:https://www.cnblogs.com/jxnclyk/archive/2010/04/21/1717619.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值