string基本操作

来源:互联网 发布:淘宝有什么好的内衣店 编辑:程序博客网 时间:2024/05/17 06:03

原文地址:http://www.dingos.cn/index.php?topic=2002.0

这些示例显示在Csharpstring类型最常用的操作

 

// 声明一个string变量并初始化它的值

string myLine = "dotnetspider.com is the best technology site";

string str;

// 提取字符串 从第0位开始取得16个字符

str = myLine.Substring(0, 16);

Console.WriteLine(str); // 显示 : dotnetspider.com

//提取字符串 从第17位开始取12个字符

str = myLine.Substring(17, 12);

Console.WriteLine(str); // 显示 : is the best 

// 拆分这行字符串。基于空格拆分(""

string[] strArray = myLine.Split(' ');

Console.WriteLine("strArray has " + strArray.Length + " words"); 

// 显示 : strArray has 6 words

// 将所有字符转换为大写字母

str = myLine.ToUpper();

Console.WriteLine(str); // 显示 : DOTNETSPIDER.COM IS THE BEST TECHNOLOGY SITE

// 在字符串中查找单词的位置

int position = myLine.IndexOf("best");

Console.WriteLine(position); // 显示 : 24

// 在字符串指定位置插入另一个字符串

str = myLine.Insert(20, "one of ");

Console.WriteLine(str); 

// 显示 : dotnetspider.com is one of the best technology site

原创粉丝点击