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);