1.使用“+”号
string name = "John";
int age = 30;
string result = "My name is " + name + " and I am " + age + " years old.";
Debug.Log(result);
2.使用string.format
string name = "John";
int age = 30;
string result = string.Format("My name is {0} and I am {1} years old.", name, age);
Debug.Log(result);
3.使用插值字符串
string name = "John";
int age = 30;
string result = $"My name is {name} and I am {age} years old.";
Debug.Log(result);
4.StringBuilder 拼接字符串,(可自行封装相关类去处理)
StringBuilder str = new StringBuilder();
str.Append("My name is Hohn");
以上为常用的四种方式,记得保存,经常用到
本文介绍了在编程中拼接字符串的四种常见方式:1)使用加号+连接字符串;2)使用string.Format方法格式化字符串;3)使用C#的插值字符串;4)利用StringBuilder类高效构建字符串。这些方法在不同的场景下各有优势,开发者可根据需求选择合适的方法。
16万+

被折叠的 条评论
为什么被折叠?



