Console.WriteLine("1) Split a string delimited by characters:\n");
Console.WriteLine("1a )The original string is \"{0}\".", s1);
Console.WriteLine("The delimiter character is '{0}'.\n",
charSeparators[0]);
Console.WriteLine("1b) Split a string delimited by characters and " +
"return all elements:");
result = s1.Split(charSeparators, StringSplitOptions.None);
Show(result);
Console.WriteLine("1c) Split a string delimited by characters and " +
"return all non-empty elements:");
result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
Console.WriteLine("1d) Split a string delimited by characters and " +
"return 2 elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.None);
Show(result);
Console.WriteLine("1e) Split a string delimited by characters and " +
"return 2 non-empty elements:");
result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
Console.WriteLine("2) Split a string delimited by another string:\n");
Console.WriteLine("2a) The original string is \"{0}\".", s2);
Console.WriteLine("The delimiter string is \"{0}\".\n", stringSeparators[0]);
Console.WriteLine("2b) Split a string delimited by another string and " +
"return all elements:");
result = s2.Split(stringSeparators, StringSplitOptions.None);
Show(result);
Console.WriteLine("2c) Split a string delimited by another string and " +
"return all non-empty elements:");
result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
Show(result);
Console.WriteLine("2d) Split a string delimited by another string and " +
"return 2 elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
Show(result);
Console.WriteLine("2e) Split a string delimited by another string and " +
"return 2 non-empty elements:");
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
Console.ReadKey();
}
// Display the array of separated strings.
public static void Show(string[] entries)
{
Console.WriteLine("The return value contains these {0} elements:", entries.Length);
foreach (string entry in entries)
{
Console.Write("<{0}>", entry);
}
Console.Write("\n\n");
}
}
三、Trimusing System;
public class TrimTest
{
/*
* Trim() 移除指定字符串的前导和后导空字符 字符串中间部分的空格并不会移除
* trim(char[]) 移除指定字符串中前导和后导的在指定参数中的字符
* 相应的TrimStart,TrimEnd分别移除前导,后导字符*
*
*/
public static void Main()
{
string[] temp = MakeArray();
Console.WriteLine("Concatenating the inital values in the array, we get the string:");
Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);
// trim whitespace from both ends of the elements
for (int i = 0; i < temp.Length; i++)
temp = temp.Trim();
Console.WriteLine("Concatenating the trimmed values in the array, we get the string:");
Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);
// reset the array
temp = MakeArray();
// trim the start of the elements. Passing null trims whitespace only
for (int i = 0; i < temp.Length; i++)
temp = temp.TrimStart(null);
Console.WriteLine("Concatenating the start-trimmed values in the array, we get the string:");
Console.WriteLine("'{0}'{1}", String.Concat(temp), Environment.NewLine);
// reset the array
temp = MakeArray();
// trim the end of the elements. Passing null trims whitespace only
for (int i = 0; i < temp.Length; i++)
temp = temp.TrimEnd(null);
Console.WriteLine("Concatenating the end-trimmed values in the array, we get the string:");
Console.WriteLine("'{0}'", String.Concat(temp));
}
private static string[] MakeArray()
{
string[] arr = { " please ", " tell ", " me ", " about ", " yourself " };
return arr;
}
}
四 、Pad
using System;
namespace Pad
{
/*
* 第二个参数指定了用于填充的unicode字符
* 其实第一个入参指定了执行后字符串的长度,若小于给定实例的长度则不填充,若大于,则PadLeft左侧填充(右对齐),PadRight右侧填充(左对齐。)
*/
class Program
{
static void Main(string[] args)
{
Console.WriteLine("a b c".PadLeft(6,'$'));
Console.WriteLine("a b c".PadRight(6,'$'));
Console.ReadKey();
}
}
}
其他的substring,indexof,lastindexof,insert,remove,repalce,format比较简单就不贴代码了。希望能帮到大家。