String类的增 删 改 查

来源:互联网 发布:ubuntu命令行更新软件 编辑:程序博客网 时间:2024/06/03 15:06

构造方法

new string(char[] value)//将字符的数组转化成字符串

示例:控制台中加入以下代码

char[] chs = {'b','o','o','k' };//声明一个数组string str = new string(chs);//将字符数组每个字符连起来组成字符串并赋值给strConsole.WriteLine(str);//打印到控制台Console.ReadKey();

控制台显示结果:
这里写图片描述

常用方法


1、string Insert(int startindex, string value);//从指定位置插入字符串

示例:控制台下加入以下代码

string str = "book";//声明一个字符串str = str.Insert(2, "12");//使用插入方法Console.WriteLine(str);//打印到控制台Console.ReadKey();

控制台显示结果:
这里写图片描述
2、string string.Join(string splitStr, string[] stringArray);//将stringArray数组,用splitStr隔开,并串起来

示例:控制台下加入以下代码

string[] strArray = new string[] { "C#", "编程", "很有趣" };//声明一个字符串数组            string str = string.Join("-", strArray);//将strArray字符串数组中的每个字符串用“-”连接起来,并赋值给str            Console.WriteLine(str);//打印到控制台            Console.ReadKey();

控制台显示结果:
这里写图片描述
3、string Replace(string oldValue, string newValue);//返回一个新字符串,字符串中出现的所有oldValue都替换为另一个newValue

示例:控制台下加入以下代码

string s = "aaa";//声明一个字符串Console.WriteLine("开始的字符串: '{0}'", s);s = s.Replace("a", "b").Replace("b", "c").Replace("c", "d");//先用b替换a,再用c替换b,再用d替换cConsole.WriteLine("最终的字符串: '{0}'", s);Console.ReadKey();

控制台显示结果:
这里写图片描述
4、 string string.Concat(string[] strs)//返回一个字符串,字符串由strs数组中的字符串连接而成

string [] s = { "你好,", "欢迎", "来到", "C#" };//声明一个字符串Console.WriteLine(string.Concat(s));//将字符串连接起来并输出到控制台Console.ReadKey();

控制台打印结果如下:
这里写图片描述
5、string PadLeft(int totalWidth, char paddingChar); 在左侧填充所需任意数量的 paddingChar 字符,使长度达到 totalWidth; 但是如果totalWidth≤字符串长度,则返回原有的字符串

string str = "four";char pad = '.';Console.WriteLine(str.PadLeft(15, pad));//Console.WriteLine(str.PadLeft(2, pad));Console.ReadKey();

控制台显示结果:
这里写图片描述


删:

1、string Remove(int startIndex, int count)//从字符串starIndex开始删除count数的字符数,最后返回一剩余的字符串

string str = "012345";str = str.Remove(3, 2);//从第3个字符开始(*从0开始数*),删除2个字符,并将删除后的字符串赋值给strConsole.WriteLine(str);//将删除后的字符串打印到控制台Console.ReadKey();

控制台显示结果:
这里写图片描述
2、string Substring(int startIndex, int count);//从指定位置开始截取一定长度的字符串,并返回

string str = "012345";str = str.Substring(3, 2);//从第3个位置开始截取2个字符长度并返回给strConsole.WriteLine(str);//将截取后的字符串打印到控制台Console.ReadKey();

控制台显示结果:
这里写图片描述
3、string Trim(char[] chs);//从当前字符串两端移除chs数组中指定的一组字符。

char[] charsToTrim = { '*', ' ', '\'' };string str= "*** Much Ado About Nothing* **";string result = str.Trim(charsToTrim);//从字符串中移除空间、 星号 (*) 和撇号 (') 字符Console.WriteLine(result);Console.ReadKey();

控制台显示结果:
这里写图片描述


改:

1、char[] ToCharArray();//将字符串转换成字符数组

string str = "AaBbCcDd";char[] chars = str.ToCharArray();//将字符串转换成数组,并赋值给chars变量Console.WriteLine("初始的字符串为:{0}",str);Console.WriteLine("转换后的字符为:");for (int i = 0; i < str.Length; i++)//循环打印出每个数组里的字符{   Console.WriteLine(" {0}:{1}",i,chars[i]);}   Console.ReadKey();

控制台显示结果:
这里写图片描述
2、ToUpper();//将字符串转换成大写,并返回字符串

string str = "abcdefg";//初始字符串string strUpper = str.ToUpper();//大写后的字符串Console.WriteLine("初始字符串为:{0}",str);//初始字符串打印到控制台Console.WriteLine("更改后字符串为:{0}",strUpper);//更改后字符串打印到控制台Console.ReadKey();

控制台显示结果:
这里写图片描述
3、ToLower();//将字符串转换成小写,并返回字符串

string str = "ABCDEFG";//初始字符串string strUpper = str.ToLower();//将字符串转化成小写Console.WriteLine("初始字符串为:{0}",str);//初始字符串打印到控制台Console.WriteLine("更改后字符串为:{0}",strUpper);//转化成小写后的字符串打印到控制台Console.ReadKey();

控制台显示结果:
这里写图片描述


查:

1、bool Contains(string);//判断字符串中是否有指定的字符串,如果有则返回true

string s1 = "abcdef";string s2 = "b";Console.WriteLine(s1.Contains(s2));//返回TrueConsole.ReadKey();

控制台显示结果:
这里写图片描述

2、int IndexOf(char value, int startIndex);//从指定位置startIndex开始搜索value字符,如果搜索到了,则返回value在字符串中的位置(从字符串第一个位置0开始计算),如果搜索不到,则返回-1;

例如,从一句英文中寻找t分别所在的位置

//声明一个字符串变量,目的要从字符串中寻找t的位置string srt = "01234567890123456789012345678901234567890123456789012";//用来看对应t的位置string str = "Now is the time for the young man to change the world";int at = 0;int startIndex = 0;Console.Write("t所在的位置分别为");while ((startIndex < str.Length) && (at > -1))//如果没有找到t或者搜索的起始位置超过字符串的长度,则退出循环{   at = str.IndexOf('t', startIndex);//返回t在字符串中的位置   if (at == -1)//如果没有找到t,则退出循环   {      break;   }   startIndex = at + 1;   Console.Write("{0},", at);//将寻找到t的位置并打印到控制台}Console.ReadKey();

控制台显示结果:
这里写图片描述

3、int LastIndexOf(char value, int starIndex);//从字符串右边开始搜索指定的字符value,如果搜索到了,则返回value在字符串中的位置(从字符串第一个位置0开始计算),否则返回-1

string srt = "01234567890123456789012345678901234567890123456789012";string str = "Now is the time for the young man to change the world";       int at = 0;       int startIndex = str.Length - 1;//起始位置为字符串尾部开始搜索       Console.Write("t所在的位置分别为");while (at > -1)       {       at = str.LastIndexOf('t', startIndex);       if (at > -1)       {         startIndex = at - 1;         Console.Write("{0},", at);       }}Console.ReadKey();

这里写图片描述

4、int IndexOfAny(char[] anyof, int startIndex);//从字符串指定位置开始搜索,如果某个字符是anyof字符数组中的字符,则返回该字符的位置(从字符串中第一个0开始计算)

string br2 = "0123456789012345678901234567890123456789012345678901234567890123456";string str = "Now is the time for all good men to come to the aid of their party.";//从字符串中搜索i和sint start;//定义一个变量,搜索的起点位置int at;//用来接收搜索到i或者s在字符串中的位置at = 0;string target = "is";char[] anyOf = target.ToCharArray();//将is拆分成字符数组start = 0;Console.Write("'{0}'拆分后的'{1}'或者'{2}'在字符串中出现的位置: ", target, anyOf[0], anyOf[1]);while (at > -1){    at = str.IndexOfAny(anyOf, start);//在字符串中搜索i和s的位置    if (at > -1)    {       Console.Write("{0} ", at);//将搜索到的位置打印到控制台    }    start = at + 1;}Console.ReadKey();

控制台显示结果:
这里写图片描述

5、int LastIndexOfAny(char[] anyof, int statIndex);//从字符串右边开始搜索字符数组anyof中的字符,如果搜索到了,则字符在字符串中的位置(从字符串第一个位置0开始计算),否则返回-1

string srt = "0123456789012345678901234567890123456789012345678901234567890123456789";string str = "Now is the time for all good men to come to the aid of their party.";int startIndex = str.Length - 1;//声明初始搜索位置int at = 0;string target = "is";char[] anyof = target.ToCharArray();Console.Write("is拆分后的{0}或者{1}位置在:", anyof[0], anyof[1]);while (at > -1){    at = str.LastIndexOfAny(anyof, startIndex);//返回搜索到的位置    if (at == -1)//如果搜索不到,则退出循环    {        break;    }    Console.Write("{0} ", at);//打印出搜索到的位置    startIndex = at - 1;}Console.ReadKey();

控制台显示结果:
这里写图片描述

6、bool StartsWith(string str);//判断字符串str是否与给定的字符串开头的一样,如果一样,则返回True

//判断字符串开头是否是“b”string[] strSource = { "book", "apple", "banana", "building" };foreach (var str in strSource){   bool startStr= str.StartsWith("b");//判断字符串开始是否为"b",如果是,则返回True   Console.WriteLine("{0}:{1}",str,startStr);}Console.ReadKey();

控制台显示结果:
这里写图片描述