- 简单说明
string 字符串关键字,用于存放字符串,编码为unicode。
-
构造方法
string str = "xLoli.Net"
类 对象 内容
-
以下是常用方法
写法 解释
str.ToUpper() 全部转换成大写。
str.ToLower() 全部转换成小写。
str.Substring(int1,int2) 截取字符串(从int1开始取int2个)。
str.IndexOf('c') 查找c所在的位置。
str.Replace("string1","string2") 将字符串里面的string1替换成string2。
str.Remove(int1,int2) 从int1开始删除int2个字符。
str.Concat("string1","string2") 2个字符串string1 和 string2 连接,合二为一的说。
- 附加说明
str.Concat可简化. string3 = string1 + string2
*Ps: "+" 意思是连接,效果与str.Concat效果一样。
-
属性
str.Length() 字符串长度
- 代码
static void Main(string[] args)
{
string str = "Hello xLoli.Net";
string str1 = "xLoli.Net 赛高!";
string cache = string.Concat(str, str1);
Console.WriteLine("转换成小写:\t\t"+str.ToLower()); //全部转换成大写。
Console.WriteLine("转换成大写:\t\t"+str1.ToUpper()); //全部转换成小写。
Console.WriteLine("截取xLoli.Net:\t\t"+str.Substring(6,9)); //截取字符串(从int1开始取int2个)。
Console.WriteLine("检查x所在的位置:\t\t"+str.IndexOf('x')) ; //查找char所在的位置。
Console.WriteLine("替换Hello为Welcome to\t\t"+str.Replace("Hello","Welcome to")); //将字符串里面的string1替换成string2。
Console.WriteLine("去除从0开始到第5个字符:\t\t"+str.Remove(0,5)); //从int1开始删除int2个字符。
//合体
Console.WriteLine("第1种合体方式:\t\t"+cache); //2个字符串string1 和 string2 连接,合二为一
cache = str+str1;
Console.WriteLine("第2种合体方式:\t\t"+cache);
}
- 输出结果
转换成小写: hello xloli.net
转换成大写: XLOLI.NET 赛高!
截取xLoli.Net: xLoli.Net
检查x所在的位置: 6
替换Hello为Welcome to Welcome to xLoli.Net
去除从0开始到第5个字符: xLoli.Net
第1种合体方式: Hello xLoli.NetxLoli.Net 赛高!
第2种合体方式: Hello xLoli.NetxLoli.Net 赛高!
-
备注
这里只列出了常用的几个string方法属性,如需查看更多,请查询MSDN。
你太蛋疼了
呜呜,我不会C
我是路过的。过来看看。。呵呵。
现在远离了这些哦
梨花,你完全转向了技术博客了啊,可惜的是哥一点也看不懂,唉,看来以后来你博客的机会很少了,死梨花,其实哥有点想念你呢。。。。。
忽然想起来,就过来看看~你写的东西越来越深奥了~