天天看点

常用字符串处理函数

方法名

描述

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.compare(vs.80).aspx">compare</a>

比较两个指定的 string 对象。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.compareordinal(vs.80).aspx">compareordinal</a>

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.compareto(vs.80).aspx">compareto</a>

将此实例与指定的对象或 string 进行比较,并返回二者相对值的指示。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.concat(vs.80).aspx">concat</a>

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/dy85x1sa(vs.80).aspx">contains</a>

返回一个值,该值指示指定的 string 对象是否出现在此字符串中。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.copy(vs.80).aspx">copy</a>

创建一个与指定的 string 具有相同值的 string 的新实例。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.copyto(vs.80).aspx">copyto</a>

将指定数目的字符从此实例中的指定位置复制到 unicode 字符数组中的指定位置。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.endswith(vs.80).aspx">endswith</a>

确定 string 的实例的末尾是否与指定的字符串匹配。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.getenumerator(vs.80).aspx">getenumerator</a>

检索一个可以循环访问此字符串中的每个字符的对象。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.indexof(vs.80).aspx">indexof</a>

报告 string 或一个或多个字符在此字符串中的第一个匹配项的索引。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.indexofany(vs.80).aspx">indexofany</a>

报告指定 unicode 字符数组中的任意字符在此实例中第一个匹配项的索引。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.insert(vs.80).aspx">insert</a>

在此实例中的指定索引位置插入一个指定的 string 实例。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.intern(vs.80).aspx">intern</a>

检索系统对指定 string 的引用。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.isinterned(vs.80).aspx">isinterned</a>

检索对指定 string 的引用。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.join(vs.80).aspx">join</a>

在指定 string 数组的每个元素之间串联指定的分隔符 string,从而产生单个串联的字符串。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.lastindexof(vs.80).aspx">lastindexof</a>

报告指定的 unicode 字符或 string 在此实例中的最后一个匹配项的索引位置。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.lastindexofany(vs.80).aspx">lastindexofany</a>

报告在 unicode 数组中指定的一个或多个字符在此实例中的最后一个匹配项的索引位置。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.normalize(vs.80).aspx">normalize</a>

返回一个新字符串,其二进制表示形式符合特定的 unicode 范式。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.padleft(vs.80).aspx">padleft</a>

右对齐此实例中的字符,在左边用空格或指定的 unicode 字符填充以达到指定的总长度。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.padright(vs.80).aspx">padright</a>

左对齐此字符串中的字符,在右边用空格或指定的 unicode 字符填充以达到指定的总长度。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.remove(vs.80).aspx">remove</a>

从此实例中删除指定个数的字符。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.replace(vs.80).aspx">replace</a>

将此实例中的指定 unicode 字符或 string 的所有匹配项替换为其他指定的 unicode 字符或string。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.split(vs.80).aspx">split</a>

返回包含此实例中的子字符串(由指定 char 或 string 数组的元素分隔)的 string 数组。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.substring(vs.80).aspx">substring</a>

从此实例检索子字符串。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.startswith(vs.80).aspx">startswith</a>

确定 string 实例的开头是否与指定的字符串匹配。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.tochararray(vs.80).aspx">tochararray</a>

将此实例中的字符复制到 unicode 字符数组。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.tolower(vs.80).aspx">tolower</a>

返回此 string 对象的转换为小写形式的副本,返回时使用固定区域性的大小写规则。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.tolowerinvariant(vs.80).aspx">tolowerinvariant</a>

将此实例的值转换为 string。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.toupper(vs.80).aspx">toupper</a>

返回此 string 转换为大写形式的副本。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.toupperinvariant(vs.80).aspx">toupperinvariant</a>

返回此 string 对象的转换为大写形式的副本,返回时使用固定区域性的大小写规则。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.trim(vs.80).aspx">trim</a>

从此实例的开始位置和末尾移除一组指定字符的所有匹配项。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.trimend(vs.80).aspx">trimend</a>

从此实例的结尾移除数组中指定的一组字符的所有匹配项。

<a target="_blank" href="http://msdn.microsoft.com/zh-cn/library/system.string.trimstart(vs.80).aspx">trimstart</a>

从此实例的开始位置移除数组中指定的一组字符的所有匹配项。

以下进行部分常用函数举例

1)compare函数:比较两个字符串的大小(前大为1,相同伟0,后大为-1)

程序运行结果:-1

2)compareordinal函数:逐个比较两个字符串的各个字符,发现不同,停止比较,并返回两者的ascii码之差(如果相同则为0)

程序运行结果:-32

3)compareto函数:实例函数,比较两个字符串的大小(前大为1,相同伟0,后大为-1)

程序运行结果:1

4)endswith函数:实例函数,确定一个实例的末尾与指定的字符串是否匹配

程序运行结果:false

5)indexof函数:实例函数,从实例的指定位置查找实例中第一个与指定字符串匹配的字符串,如果找到则返回位置,找不到返回-1

程序运行结果:2

6)insert函数:实例函数,在实例的指定位置开始插入指定的字符串,返回最终的字符串

程序运行结果:once upon a time

7)join函数,在字符串数组的每个元素之间插入指定的分隔符

程序运行结果:hello and welcome to my world!

8)lastindexof函数:实例函数,从实例的指定位置查找实例中最后一个与指定字符串匹配的字符串,如果找到则返回位置,找不到返回-1

程序运行结果:9

9)padleft函数:实例函数,将实例按指定长度填充满,左边不足部分用指定字符替代

程序运行结果:--------hello world!

此程序使用padleft方法来创建一个总长度为20个空间的新字符串。

10)remove函数:实例函数,将指定字符串实例的指定位置删除指定数目的字符

string mystring = "hello beautifulworld!";

console.writeline(mystring.remove(5,10));

程序运行结果:hello world!

从字符串的从零开始的索引的第5个位置开始,从该字符串中移除10个字符。

11)startswith函数:实例函数,确定一个实例的开头与指定的字符串是否匹配

程序运行结果:true

12)substring函数:实例函数,从字符串中的指定位置提取指定数目的子字符串

程序运行结果:world

13)tolower函数:实例函数,字符串全部变小写

14)trimstart函数:实例函数

程序运行结果:world!

15)trimend函数:实例函数

程序运行结果:he

在此例子中,‘r’字符和‘w’字符的位置反转,对于运行结果没有影响。

程序运行结果:hello,

在此例子中,单词hello后尾随一个逗号,而由于在要移除的字符的数组中没有指定逗号,因此移除在逗号处结束。

16)indexofany函数和lastindexofany函数:实例函数

程序运行结果:0

程序运行结果:3

17)split函数:实例函数

程序运行结果:

坚持

可以创造奇迹

不幸的是

很少有人

能够长时间地坚持下去

直至

奇迹发生

继续阅读