C#基础之字符串常用方法

类型转化

将字符串的类型从字符字符类型转化为int类型,再由int类型转化为字符类型

Console.WriteLine("请输入一句话");
string text = Console .ReadLine ();
Console.WriteLine("这句话的字符数为{0}",text.Length )
char[]chs=text.ToCharArray();
chs[6]='纯';
chs[7]='洁';
text = new string(chs);
Console.WriteLine("改变字符后为{0}",text );
Console.ReadKey();

这里写图片描述

ToLower()

得到字符串的小写形式

Console.WriteLine("请输入字符");
string str = Console.ReadLine().ToLower ();
Console.WriteLine("该字符的小写形式为{0}",str);
Console.ReadKey();

这里写图片描述

ToUpper()

得到字符串的大写形式

Console.WriteLine("请输入字符");
string str = Console.ReadLine().ToUpper ();
Console.WriteLine("该字符的大写形式为{0}",str);
Console.ReadKey();

这里写图片描述

s1.Equals(s2, tringComparison.OrdinalIgnoreCase)

两个字符串进行比较不区分大小写的比较

Console.WriteLine("请输入第一门课程");
string str1 = Console.ReadLine ();
Console.WriteLine("请输入第二门课程");
string str2 = Console.ReadLine ();
bool result = str1.Equals(str2, StringComparison.OrdinalIgnoreCase);
if (result)
{
    Console.WriteLine("课程相同"+ str1);
}
else
{
    Console.WriteLine("课程不同{0}--------{1}", str1, str2);
}
Console.ReadKey();

这里写图片描述

string[] Split(char[] separator, StringSplitOptions options)

将字符串按照指定的char分割符分割为字符串数组,当options取RemoveEmptyEntries的时候移除结果中的空白字符串

string date = "2012----------11--------02";
char[] chs = new char[] { '-' };
string[] time = date.Split(chs, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("{0}年{1}月{2}日", time[0], time[1], time[2]);
Console.ReadKey();

这里写图片描述

string Replace(string oldValue, string newValue)

将字符串中的出现oldValue的地方替换为newValue。

Console.WriteLine("请输入傻子的姓名");
string str1 = Console.ReadLine();
Console.WriteLine("{0}是个傻子",str1 );
Console.WriteLine("请输入真正的傻子的姓名");
str1 = str1.Replace(str1, Console.ReadLine());
Console.WriteLine("你说的对,{0}才是个真正的傻子",str1);
Console.ReadKey();

这里写图片描述

bool Contains(string value)

判断字符串中是否含有子串value

bool StartsWith(string value)

判断字符串是否以子串value开始

bool EndsWith (string value)

判断字符串是否以子串value结束

Int IndexOf(string value,int startIndex)

#

取子串value在固定位置后第一次出现的位置

            Console.WriteLine("请输入一句话");
            string str1=Console.ReadLine();
            Console.WriteLine("请输入关键字");
            bool result1 = str1.Contains(Console.ReadLine());//判断输入的那句话中是否有输入的关键字
            if (result1)
            {
                Console.WriteLine("包含该关键字");
            }
            else
            {
                Console.WriteLine("不包含该关键字");
            }
            Console.WriteLine("请输入开头");
            string str2 = Console.ReadLine();
            bool result2 = str1.StartsWith(str2);//判断输入的话中是否以输入的str2为开头
            if (result2 )
            {
                Console.WriteLine("该句是以{0}开头的", str2);
            }
            else
            {
                Console.WriteLine("该句不是以{0}开头的", str2);
            }
            Console.WriteLine("请输入结尾");
            string str3 = Console.ReadLine();
            bool result3 = str1.EndsWith(str3);//判断输入的话中是否以str3为结尾
            if (result3)
            {
                Console.WriteLine("该句是以{0}结尾的", str3);
            }
            else
            {
                Console.WriteLine("该句不是以{0}结尾的", str3);
            }
            Console.WriteLine("请输入想要查询位置的字符");
            string str4 = Console.ReadLine();
            int index1 = str1.IndexOf(str4,1);//查询输入st4的字符的位置
            Console.WriteLine("您选择的字符{0}位置为{1}",str4 ,index1 );
            Console.ReadKey();

这里写图片描述

string Substring(int startIndex, int length)

取从位置startIndex开始长度为length的子字符串,如果子字符串的长度不足length则报错。

Console.WriteLine("请输入一句话");
string str1 = Console.ReadLine();
Console.WriteLine("请输入想要开始截取的位置");
string strstart = Console.ReadLine();
Console.WriteLine("请输入想要结束截取的位置");
string strend = Console.ReadLine();
Console.WriteLine("截取的字符为{0}", str1.Substring (str1.IndexOf (strstart ),str1.IndexOf (strend )-str1 .IndexOf (strstart )) );
Console.ReadKey ();

这里写图片描述
这里写图片描述

string Insert(int startIndex,string value)

向指定索引位置插入一个子字符串

string Remove(int startIndex,int count)

在指定位置开始删除指定数目的子字符串

Console.WriteLine("请输入一句话");
string str = Console.ReadLine();
Console.WriteLine("请输入插入的位置");
string strinsertnum=Console .ReadLine ();
int intinsert = str.IndexOf(strinsertnum);//确定插入的位置
Console.WriteLine("请输入插入的内容");
string strinsertstr = Console.ReadLine();//确定插入的内容
str = str.Insert(intinsert, strinsertstr);//向指定的位置插入指定的内容
Console.WriteLine("插入成功:{0}", str);
Console.WriteLine("请输入要删除的内容");
string strremove = Console.ReadLine ();
str = str.Remove(str.IndexOf(strremove), strremove.Length);//删除输入字符位置的输入字符长度
Console.WriteLine("删除成功:{0}",str );
Console.ReadKey();

这里写图片描述

猜你喜欢

转载自blog.csdn.net/xyf13920745534/article/details/80720019