C#---第十课:字符串String的判断、截取、分割、拼接、清洗、索引获取等操作

1.字符串的判断

(1)判断两个字符串是否相等(不区分大小写)—Equals

string say = "HELLOWORLD";
string said = "helloworld";


bool result = say.Equals(said, StringComparison.OrdinalIgnoreCase);
Console.WriteLine(result);
Console.ReadKey();

// True



(2)判断字符串是否包含某个字符串—Contains & 替换字符串—Replace

string str = "hello world alien";

if (str.Contains("alien"))
{


    str = str.Replace("alien", "Alien");

}

Console.WriteLine(str);
Console.ReadKey();  


// hello world Alien




(3)判断字符串是否为空或null—string.IsNullOrEmpty


string str = "alien";

if (string.IsNullOrEmpty(str))
{

    Console.WriteLine("空 或 null");

}
else
{

    Console.WriteLine("非空或null");

}

// 非空或null



(4)判断字符串是否以某个字符串开头、结尾—StartsWith & EndsWith



string names = "helloworldalien";

bool result_1 = names.StartsWith("hello");
Console.WriteLine(result_1);

bool result_2 = names.EndsWith("alien");
Console.WriteLine(result_2);
 
Console.ReadKey();


// True
// True



(5)计算某字符/字符串在某个大字符串中的数量—Regex.Matches

using System.Text.RegularExpressions;


string Names = "alien,alien,hello";
string Name = "alien";
var num = Regex.Matches(Names, Name).Count;


Console.WriteLine(num);
Console.ReadKey();

// 2




2.字符串转为大写或小写-----ToUpper & ToLower


string say = "hellworld";

say.ToUpper();
Console.WriteLine(say);

say = say.ToUpper();
Console.WriteLine(say);
Console.ReadKey();


// hellworld
// HELLWORLD


string say = "HELLOWORLD";

say.ToLower();
Console.WriteLine(say);

say = say.ToLower();
Console.WriteLine(say);
Console.ReadKey();


// HELLOWORLD
// helloworld



3. 字符串的截取

(1)截取字符串—Substring(start_index, num)


string str = "hello world alien";

str = str.Substring(1, 9);		// 从索引1一直到索引9,[1, 9] , 第一个索引是0

Console.WriteLine(str);
Console.ReadKey();  

// ello worl



(2)截取字符串—Substring(start_index)


string str = "hello world alien";

str = str.Substring(2);		// 从索引2到最后, 第一个索引是0

Console.WriteLine(str);
Console.ReadKey();  

// llo world alien



4.字符串的分割—Split

(1)根据单个字符分割


namespace Helloworld
{
    internal class common
    {
        public static void Main(string[] args)
        {
            string fieldList = "abc;de;ddd;fff";
            string[] sArray = fieldList.Split(';');
            foreach (string i in sArray)
            {
                Console.WriteLine($"output info------:{i}");
            }
        }
    }
}


//output info------:abc
//output info------:de
//output info------:ddd
//output info------:fff



(2)根据多个字符分割—数组


string say = "a b   c  _  , f, , , _,_m ";

char[] chs = {' ', '_', ',' };

// StringSplitOptions.RemoveEmptyEntries 将空字符移除
string[] result = say.Split(chs, StringSplitOptions.RemoveEmptyEntries);

foreach(string s in result)
{
    Console.WriteLine(s);
}
Console.ReadKey();

//a
//b
//c
//f
//m



(3)根据字符串分割


namespace Helloworld
{
    internal class common
    {
        public static void Main(string[] args)
        {
            string str = "aaajsbbbjsccc";
            string[] sArray = str.Split("js");
            foreach (string i in sArray)
            {
                Console.WriteLine($"output info------:{i}");
            }
        }
    }
}


//output info------:aaa
//output info------:bbb
//output info------:ccc



5.字符串的拼接

(1)使用加号+


namespace Helloworld
{
    internal class common
    {
        public static void Main(string[] args)
        {
            string str1 = "abc";
            string str2 = "def";
            string str = str1 + str2;
            Console.WriteLine("output info------:{0}", str);
        }
    }
}

//output info------:abcdef




(2)使用加号$


namespace Helloworld
{
    internal class common
    {
        public static void Main(string[] args)
        {
            string str1 = "abc";
            string str2 = "def";
            string str = str1 + str2;
            Console.WriteLine($"output info------:{str}");

        }
    }
}


//output info------:abcdef

注意:
1.使用$符号,双引号里面的引用的变量,都必须使用{params},params是变量名,不能与{index}混合使用

2.Console.WriteLine($“output info------:{str}------{0}”, str); 这种方式是错的!!!



(3)将数组中的字符串拼接—join


string[] names = { "hello", "wolrd", "alien" };

string strNew = string.Join("_", names);
Console.WriteLine(strNew);
Console.ReadKey();


// hello_wolrd_alien



(4)字符串的拼接—string.format


namespace Helloworld
{
    internal class common
    {
        public static void Main(string[] args)
        {
            string str1 = "abc";
            string str2 = "_";
            string str3 = "def";
            string str = String.Format("{0}{1}{2}", str1, str2, str3);
            Console.WriteLine($"output info------:{str}");

        }
    }
}


//output info------:abc_def



(5)字符串的拼接—StringBuilder


using System.Text;

namespace Helloworld
{
    internal class common
    {
        public static void Main(string[] args)
        {

            StringBuilder sb = new StringBuilder("hello ");
            sb.Append("world");                     // 将string添加到StringBuilder的字符串中
            Console.WriteLine(sb.ToString());
            sb.Append(123);                         // 可以将int类型添加到StringBuilder的字符串中
            Console.WriteLine(sb.ToString());

        }
    }
}


// hello world
// hello world123



6.获取字符串中第一次、最后一次出现某个字符的索引—IndexOf & LastIndexOf



string path = @"C:\a\b\c\d\e\alien.cs";

//int index = path.IndexOf("\\");       // 获取某字符串第一次出现的索引
int index = path.LastIndexOf("\\");     // 获取某字符串最后一次出现的索引
Console.WriteLine(index);

string file_name = path.Substring(index + 1);
Console.WriteLine(file_name);

Console.ReadKey();  

// 12
// alien.cs



7.去掉字符串开头、结尾的空字符串—Trim、TrimStart、TrimEnd



string str = "           alien           ";

string str_1 = str.Trim();
Console.WriteLine(str_1);

string str_2 = str.TrimStart();			// 去掉开头的空字符串
Console.WriteLine(str_2);

string str_3 = str.TrimEnd();			// 去掉尾部的空字符串
Console.WriteLine(str_3);

Console.ReadKey();  

//alien
//alien					// 此处其实包含alien后面的空字符串
//           alien



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello-alien

您的鼓励,是我最大的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值