[150816][C#]C#基础知识——字符串

字符串类广泛存在于面向对象的语言中,所以本篇文章重点介绍C#中字符串类型的特点与使用。

C#中字符串类包括System命名空间中的String类和System.Text命名空间中的StringBuilder类。

String类:

我们常用string关键字来创建一个字符串,它实际上是String类的一个别名,重载了+、=、+=运算符,可以很轻松地创建一个字符串。例如

            string s1 = "www";
            string s2 = s1 + "xiaotustudio.com";
            string s3 = "http://";
            s3 += s2;

C#还允许像C++一样使用数组下表来提取字符串中的字符,例如

            char c = s1[0];

C#中用\来表示转义字符,如果字符串中含有\本身,则用\\来表示,不过如果字符串中含有大量的\,比如windows的路径名,未免太过麻烦,我们则可以使用@来禁止转移

string s4 = @"C:\windows\123";
如果字符转中含有"则用""来转义表示
下面是一些常用的方法:
<table id="memberList2" class="members " style="DISPLAY: table-row"><tbody><tr data="public;declared;xnafw;portable_library;Windows_store_apps;"><td><a target=_blank class="mtps-internal-link" href="ms-xhelp:///?method=page&id=m%3asystem.string.equals(system.string)&vendor=Microsoft&topicVersion=110&topicLocale=ZH-CN">Equals(String)</a></td><td><span class="mt-sentence">确定此实例是否与另一个指定的 <span class="selflink">String</span> 对象具有相同的值。
</span></td></tr></tbody></table>用于判断字符串内容提供是否相等,等同于==,区别于java
<table id="memberList2" class="members " style="DISPLAY: table-row"><tbody><tr data="public;declared;xnafw;portable_library;Windows_store_apps;"><td><a target=_blank class="mtps-internal-link" href="ms-xhelp:///?method=page&id=m%3asystem.string.indexof(system.char%2csystem.int32)&vendor=Microsoft&topicVersion=110&topicLocale=ZH-CN">IndexOf(Char, Int32)</a></td><td><span class="mt-sentence">报告指定 Unicode 字符在此字符串中的第一个匹配项的从零开始的索引。</span> <span class="mt-sentence">该搜索从指定字符位置开始。</span></td></tr></tbody></table><table id="memberList2" class="members " style="DISPLAY: table-row"><tbody><tr data="public;declared;xnafw;portable_library;Windows_store_apps;"><td><a target=_blank class="mtps-internal-link" href="ms-xhelp:///?method=page&id=m%3asystem.string.lastindexof(system.char)&vendor=Microsoft&topicVersion=110&topicLocale=ZH-CN">LastIndexOf(Char)</a></td><td><span class="mt-sentence">报告指定 Unicode 字符在此实例中的最后一个匹配项的从零开始的索引的位置。</span></td></tr></tbody></table>用于字符检索
<table id="memberList2" class="members " style="DISPLAY: table-row"><tbody><tr data="public;declared;xnafw;portable_library;Windows_store_apps;"><td><a target=_blank class="mtps-internal-link" href="ms-xhelp:///?method=page&id=m%3asystem.string.replace(system.char%2csystem.char)&vendor=Microsoft&topicVersion=110&topicLocale=ZH-CN">Replace(Char, Char)</a></td><td><span class="mt-sentence">返回一个新字符串,其中此实例中出现的所有指定 Unicode 字符都替换为另一个指定的 Unicode 字符。</span></td></tr></tbody></table>用于简单加密
<table id="memberList2" class="members " style="DISPLAY: table-row"><tbody><tr data="public;declared;portable_library;Windows_store_apps;"><td><a target=_blank class="mtps-internal-link" href="ms-xhelp:///?method=page&id=m%3asystem.string.split(system.char%5b%5d%2csystem.int32)&vendor=Microsoft&topicVersion=110&topicLocale=ZH-CN">Split(<span id="ID1409"></span>Char<span id="ID1410">[]</span>, Int32)</a></td><td><span class="mt-sentence">返回的字符串数组包含此实例中的子字符串(由指定 Unicode 字符数组的元素分隔)。</span> <span class="mt-sentence">参数指定返回的子字符串的最大数量。</span></td></tr></tbody></table>用于解析具有指定分隔符的字符串,如网址或自定义格式协议
<table id="memberList2" class="members " style="DISPLAY: table-row"><tbody><tr data="public;declared;xnafw;portable_library;Windows_store_apps;"><td><a target=_blank class="mtps-internal-link" href="ms-xhelp:///?method=page&id=m%3asystem.string.substring(system.int32%2csystem.int32)&vendor=Microsoft&topicVersion=110&topicLocale=ZH-CN">Substring(Int32, Int32)</a></td><td><span class="mt-sentence">从此实例检索子字符串。</span> <span class="mt-sentence">子字符串从指定的字符位置开始且具有指定的长度。</span></td></tr></tbody></table>注意参数与java的不同
<table id="memberList2" class="members " style="DISPLAY: table-row"><tbody><tr data="public;declared;xnafw;portable_library;Windows_store_apps;"><td><a target=_blank class="mtps-internal-link" href="ms-xhelp:///?method=page&id=m%3asystem.string.tolower&vendor=Microsoft&topicVersion=110&topicLocale=ZH-CN">ToLower<span id="ID1551">()</span></a></td><td><span class="mt-sentence">返回此字符串转换为小写形式的副本。</span></td></tr></tbody></table><table id="memberList2" class="members " style="DISPLAY: table-row"><tbody><tr data="public;declared;xnafw;portable_library;Windows_store_apps;"><td><a target=_blank class="mtps-internal-link" href="ms-xhelp:///?method=page&id=m%3asystem.string.toupper&vendor=Microsoft&topicVersion=110&topicLocale=ZH-CN">ToUpper<span id="ID1612">()</span></a></td><td><span class="mt-sentence">返回此字符串转换为大写形式的副本。</span></td></tr></tbody></table>用于忽略大小写实现
<table id="memberList2" class="members " style="DISPLAY: table-row"><tbody><tr data="public;declared;xnafw;portable_library;Windows_store_apps;"><td><a target=_blank class="mtps-internal-link" href="ms-xhelp:///?method=page&id=m%3asystem.string.trim&vendor=Microsoft&topicVersion=110&topicLocale=ZH-CN">Trim<span id="ID1645">()</span></a></td><td><span class="mt-sentence">从当前 <span class="selflink">String</span> 对象移除所有前导空白字符和尾部空白字符。</span></td></tr></tbody></table>去除空白符,用于统一格式
StringBuilder类:
为什么要使用StringBuilder类?我们要先从String类的实现谈起,String类一旦生成实例,其内容是不可变的。我们在对字符串值进行修改操作时,实际上是新生成了一个对象,然后用原字符串变量来表示新值。所以说,如果对一个字符串进行频繁地修改,就会产生大量的垃圾字符串对象等待被回收,导致程序的效率越来越低,所以说我们有必要新建一个内容可变的字符串类型存储这些频繁更改的字符串,那就是StringBuilder类。
StringBuilder类是预先给字符串分配一个足够大的空间来存储字符串,我们可以隐式或者显式地声明字符串的最大长度。
但并不是StringBuilder一定比String的效率高,比如在字符串加密的时候,需要频繁修改字符串,则前者效率较高,如果只是简单操作,则后者更好而且更方便。
需要注意的是,StringBuilder的对象是不能赋值给String对象的,唯一可用的方法是使用ToString()方法来转换。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值