系列文章——关于C#,你应该知道的2000件事情(002)-创新互联

该系列文章从关于C#,你应该知道的2000件事情翻译

公司主营业务:做网站、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出莫力达免费做网站回馈大家。

6. 用Reverse方法反转一个字符串

string funnyMan = "Roscoe Arbuckle";
string backwardsGuy = new string(funnyMan.Reverse().ToArray());
//backwardsGuy="elkcubrA eocsoR";

7. 使用String.Split把字符串分割成子字符串

string names = "John,Mary,Elvis,Ringo";//names = "John,Mary,Elvis,Ringo?I'm fine";
//Split参数是数组,所以可以多个字符作为分隔符
string[] nameList = names.Split(new char[] { ','});//new char[] { ',','?','\'',' '}
Console.WriteLine(nameList[0]);    // John
Console.WriteLine(nameList[1]);    // Mary
Console.WriteLine(nameList[2]);    // Elvis
Console.WriteLine(nameList[3]);    // Ringo

也可以使用循环来遍历string数组
string names = "John - Mary - Elvis - Ringo";
// Same result as before - we get four names, without spaces or dash
string[] nameList = names.Split(new string[] { " - " }, 
                                      StringSplitOptions.RemoveEmptyEntries);
foreach (string str in nameList)
{
    Console.WriteLine(str);
}
参数指定移除空格

8.字符串函数连在一起操作

char[] braces = new char[] { '{', '}' };
string s = "{This|That|Such}";
s = s.Replace("|", " and ").Trim(braces).Insert(0, "=> ").ToLower();
Console.WriteLine(s);       // => this and that and such

可以将操作的字符串的函数在一行中实现

9. 通过Trim方法在字符串中减少前导和尾随字符

string s = "  The core phrase";  // 2 leading spaces, 1 trailing
s = s.Trim();     // s = "The core phrase"
注意:(1)Trim()方法默认只是去掉开头和结尾的空格,不会去掉字符串中间的空格。
    (2)任何对字符串的操作,都不改变原字符串的值,都会返回一个新的实例,需要赋值给一个变量,才能得到对字符串操作结果的字符串。
  • 也可以给Trim()方法附加参数,指定要截去的字符

string s = "  {The core phrase,} ";
s = s.Trim(new char[] { ' ', '{', ',', '}' });// s = "The core phrase"
s = " {Doesn't {trim} internal stuff }";
s = s.Trim(new char[] { ' ', '{', '}' });// s = "Doesn't {trim} internal stuff"

  • 也可以通过TrimStart和 TrimEnd方法截去字符串的开头和结尾

string s = "{Name}";
char[] braces = new char[] { '{', '}' };
string s2 = s.TrimStart(braces); // s2 = "Name}"
string s3 = s.TrimEnd(braces); // s3 = "{Name"

10. 插入和移除子字符串

可以通过String.Insert方法在一个字符串的任何位置插入一个子字符串。

string s = "John Adams";
int n = s.IndexOf("Adams");
s = s.Insert(n, "Quincy "); // s now "John Quincy Adams"

注意:字符串时不可变的,即使调用Insert方法,如果未把操作的结果赋给任何变量,对原字符串没有任何影响。

string s = "John Adams";
s.Insert(5, "Quincy "); // Allowed, but s is not changed

可以通过String.Remove方法指定下标开始,指定长度的子字符串,字符串的下标从0开始。

string s = "OHOLEne";
s = s.Remove(1, 4); // Start at position 1, remove 4 characters,Now s is "One"

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。

文章题目:系列文章——关于C#,你应该知道的2000件事情(002)-创新互联
本文来源:https://www.cdcxhl.com/article4/digjoe.html

成都网站建设公司_创新互联,为您提供服务器托管外贸网站建设微信小程序响应式网站网站设计公司Google

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站托管运营