.net 框架基础(一) 字符、字符串

发布于:2024-05-16 ⋅ 阅读:(60) ⋅ 点赞:(0)

总目录
C# 语法总目录

字符、字符串

1.字符

占用两个字节,16位,16bit,最小值0,最大值65535

System.Char 等同于 char,如果引用了System命名空间,那么也可以简写成Char

1.1 字段

class Program
{
	static void Main(string[] args)
    {
		Console.WriteLine(Convert.ToInt32(char.MaxValue));  //字符串表示的最大范围,Convert.ToInt32是将二进制转换成十进制
		Console.WriteLine(Convert.ToInt32(char.MinValue));  //字符串表示的最小范围,Convert.ToInt32是将二进制转换成十进制
    }
}

1.2 静态方法

char chA = 'A';
char ch1 = '1';
string str = "test string";

Console.WriteLine(chA.CompareTo('C'));          //-2 (meaning 'A' is 2 less than 'C')
Console.WriteLine(chA.Equals('A'));             //"True"
Console.WriteLine(Char.GetNumericValue(ch1));   //"1"
Console.WriteLine(Char.IsControl('\t'));        //"True"
Console.WriteLine(Char.IsDigit(ch1));           //"True"
Console.WriteLine(Char.IsLetter(','));          //"False"
Console.WriteLine(Char.IsLower('u'));           //"True"
Console.WriteLine(Char.IsNumber(ch1));          //"True"
Console.WriteLine(Char.IsPunctuation('.'));     //"True"
Console.WriteLine(Char.IsSeparator(str, 4));    //"True"
Console.WriteLine(Char.IsSymbol('+'));          //"True"
Console.WriteLine(Char.IsWhiteSpace(str, 4));   //"True"
Console.WriteLine(Char.Parse("S"));             //"S"
Console.WriteLine(Char.ToLower('M'));           //"m"
Console.WriteLine('x'.ToString());              //"x"

2. 字符串

string 等价于 System.String 。string是一个不可变字符序列。

//字符串定义
string s1 = "hello";
string s2 = @"\\hello";

Console.Write(new string('*',5));		//*****

//字符串转为字符数组
string s1 = "hello";
char[] chs = s1.ToCharArray();
foreach (var item in chs)
{
    Console.WriteLine(item);
}
Console.WriteLine(new string(chs));
//输出
h
e
l
l
o
hello

2.1 null和空字符串

  • 空字符串

空字符串是值长度为0的字符串,可以用string.Empty 字段来表示。

判断一个字符串是否长度为0,可以用string.Empty来比较或者使用长度是否为0来判断。

  • null

null是指字符串指向null,没有初始化。

判断一个字符串是否为null,可以与null进行比较,或者使用方法string.IsNullOrEmpty来判断字符串是否为null或者空字符串

string empty = "";
Console.WriteLine(string.Empty == empty);
Console.WriteLine("" == empty);
Console.WriteLine(string.IsNullOrEmpty(empty));

string? nullStr = null;
Console.WriteLine(nullStr == null);
Console.WriteLine(nullStr == "");
Console.WriteLine(string.IsNullOrEmpty(nullStr));
//输出
True
True
True
True
False
True

2.2 访问字符串中的字符

string s1 = "times";
char ch = s1[1];
Console.WriteLine(ch);
//输出
i

string实现了IEnumerable< char > ,所以可以用 foreach 遍历它的字符

string s1 = "times";
foreach (var item in s1)
{
    Console.Write(item+",");
}
//输出
t,i,m,e,s,

2.3 搜索字符串内的字段

string s1 = "times";
Console.WriteLine(s1.StartsWith("ti"));
Console.WriteLine(s1.EndsWith("se"));
Console.WriteLine(s1.Contains("me"));
Console.WriteLine(s1.IndexOf("se"));
//输出
True
False
True
-1

2.4 字符串的修改方法

string s1 = "helloworld";
Console.WriteLine(s1.Substring(1,4));   //ello
Console.WriteLine(s1.Substring(2));     //lloworld  截取从开始位置以后的所有
Console.WriteLine(s1.Insert(5,"="));    //hello=world
Console.WriteLine(s1.Remove(4,3));      //hellrld
Console.WriteLine(s1.PadLeft(15,'-'));  //-----helloworld
Console.WriteLine(s1.Replace('l','o')); //heoooworod

string s2 = "---times---";
Console.WriteLine(s2.TrimStart('-'));   //times---
Console.WriteLine(s2.TrimEnd('-'));     //---times
Console.WriteLine(s2.Trim('-'));        //times

string s3 = "  t imes-- - ";
Console.WriteLine(s3.Trim().Length);    //10  去掉头尾的空格,得出的字符串长度

string s4 = "How are you?";
string[] s4s = s4.Split();
foreach (var ss in s4s)
{
    Console.WriteLine(ss);
}
//输出
//How
//are
//you?

Console.WriteLine(string.Join(" ",s4s));    //How are you?

2.5 字符串格式初体验

string patten = "hello {0},my name is {1}.";
Console.WriteLine(string.Format(patten,"lisi","zhangsan")); //hello lisi,my name is zhangsan.

//C#6 以后可以用插值
string name = "lisi";
Console.WriteLine($"hello, {name}.");     //hello,lisi.

string he = "hello";
string ll = "hello";
Console.WriteLine(he==ll);          //True
Console.WriteLine(he.Equals(ll));   //True

2.6 字符串的顺序比较

string s1 = "hello";
string s2 = "world";
Console.WriteLine(s1.CompareTo(s2));        //-1
Console.WriteLine(s1.CompareTo("hello"));   //0
Console.WriteLine(string.Compare(s1,"Hello",true)); //0        忽略大小写比较

3. StringBuilder类

它是一个可变的字符串,属于System.Text命名空间。内部容量的初始值是16个字符,如果需要添加字符,那么自动调整它的容量,默认是int.MaxValue

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++)
{
    //sb.Append(i + ",");
    //替换为下面这种最好
    sb.Append(i);
    sb.Append(",");
}
Console.WriteLine(sb.ToString());               //0,1,2,3,4,5,6,7,8,9,

Console.WriteLine(sb.Insert(2, "nihao"));       //0,nihao1,2,3,4,5,6,7,8,9,
Console.WriteLine(sb.Remove(sb.Length-1,1));    //0,nihao1,2,3,4,5,6,7,8,9
Console.WriteLine(sb.Replace(',','+'));         //0+nihao1+2+3+4+5+6+7+8+9

总目录
C# 语法总目录