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

static void Main(string[] args)
{
	CommonOperationfunc Cof = new CommonOperationfunc();
	Cof._Length();//获取长度
	Cof._Substring();//截取字符串
	Cof._IndexOf();//索引参数在字符串的位置
	Cof._Trim();//删除前后空格(字符)
	Cof._Contains();//判断包含函数
	Cof._LowerOrUpper();//大小阶
	Cof._CompareTo();//对比字符串
	Cof._Replace();//替换
	Cof._ToCharArray();//字符串转换字符数组
	Cof._Split();//分割
	Cof._Pad();//前后不铺指定长度的字符
	Cof._Insert();//字符串指定位置插入字符串(字符)
	Cof._Remove();//删除指定位置的字符串
	Cof._AsMemory();//....
	Cof._With();//判断字符串位置(前或后)是否存在指定字符串
	Console.ReadKey();
}
class CommonOperationfunc
{
	string testStr = "a123fordTEST123b ";
	string testStr2 = "aaa123vbbbv";
	char[] arr = { '2', '1', '3' };
	List<string> list = new List<string>() { 
	"a","b","c","d","e","f"
	};

public void _Length()
{
	Console.WriteLine("--------------------------- Length start -----------------------------------");
	Console.WriteLine("字符串:" + testStr.Length);
	Console.WriteLine("数组:" + arr.Length);
	Console.WriteLine("集合:" + list.Count);
	Console.WriteLine("--------------------------- Length end -----------------------------------");
}
public void _Substring()
{
	Console.WriteLine("--------------------------- Substring start -----------------------------------");
	Console.WriteLine("只对字符串操作:" + testStr.Substring(0, testStr.Length - 1));
	Console.WriteLine("--------------------------- Substring end -----------------------------------");
}
public void _Trim()
{
	Console.WriteLine("--------------------------- Trim start -----------------------------------");
	Console.WriteLine("删除字符串前后空格:" + testStr.Trim());
	Console.WriteLine("删除字符串前空格:" + testStr.TrimStart());
	Console.WriteLine("删除字符串后空格:" + testStr.TrimEnd());
	Console.WriteLine("删除字符串前后的参数字符:" + testStr.Trim('1'));
	Console.WriteLine("删除字符串前后的字符数组内包含的字符:" + testStr.Trim(arr));
	Console.WriteLine("--------------------------- Trim end -----------------------------------");
}
public void _IndexOf()
{
	Console.WriteLine("--------------------------- IndexOf start -----------------------------------");
	Console.WriteLine("获取参数在字符串的位置:" + testStr.IndexOf("123"));//获取参数(123)在字符串的位置,存在时返回字符串位置,不存在时返回-1
	Console.WriteLine("获取参数在字符串的最后出现位置:" + testStr.LastIndexOf("123"));
	Console.WriteLine("获取参数在字符串的位置:" + testStr.IndexOfAny(arr));//索引字符数组在字符串中最早出现的位置(下标)
	Console.WriteLine("获取参数在字符串的最后出现位置:" + testStr.LastIndexOfAny(arr));
	Console.WriteLine("--------------------------- IndexOf end -----------------------------------");
            }
public void _Contains()
{
	Console.WriteLine("--------------------------- Contains start -----------------------------------");
	Console.WriteLine("获取参数在字符串的位置:" + testStr.Contains("123"));//索引字符串是否包含参数(123));
	Console.WriteLine("--------------------------- Contains end -----------------------------------");
}
public void _LowerOrUpper()
{
	Console.WriteLine("--------------------------- LowerOrUpper start -----------------------------------");
	char d = 'T';
	Console.WriteLine("小阶" + testStr.ToLower());//字符串转换成小阶
	//Console.WriteLine("ToLowerInvariant:" + testStr.ToLowerInvariant());
	Console.WriteLine("大阶" + testStr.ToUpper());//字符串转换成大阶
	Console.WriteLine("--------------------------- LowerOrUpper end -----------------------------------");
}
public void _CompareTo()
{
	Console.WriteLine("--------------------------- CompareTo start -----------------------------------");
	Console.WriteLine("CompareTo:" + testStr.CompareTo(testStr2));
	Console.WriteLine("Equals:" + testStr.Equals(testStr2));
	Console.WriteLine("--------------------------- CompareTo end -----------------------------------");
}
public void _Replace()
{
	Console.WriteLine("--------------------------- Replace start -----------------------------------");
	Console.WriteLine("Replace:" + testStr.Replace("123", "345"));
	Console.WriteLine("--------------------------- Replace end -----------------------------------");
}
public void _ToCharArray()
{
	Console.WriteLine("--------------------------- _ToCharArray start -----------------------------------");
	Console.WriteLine("字符串拆分数组" + testStr.ToCharArray());
	foreach (var item in testStr.ToCharArray())
	{
		Console.WriteLine(item);
	}
	Console.WriteLine("--------------------------- _ToCharArray end -----------------------------------");
}
public void _Split()
{
	Console.WriteLine("--------------------------- Split start -----------------------------------");
	Console.WriteLine(testStr.Split('t'));
	foreach (var item in testStr.Split('T'))
	{
		Console.WriteLine(item);
	}
	Console.WriteLine("--------------------------- Split end -----------------------------------");
}
public void _Pad()
{
	Console.WriteLine("--------------------------- _Pad start -----------------------------------");
	Console.WriteLine(testStr.PadLeft(4,'t'));//第二个参数只能是字符
	Console.WriteLine(testStr.PadRight(4, 't'));
	Console.WriteLine("--------------------------- _Pad end -----------------------------------");
}
public void _Insert()
{
	Console.WriteLine("--------------------------- _Insert start -----------------------------------");
	Console.WriteLine(testStr.Insert(4,"abc"));
	Console.WriteLine("--------------------------- _Insert end -----------------------------------");
}
public void _Remove()
{
	//testStr.Insert(start, end)
	Console.WriteLine("--------------------------- _Remove start -----------------------------------");
	Console.WriteLine(testStr.Remove(3));
	Console.WriteLine(testStr.Remove(0,3));
	Console.WriteLine("--------------------------- _Remove end -----------------------------------");
}
public void _AsMemory()
{
	//尚未搞明白
	Console.WriteLine("--------------------------- _AsMemory start -----------------------------------");
	Console.WriteLine(testStr.AsMemory(1, 3));
	Console.WriteLine("--------------------------- _AsMemory end -----------------------------------");
}
public void _With()
{
	Console.WriteLine("--------------------------- _With start -----------------------------------");
	Console.WriteLine(testStr.StartsWith('a'));//判断字符串前是否有参数字符
	Console.WriteLine(testStr.StartsWith("a123"));
	Console.WriteLine(testStr.EndsWith('b'));//判断字符串后是否有参数字符
	Console.WriteLine(testStr.EndsWith("b "));;
	Console.WriteLine("--------------------------- _With end -----------------------------------");
}
public void _Normalize()
{
	Console.WriteLine("--------------------------- _Normalize start -----------------------------------");
	Console.WriteLine(testStr.EnumerateRunes('a'));//判断字符串前是否有参数字符
	Console.WriteLine("--------------------------- _Normalize end -----------------------------------");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值