八、字符串和正则表达式

一、字符串 System.String

  一些常用的方法

1、转移字符“/”

  字符串中可以包含转义符,如“/n”(新行)和“/t”(制表符)。如果希望包含反斜杠,则它前面必须还有另一个反斜杠,如“//”。

2、“@”符号 

@ 符号会告知字符串构造函数忽略转义符和分行符

因此,以下两个字符串是完全相同的:

string p1 = "My Documents//My Files//";

string p2 = @"http://www.cnblogs.com/xianspace/admin/file://my/ Documents/My Files/";

3、ToString()

.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

//outputs "2009-03-11 18:05:16.345"

//"MM":指定月份为2位字符串,不足2位则前方补"0";"M":为月份数值转换的字符串;

//"HH":表示24小时制的小时;"hh"表示12小时制的小时;

4、SubString()

格式:Substring(int startindex, int len)

示例:

string s4 = "Visual C# Express";

System.Console.WriteLine(s4.Substring(7, 2)); // outputs "C#" ,C为第八个字符

System.Console.WriteLine(s4.Replace("C#", "Basic")); // outputs "Visual Basic Express"

5、Replace()

格式:Replace(string oldValue, string newValue)

string s5 = "Visual C# Express";

System.Console.WriteLine(s5.Replace("C#","VB")); // outputs "Visual VB Express"

6、Split()

将字符串拆分为子字符串(如将句子拆分为各个单词)是一个常见的编程任务。Split() 方法使用分隔符(如空格字符)char 数组,并返回一个子字符串数组。您可以使用 foreach 访问此数组。

示例:

char[] delimit = new char[] { ' ' };

string s14 = "The cat sat on the mat.";

foreach (string substr in s14.Split(delimit)) //使用空格拆分

{

    System.Console.WriteLine(substr);

}

7、Trim()

Trim() 从当前 String 对象移除所有前导空白字符和尾部空白字符。

8、ToCharArray()

格式:ToCharArray(int startindex,int len)

9、利用索引访问字符串中的各个字符

格式:str[int index]

示例:逆序排列字符串

string s9 = "Printing backwards";

for (int i = 0; i < s9.Length; i++)

{

System.Console.Write(s9[s9.Length - i - 1]); // outputs "sdrawkcab gnitnirP"

}

10、更改大小写,ToUpper() 和 ToLower()

若要将字符串中的字母更改为大写或小写,可以使用 ToUpper() 或 ToLower()。如下所示:

string s10 = "Battle of Hastings, 1066";

System.Console.WriteLine(s10.ToUpper()); // outputs "BATTLE OF HASTINGS 1066"

System.Console.WriteLine(s10.ToLower()); // outputs "battle of hastings 1066"

11、比较Compare

 

12、CompareTo()

字符串对象也有一个 CompareTo() 方法,它根据某个字符串是否小于 (<) 或大于 (>) 另一个,返回一个整数值(小于0或大于等于0)。比较字符串时使用 Unicode 值,小写的值小于大写的值。示例:

13、字符串索引 IndexOf()。

若要在一个字符串中搜索另一个字符串,可以使用 IndexOf()。如果未找到搜索字符串,IndexOf() 返回 -1;否则,返回它出现的第一个位置的索引(从零开始)。

示例:

string s13 = "Battle of Hastings, 1066";

System.Console.WriteLine(s13.IndexOf("Hastings")); // outputs 10 ,注意从0开始数起

二、StringBuilder

StringBuilder 类创建了一个字符串缓冲区,用于在程序执行大量字符串操作时提供更好的性能。StringBuilder 字符串还允许您重新分配个别字符,这些字符是内置字符串数据类型所不支持的。例如,此代码在不创建新字符串的情况下更改了一个字符串的内容:

示例:

System.Text.StringBuilder sb = new System.Text.StringBuilder("Rat: the ideal pet");

sb[0] = 'C';

System.Console.WriteLine(sb.ToString()); // displays Cat: the ideal pet

System.Console.ReadLine();

在以下示例中,StringBuilder 对象用于从一组数值类型中创建字符串。

示例:

class TestStringBuilder

{

static void Main()

{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

// Create a string composed of numbers 0 - 9

for (int i = 0; i < 10; i++)

{

sb.Append(i.ToString());

}

System.Console.WriteLine(sb); // displays 0123456789

// Copy one character of the string (not possible with a System.String)

sb[0] = sb[9];

System.Console.WriteLine(sb); // displays 9123456789

}

}

 

三、正则表达式

StringBuilder 类创建了一个字符串缓冲区,用于在程序执行大量字符串操作时提供更好的性能。StringBuilder 字符串还允许您重新分配个别字符,这些字符是内置字符串数据类型所不支持的。例如,此代码在不创建新字符串的情况下更改了一个字符串的内容:

示例:

System.Text.StringBuilder sb = new System.Text.StringBuilder("Rat: the ideal pet");

sb[0] = 'C';

System.Console.WriteLine(sb.ToString()); // displays Cat: the ideal pet

System.Console.ReadLine();

在以下示例中,StringBuilder 对象用于从一组数值类型中创建字符串。

示例:

class TestStringBuilder

{

static void Main()

{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

// Create a string composed of numbers 0 - 9

for (int i = 0; i < 10; i++)

{

sb.Append(i.ToString());

}

System.Console.WriteLine(sb); // displays 0123456789

// Copy one character of the string (not possible with a System.String)

sb[0] = sb[9];

System.Console.WriteLine(sb); // displays 9123456789

}

}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值