本章知识流程图如下:
一:字符串类
备注: string是一个引用类型,是一个sealed类,是一个不可变类型,存储在堆内存上,每一次修改都会从新创建一个新的string来存储,原始的会自动被回收。
(来源于官方解释:String)
二:实例化字符串对象
1:通过将字符串文本赋给 String 变量。
例:
string str1 = "你好String!";
Console.WriteLine(str1);
string str2 = "C:\\java\\java学习日志.txt";
Console.WriteLine(str2);
string str3 = @"C:\java\java学习日志.txt";
Console.WriteLine(str3);
Console.ReadLine();
运行结果:
2:通过调用 String 类构造函数。
例:
char[] chars = { 'w', 'o', 'r', 'd' };
string string1 = new string(chars);
Console.WriteLine(string1);
string string2 = new string('c', 20);
Console.WriteLine(string2);
Console.ReadLine();
运行结果:
3:通过使用字符串连接运算符
例1使用"+":
static void Main(string[] args)
{
Console.WriteLine(StrAdd());
Console.ReadLine();
}
public static string StrAdd()
{
string a = "a", b = "b", c = "c";
string sum = a + b + c;
return sum;
}
运行结果:
例2使用Concat方法:
static void Main(string[] args)
{
Console.WriteLine(StrAdd());
Console.ReadLine();
}
public static string StrAdd()
{
string sum = String.Concat("a", "b", "c");
return sum;
}
运行结果:
不推荐,使用下面这种方式进行字符串拼接,如果确实缺对象,那也可以使用这种方式进行拼接。
string sum="a";
sum+="b";
sum+="c"
备注:对于少量固定的字符串拼接,如string s= “a” + “b” + “c”,系统会优化成s= String.Concat(“a”,“b”,“c”),不会新建多个字符串。
例3使用StringBuilder :
StringBuilder str = new StringBuilder();
str.Append("你好");
str.Append("StringBuilder!");
Console.WriteLine(str.ToString());
Console.ReadLine();
例4使用Format方法:
Console.WriteLine(string.Format("{0}","你好Format!"));
Console.ReadLine();
运行结果:
例5使用$:
string content = "你好Format,我是你的简写模式:我是$!";
Console.WriteLine($"{content}");
Console.ReadLine();
运行结果:
备注:Format方法实现原理,是通过StringBuilder类进行实现的,$是Format的简写模式。
三:字符串和索引
1:这个[] 操作者可用于对字符串的单个字符进行只读访问。 有效的索引值从0并且必须小于字符串的长度
string str = "0123";
char x = str[3]; // x = 3;
Console.WriteLine("x={0}", x);
Console.ReadLine();
运行结果:
注意:索引大于字符串长度?
string str = "0123";
char x = str[4];
Console.WriteLine("x={0}", x);
Console.ReadLine();
2:迭代字符串中的每个字符
①:使用for进行迭代
string str = "你好字符串!";
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine($"for遍历test:{i}:{str[i]}");
}
Console.ReadLine();
②:使用foreach进行迭代
string str = "你好字符串!";
int count = 0;
foreach (char cr in str)
{
Console.WriteLine($"foreach遍历test:{count}:{cr}");
count++;
}
Console.ReadLine();
运行结果:
四:空字符串
1:IsNullOrEmpty,指示字符串是 null 还是与 String.Empty相等
//【1】
string str = "你好字符串!";
if(str!=null&&!str.Equals(String.Empty))
{
Console.WriteLine(str);
}
//【2】
if(!string.IsNullOrEmpty(str))
{
Console.WriteLine(str);
}
Console.ReadLine();
运行结果:
备注:上处代码【1】和【2】是等价的
2:IsNullOrWhiteSpace,指示字符串是 null、等于 String.Empty还是仅由空白字符组成。
//【1】
string str = "你好字符串!";
if(str!=null&&!str.Equals(String.Empty)&& !str.Trim().Equals(String.Empty))
{
Console.WriteLine(str);
}
//【2】
if(!string.IsNullOrWhiteSpace(str))
{
Console.WriteLine(str);
}
Console.ReadLine();
运行结果:
备注:上处代码【1】和【2】是等价的
五:常用方法和属性
1:字符串包含字符串
string strA = "str2";
string strB = "str";
string strc = "strA";
Console.WriteLine(strA.Contains(strB));
Console.WriteLine(strA.Contains(strc));
Console.ReadLine();
运行结果:
2:大小写转换
string strA = "a";
string strB = "B";
Console.WriteLine(strA.ToUpper());//"a" =>A
Console.WriteLine(strB.ToLower());//"B" =>b
Console.WriteLine();
Console.ReadLine();
运行结果:
3:字符串转换为char[]
string str = "你好String";
char[] strsum = str.ToCharArray();
for (int i = 0; i < strsum.Length; i++)
{
Console.WriteLine($"str[{i}]={strsum[i]}");
}
Console.ReadLine();
运行结果:
4:字符串分隔符
//注意观察这个字符串出现的符号和Split的元素
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string[] split = words.Split(new Char[] { ' ', ',', '.', ':', '\t' });
foreach (string s in split)
{
if (s.Trim() != "")
Console.WriteLine(s);
}
Console.ReadLine();
运行结果:
5:Equles方法和==
string a = "hello";
string b = "h";
b += "ello";
//==:比较值和地址是否一致 Equals比较值是否一致
Console.WriteLine(a == b);
Console.WriteLine(a.Equals(b));
Console.WriteLine(object.ReferenceEquals(a, b));
运行结果:
ReferenceEquals()方法
备注: 因为String引用类型是不可修改的,当用户使用 == 比较大小时,微软重写了 == ,使 == 只是起到了比较作用;判断内容是否一致,不用去比较地址。
6:获取字符串索引
①:第一次出现该字符或该字符串的位置索引。(startIndex参数表示从什么开始)
②: 最后一次出现的该字符或该字符串的位置索引。
string str = "你好String你好String你好";
Console.WriteLine($"[你]第一次出现[你]的索引是{str.IndexOf('你')}");
Console.WriteLine($"从第一个[g]开始第一次出现你好的索引是{str.IndexOf("你好", 9)}");
Console.WriteLine($"[你好]第一次出现的位置{str.IndexOf("你好")}");
Console.WriteLine($"[你好]最后一次出现的位置{str.LastIndexOf("你好")}");
Console.ReadLine();
运行结果:
7:字符串截取
string str = "你好String";
Console.WriteLine(str.Substring(1));//从什么位置开始截取
Console.WriteLine(str.Substring(0,2));//从什么位置开始,截取好长
Console.ReadLine();
运行结果:
8:增删改(不常用)
string str = "你好";
//插入
string str1 =str.Insert(2,"String");//从str中索引号为2开始返回一个新的字符串。
Console.WriteLine(str);
Console.WriteLine(str1);
//删除
string str2=str1.Remove(0,2);
Console.WriteLine(str1);
Console.WriteLine(str2);
//替换
string str3=str1.Replace("String","字符串");
Console.WriteLine(str1);
Console.WriteLine(str3);
Console.ReadLine();
运行结果:
备注: 字符串是不可变的,无法进行修改(增,删,改),修改也仅仅是进行字符串的拼接,拼接后返回新的字符串。
其他方法属性,请查看API
String API