码迷,mamicode.com
首页 > 其他好文 > 详细

字符串2014年6月7日17:29:44

时间:2014-06-09 22:47:39      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

---恢复内容开始---

字符串替换:string Replace(string oldValue, string newValue)将字符串中的出现oldValue的地方替换为newValue。例子:名字替换。

1             string s = "abc1234";
2             s = s.Replace("a", "A");//第一个参数是旧的,第二个是新的
3             Console.WriteLine(s);

 

 

取子字符串:string Substring(int startIndex),取从位置startIndex开始一直到最后的子字符串;

1             string s = "abcdef12";
2             //string s1 = s.Substring(2);//从位置2开始一直到最后的子字符串"cdef12"
3             string s1 = s.Substring(2, 3);//第二个参数是长度,不是结束位置"cde"
4             Console.WriteLine(s1);

 

 

string Substring(int startIndex, int length),取从位置startIndex开始长度为length的子字符串,如果子字符串的长度不足length则报错。

案例:截取字符串前5个,string s2 = s1.Substring(0, Math.Min(s1.Length, 5));//取字符串的长度和5中的最小值来截取。谁小娶谁。

1             string s = "abc12f";
2             //string s1 = s.Substring(2, s.Length - 2>5?5:s.Length - 2);
3             //Math.Min返回两个参数中的最小值
4             string s1 = s.Substring(2, Math.Min(s.Length - 2,5));
5             Console.WriteLine(s1);

 

bool Contains(string value)判断字符串中是否含有子串value;

bubuko.com,布布扣
1             string s = "伟大的共党召开大会";
2             if (s.Contains("共党"))//判断字符串中是否含有子字符串
3             {
4                 Console.WriteLine("禁止发表");
5             }
6             else
7             {
8                 Console.WriteLine("可以发表");
9             }
bubuko.com,布布扣

 

bool EndsWith (string value)判断字符串是否以子串value结束

bool StartsWith(string value)判断字符串是否以子串value开始

bubuko.com,布布扣
1             string s = "ATM我是你们的忠实听众";
2             if (s.StartsWith("ATM"))//是否以某个字符串开头
3             {
4                 Console.WriteLine("短信正确");
5             }
6             else
7             {
8                 Console.WriteLine("短信错误");
9             }
bubuko.com,布布扣

 

int IndexOf(string value):取子串value第一次出现的位置。

1             string s = "我北爱北京敏感词";
2             //int i = s.IndexOf("北京");
3             int i = s.IndexOf("南京");
4             //子字符串在字符串中第一次出现的位置(以第一个字符为序号)
5             //如果不存在则返回-1
6             Console.WriteLine(i);

 

例子1.接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。"abc"→"cba"

1             string s = Console.ReadLine();
2             for (int i = s.Length - 1; i >= 0; i--)//从最后一个开始倒着循环
3             {
4                 char ch = s[i];
5                 Console.WriteLine(ch);
6             }

 

例子2.接收用户输入的一句英文,将其中的单词以反序输出。"hello c sharp"→"sharp c hello"

bubuko.com,布布扣
1             string s = Console.ReadLine();
2             //以空格分割,并去除结果中的空白字符串(第二个参数功能)
3             string[] strs = s.Split(new char[] {   },StringSplitOptions.RemoveEmptyEntries);
4             //倒序输出
5             for (int i = strs.Length - 1; i >= 0; i--)
6             {
7                 string str = strs[i];
8                 Console.WriteLine(str);
9             }
bubuko.com,布布扣

例子3.从Email中提取出用户名和域名:abc@163.com。IndexOf找到@的位置。SubString。要考虑Email地址错误的情况(必须含有@,并且@不能在最开始、最后出现),如果错误告诉用户。

bubuko.com,布布扣
 1             //从Email中提取出用户名和域名:abc@163.com。
 2             //IndexOf找到@的位置。SubString。
 3             //要考虑Email地址错误的情况(必须含有@,并且@不能在最开始、最后出现),如果错误告诉用户。
 4 
 5             string email = "abc@163.com";
 6             if (!email.Contains("@") || email.StartsWith("@") || email.EndsWith("@"))
 7             {
 8                 Console.WriteLine("Email地址不正确");
 9                 return;
10             }
11             int atIndex = email.IndexOf("@");
12             string username = email.Substring(0, atIndex);
13             string domain = email.Substring(atIndex + 1);
14             Console.WriteLine("用户名:{0},域名:{1}", username, domain);
bubuko.com,布布扣

例子4.文本文件中存储了多个文章标题、作者,标题和作者之间用若干空格(数量不定)隔开,每行一个,标题有的长有的短,输出到控制台的时候最多标题长度20,如果超过20,则截取长度17的子串并且最后添加“...”,加一个竖线后输出作者的名字。

 

---恢复内容结束---

字符串2014年6月7日17:29:44,布布扣,bubuko.com

字符串2014年6月7日17:29:44

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/skyl/p/3774996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!