4.1.String类的应用

class String类应用
{
static void Main(string[] args)
{
string astring = "Now is The Time";
//拆分位置
int pos;
//单词
string word;
ArrayList words = new ArrayList();
pos = astring.IndexOf(" ");
while (pos > 0)
{
word = astring.Substring(0, pos);
words.Add(word);
astring = astring.Substring(pos + 1, astring.Length - (pos + 1));
pos = astring.IndexOf(" ");
Console.WriteLine("astring现在的值:" + astring);
}
Console.Read();
}
}

 

4.1.1.Split 方法和 Join 方法

1.Split 方法取得一条字符串后,就会把它分解成数据成分块,然后把这些块放入 String 数组内。

2.Join 方法从数组变为字符串。

string data = "Mike,McMillan,3000 W. Scenic,North Little Rock,AR,72118";
string[] sdata;
char[] delimiter = new char[] { ',' };
sdata = data.Split(delimiter, data.Length);
foreach (string tword in sdata)
Console.WriteLine(tword + " "); string joined;
joined = String.Join(",", sdata);
Console.Write("\n\t"+joined);

 

 

4.1.2.比较字符串

1.第一个要检测的比较方法就是 Equal 方法

string s1 = "foobar";
string s2 = "foobar";
if (s1.Equals(s2))
Console.WriteLine("They are the same.");
else
Console.WriteLine("They are not the same.");

 

2.第二个比较字符串的方法就是 CompareTo

string s1 = "foobar";
string s2 = "foobar"; int s11 = GetASCII(s1); Console.WriteLine(s1.CompareTo(s2)); // 相等 returns 0
s2 = "foofoo";
Console.WriteLine(s1.CompareTo(s2)); //S2低于S1 returns -1
s2 = "fooaar";
Console.WriteLine(s1.CompareTo(s2)); //S2高于S1 returns 1 int compVal = String.Compare(s1, s2);
switch (compVal)
{
case 0: Console.WriteLine(s1 + " " + s2 + " are equal");
break;
case 1: Console.WriteLine(s1 + " is less than " + s2);
break;
case 2: Console.WriteLine(s1 + " is greater than" + s2);
break;
default: Console.WriteLine("Can't compare");
break;
}

 

3.另外两种在处理字符串时会很有用的比较方法是 StartsWith EndsWith

string[] nouns = new string[] { "cat", "dog", "bird", "eggs", "bones" };
ArrayList pluralNouns = new ArrayList();
foreach (string noun in nouns)
if (noun.EndsWith("s"))
pluralNouns.Add(noun);
foreach (string noun in pluralNouns)
Console.Write(noun + " "); Console.WriteLine("\n"); string[] words = new string[] { "triangle", "diagonal", "trimester", "bifocal", "triglycerides" };
ArrayList triWords = new ArrayList();
foreach (string word in words)
if (word.StartsWith("tri"))
triWords.Add(word);
foreach (string word in triWords)
Console.Write(word + " ");

 

 

 

4.1.3.处理字符串的方法

字符串处理通常包括对字符串的改变操作。我们需要在字符串中插入新的字符,从字符串中移除字符,用新字符替换旧字符,改变某些字符的情况,以及向字符串添加空格或者从字符串中移除空格

1.Insert 方法和 Remove 方法

string s1 = "Hello, . Welcome to my class.";
string name = "TangSanSan";
int pos = s1.IndexOf(",");
s1 = s1.Insert(pos + 2, name);
Console.WriteLine(s1);
s1 = s1.Remove(pos + 2, name.Length);
Console.WriteLine(s1);

 

2.Replace 替换

3.PadLeft 方法和 PadRight 方法。 PadLeft 方法会对字符串进行右对齐排列,而 PadRight 方法会对字符串进行左对齐排列。

4.Concat方法。此方法会取走 String对象的列表,把它们串联在一起,然后返回结果字符串。

5.ToLower 方法和 ToUpper 方法还可以把字符串从小写转换成大写形式

6.Trim 方法和 TrimEnd 方法将会把空格或其他字符从字符串的任一端移除掉。

 

 

 

4.2.构造 StringBuilder

在 StringBuilder 类中有几种属性可以用来获取有关 StringBuilder 对象的信息。

Length 属性指定了当前实例中字符的数量,

 Capacity 属性则返回了实例的当前容量。

MaxCapacity 属性会返回对象当前实例中所允许的最大字符数量(尽管这个数量会随着对象添加更多的字符而自动增加)。

1.Append

通过使用 Append 方法可以在 StringBuilder 对象的末尾处添加字符

            StringBuilder stBuff = new StringBuilder();
String[] words = new string[] {"now ", "is ", "the ", "time ", "for ", "all ",
"good ", "men ", "to ", "come ", "to ", "the ","aid ", "of ", "their ", "party"};
for (int i = 0; i <= words.GetUpperBound(0); i++)
stBuff.Append(words[i]);
Console.WriteLine(stBuff);

 

2.AppendFormat

给 StringBuilder 对象添加格式字符串,使用AppendFormat

StringBuilder stBuff = new StringBuilder();
Console.WriteLine();
stBuff.AppendFormat("Your order is for {0} widgets.", 234);
stBuff.AppendFormat("\nWe have {0000} widgets left.", 12);
Console.WriteLine(stBuff);

 

3.Insert

此方法会取得三个参数。第一个参数说明了插入的开始位置。第二个参数则是要插入的字符串。而作为可选项的第三个参数则是一个整数,它用来说明打算在对象中插入字符串的次数。

StringBuilder stBuff = new StringBuilder();
Console.WriteLine();
stBuff.Insert(0, "Hello");
stBuff.Append("world");
stBuff.Insert(5, ", ");
Console.WriteLine(stBuff);
char[] chars = new char[] { 't', 'h', 'e', 'r', 'e' };
stBuff.Insert(5, " " + new string(chars));
Console.WriteLine(stBuff);

 

4.Remove

Remove 方法可以把字符从 StringBuilder 对象中移除掉

 

5.Replace

StringBuilder stBuff = new StringBuilder("HELLO WORLD");
string st = stBuff.ToString();
st = st.ToLower();
st = st.Replace(st.Substring(0, 1),st.Substring(0, 1).ToUpper());
stBuff.Replace(stBuff.ToString(), st);
Console.WriteLine(stBuff);

最新文章

  1. 基础学习day10--异常、包
  2. java 处理xml格式数据
  3. linux如何查看磁盘剩余空间
  4. bios作用
  5. hdu1042
  6. Markdown 基础
  7. Htmlunit使用
  8. unity 本地帮助文档 慢
  9. WPF基础篇之控件模板(ControlTemplate)
  10. Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)
  11. GIT入门笔记(16)- 分支创建和管理
  12. 软件定义网络(Software Defined Network,SDN)简介
  13. Mybaits入门使用
  14. Python学习【01】编程语言简介,Python安装及环境变量配置
  15. Linux中硬链接和软链接的区别
  16. 字节缓冲流 BufferedOutputStream BufferedInputStream
  17. core net 实现post 跟get
  18. day022 python (re模块和 模块)
  19. pta l2-14(列车调度)
  20. IE盒模型和W3C盒子模型的区别

热门文章

  1. 极客DIY:如何用Siri与树莓派“交互”
  2. Dex动态加载
  3. java中类名,方法,变量,包名等大小写命名规范
  4. 百度地图API 海量点 自定义添加信息
  5. Flask 模板语言
  6. 交叉编译alsa声卡驱动
  7. 巧用linux服务器的/dev/shm/,如果合理使用,可以避开磁盘IO不给力,提高网站访问速度。
  8. HTML文档中头部文件介绍
  9. Interleaving Positive and Negative Numbers
  10. Linq查询非泛型集合要指定Student类型(比如List)