轻松学习C#的String类

 更新时间:2022年05月06日 14:30:49   作者:丿木呈广予口贝  
轻松学习C#的String类,小编也是第一次接触C#的String类,感兴趣的小伙伴们可以参考一下,大家一起学习

字符串是由零个或多个字符组成的有限序列,是几乎所有编程语言中可以实现的非常重要和有用的数据类型。在C#语言中,字符串是System.String类的一个引用类型,但与其他引用类型不同的是,C#将字符串视为一个基本类型,可以声明为一个常量,并可以直接赋值。由于C#中的字符串是由System,String类派生而来的引用对象,因此可以使用String类的方法来对字符串进行各种操作。下面通过几个例子来讲述String类的几个重要方法。

一、字符串的截取

字符串截取是通过Substring方法实现的,它有两种重载方法,格式分别为:

(1)字符串1.Substring(整数n);将字符串1前n个长度的字符串截取掉,保留后面的字符串
(2)字符串1.Substring(整数n,整数m);保留从字符串1第n个长度开始数m个长度的字符串

两种重载方法都返回一个新的字符串。

例一:实现对字符串“0123456789”的截取

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   string nums = "0123456789"; 
   string newnums1; 
   string newnums2; 
   newnums1 = nums.Substring(5);//截取从索引5开始后的字符 
   newnums2 = nums.Substring(3,5);//截取从索引3开始数5个字符 
   Console.WriteLine(newnums1); 
   Console.WriteLine(newnums2); 
   Console.ReadLine(); 
  } 
 } 
}

输出的结果为:56789
                        34567

注意:字符串的索引是从0开始的,在使用Substring方法的第二种重载时,整数n和整数m的和不要大于要截取的字符串的长度,否则会产生越出索引异常。

二、字符串的分割

字符串的分割是通过Split方法实现的。常用的一种格式为:

字符串1.Split(字符串或字符数组)
通过Split方法分割字符串后将生成多个字符串,所以经过Split方法分割的返回值是一个字符串数组。

例二:实现对字符串“abcefgaabsbdeesdabc”的分割

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   string str="abcefgaabsbdeesdabc"; 
   Console.WriteLine("原字符串为{0}",str); 
   Console.WriteLine("通过单个字符e分割后如下:"); 
   string[] singleSplit = str.Split('e');//进行单个字符分割的方法 
   foreach (string outstr in singleSplit) 
   { 
    Console.WriteLine(outstr); 
   } 
   Console.WriteLine("通过多个字符e,b,s分割后如下:"); 
   string[] multiSplit = str.Split(new char[] {'e','b','s'});//进行多个字符分割的方法 
   foreach (string outstr in multiSplit) 
   { 
    Console.WriteLine(outstr); 
   } 
   Console.ReadLine(); 
  } 
 } 
}

输出的结果为:

三、字符串的合并

字符串的合并通过“+”,Concat方法和Join方法来实现的。

(1)用“+”符号来连接两个字符串后形成一个新的字符串,格式为:字符串1+字符串2=字符串3。
(2)用Concat方法连接字符串的格式为:string.Concat(字符串1,字符串2,...,字符串n)。
(3)用Join方法是将字符串数据合并为一个新的字符串,格式为:string.Join(合并后的分隔符,字符串数组)。

例三,实现对字符串str1和str2的合并

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   string str1 = "abc"; 
   string str2 = "ghj"; 
   string[] array = {"123","456","789"}; 
   string str3 = str1 + str2; 
   string str4 = string.Concat(str1,str2); 
   string str5 = string.Join("|",array);//将数组中元素合并为一个新的字符串 
   Console.WriteLine(str3); 
   Console.WriteLine(str4); 
   Console.WriteLine(str5); 
   Console.ReadLine(); 
  } 
 } 
}

输出的结果为:abcghj
                       abcghj
                       123|456|789

四、字符串的替换

字符串的替换是通过Replace方法实现的,格式为:字符串.Replace(要替换的原字符串,替换后的字符串);

例四、实现对字符串str的替换

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   string str = "abcadfaslfj"; 
   string replacestr = str.Replace("a","|");//用“|”替换“a” 
   Console.WriteLine(replacestr); 
   Console.ReadLine(); 
  } 
 } 
}

输出的结果为:|bc|df|slfj

五、字符串的插入与填充

字符串的插入是通过Insert方法实现的,其格式为:字符串.Insert(插入位置,插入字串)
字符串的填充是通过PadRight方法和PadLeft方法实现的。
PadRight方法是在字符串的结尾通过添加指定的重复字符填充字符串,格式为:字符串.PadRight(总长度)(以空格填充)和字符串.PadRight(总长度,要填充的字符)。
PadLeft方法是在字符串的开头通过添加指定的重复字符填充字符串,格式为:字符串.PadLeft(总长度)(以空格填充)和字符串.PadLeft(总长度,要填充的字符)。

例五、实现对字符串str的插入与填充

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   string str = "abcdefg"; 
   string insertstr; 
   string padrightstr; 
   string padleftstr; 
   insertstr = str.Insert(5,"12345"); 
   padrightstr = str.PadRight(10,'v'); 
   padleftstr = str.PadLeft(10,'w'); 
   Console.WriteLine(insertstr); 
   Console.WriteLine(padrightstr); 
   Console.WriteLine(padleftstr); 
   Console.ReadLine(); 
  } 
 } 
}

输出的结果为:

六,字符串的删除

字符串的删除是通过Remove方法实现的,格式为:
         (1)字符串.Remove(开始位置)
         (2)字符串.Remove(开始位置,移除数)
其中,开始位置是指字符串的索引,是一个整数,且小于字符串的长度。第一种格式,是将字符串开始位置后的所有子子符删除,而第二种是将从开始位置开始数到移除数位置的字符删除。

例六,实现字符串str的删除

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  string str = "0123456789"; 
  string delstr1; 
  string delstr2; 
  delstr1 = str.Remove(6);//删除字符串索引为6后面的字符 
  delstr2 = str.Remove(5,5);//删除字符串索引自5开始后数5个长度的字符 
  Console.WriteLine(delstr1); 
  Console.WriteLine(delstr2); 
  Console.ReadLine(); 
 } 
 } 
}

输出的结果为:012345
                        01234

七、字符串的复制

字符串的复制是通过Copy方法和CopyTo方法实现的。若想把一个字符串复制到另一个字符数组中,可以使用String的静态方法Copy来实现。其格式为:string.Copy(要复制的字符串)。
CopyTo方法可以实现Copy同样的功能,但是功能更为丰富,可以复制原字符串的一部分到一个字符数组中,其格式为:CopyTo(要复制的字符起始位置,目标字符数组,目标数组中的开始存放位置,要复制的字符个数)。

例七,实现字符串str的复制

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  string str = "This is a string"; 
  string copystr; 
  copystr = string.Copy(str); 
  char[] newchar=new char[20]; 
  str.CopyTo(5,newchar,0,11); 
  Console.WriteLine(copystr); 
  Console.WriteLine(newchar); 
  Console.ReadLine(); 
 } 
 } 
}

输出的结果为:This is a string
                        is a string

八、字符串的大小写转换

字符串大小写转换是通过String类的ToLower方法和ToUpper方法实现的,ToLower方法是将字符串转换为小写形式,ToUpper是将字符串转换为大写形式。
例八,实现字符串str的大小写转换

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  string str = "This is a string"; 
  string lowerstr; 
  string upperstr; 
  lowerstr = str.ToLower(); 
  upperstr = str.ToUpper(); 
  Console.WriteLine("小写形式:{0}",lowerstr); 
  Console.WriteLine("大写形式:{0}",upperstr); 
  Console.ReadLine(); 
 } 
 } 
}

输出的结果为:this is a string
                       THIS IS A STRING

九、字符串的查找

字符串的查找是通过IndexOf方法和LastIndexOf方法实现的。其格式为:
字符串.IndexOf(要查找的字符或字符串)
字符串.LastIndexOf(要查找的字符或字符串)
其中,IndexOf方法是返回要查找的字符或字符串第一次在所要查找的字符串出现的位置,LastIndexOf方法是返回要查找的字符或字符串最后一次在所要查找的字符串中出现的位置。IndexOf方法和LastIndexOf方法都返回一个整数,如果在所要查找的字符串内不包含要查找的字符或字符串则返回一个负数。
例九,实现字符串str的查找

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  string str = "This is a string"; 
  int rh1 = str.IndexOf("i"); 
  int rh2 = str.LastIndexOf("i"); 
  if (rh1>=0) 
  { 
  Console.WriteLine("字符i在字符串str第一次出现的位置是:{0}",rh1); 
  Console.WriteLine("字符i在字符串str最后一次出现的位置是:{0}", rh2); 
  } 
  else 
  { 
  Console.WriteLine("字符i在字符串str未出现"); 
  } 
  Console.ReadLine(); 
 } 
 } 
}

输出的结果为:字符i在字符串str第一次出现的位置是:2
                       字符i在字符串str最后一次出现的位置是:13

十、字符串的追加

在使用System.String类中的方法时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间。在需要对字符串执行重复修改的情况下,与创建新的String对象相关的系统开销就可能非常高。为了解决这个问题,C#提供了一个类StringBuilder。
使用StringBuilder类时,首先要引入System.Text命名空间,然后通过new关键字对其进行初始化。StringBuilder类的方法使用和String类的方法使用是一样的。

例十、实现对字符串str的追加

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  StringBuilder str =new StringBuilder( "Hellow World!"); 
  Console.WriteLine("---Append---"); 
  str.Append("What a beautiful day"); 
  Console.WriteLine("追加后的字符串为:{0}",str); 
  Console.ReadLine(); 
 } 
 } 
}

输出的结果为:---Append---
                       追加后的字符串为:Hellow World! What a beautiful day

补充:转义字符
转义字符具有特定的含义,不同于字符原有的意义的字符。在C#语言中,转义字符是指“\”,主要用来表示那些用一般字符不方便表示的控制代码。
对于转义字符的输出C#语言有着特殊的格式:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 字符串 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  Console.WriteLine(@"C:\Windows\system32");//第一种输出格式就是在前面加@ 
  Console.WriteLine("C:\\Windows\\system32");//第二种输出格式就是将"\"改成"\\" 
  Console.ReadLine(); 
 } 
 } 
}

输出的结果为:

以上就是C#的String类全部学习例题,很详细的学习教程,希望对大家的学习有所帮助。

相关文章

  • WPF自定义实现IP地址输入控件

    WPF自定义实现IP地址输入控件

    这篇文章主要给大家介绍了关于WPF自定义实现IP地址输入控件的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WPF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • .NET的深复制方法(以C#语言为例)

    .NET的深复制方法(以C#语言为例)

    深复制需要将对象实例中字段引用的对象也进行复制,在平时的编程工作中经常要用到这种复制方式,下面以c#为例来演示一下方法。
    2016-10-10
  • LRU缓存替换策略及C#实现方法分享

    LRU缓存替换策略及C#实现方法分享

    LRU(Least Recently Used)缓存替换策略是一种常用的缓存管理策略,它根据数据最近被访问的时间来决定哪些数据应该被保留在缓存中。本文将介绍LRU缓存替换策略的原理和C#实现方法。
    2023-04-04
  • C#控件编程之文本框(TextBox)的使用

    C#控件编程之文本框(TextBox)的使用

    这篇文章主要介绍了C#控件编程之文本框(TextBox)的使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • C#中在WebClient中使用post发送数据实现方法

    C#中在WebClient中使用post发送数据实现方法

    这篇文章主要介绍了C#中在WebClient中使用post发送数据实现方法,需要的朋友可以参考下
    2014-08-08
  • C#组合模式实例详解

    C#组合模式实例详解

    这篇文章主要介绍了C#组合模式,实例分析了C#实现组合模式的原理与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • 深入学习C#网络编程之HTTP应用编程(下)

    深入学习C#网络编程之HTTP应用编程(下)

    这篇文章主要介绍了深入学习C#网络编程之HTTP应用编程的相关知识,文中讲解的非常详细,帮助大家更好的学习c#网络编程,感兴趣的朋友可以了解下
    2020-06-06
  • c# 编写一个轻量级的异步写日志的实用工具类(LogAsyncWriter)

    c# 编写一个轻量级的异步写日志的实用工具类(LogAsyncWriter)

    这篇文章主要介绍了c# 如何编写一个轻量级的异步写日志的实用工具类(LogAsyncWriter),帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C#基础教程之类class与结构struct的区别

    C#基础教程之类class与结构struct的区别

    struct是值类型,创建一个struct类型的实例被分配在栈上,class是引用类型,创建一个class类型实例被分配在托管堆上,下面这篇文章主要给大家介绍了关于C#基础教程之类class与结构struct区别的相关资料,需要的朋友可以参考下
    2022-11-11
  • C#中SQL参数传入空值报错解决方案

    C#中SQL参数传入空值报错解决方案

    这篇文章主要介绍了C#中SQL参数传入空值报错解决方案,需要的朋友可以参考下
    2017-06-06

最新评论