C# 字符串(String)
在 C# 中,您可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量。string 关键字是 System.String 类的别名。
创建 String 对象
您可以使用以下方法之一来创建 string 对象:
- 通过给 String 变量指定一个字符串
- 通过使用 String 类构造函数
- 通过使用字符串串联运算符( + )
- 通过检索属性或调用一个返回字符串的方法
- 通过格式化方法来转换一个值或对象为它的字符串表示形式
下面的实例演示了这点:
实例
使用 System;
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
字符串,字符串连接
string fname, lname;
fname = “萝婉”;
lname = “阿特金森”;
字符串 fullname = fname + lname;
控制台。WriteLine(“全名:{0}”, fullname));
通过使用 string 构造函数
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
控制台。WriteLine(“问候:{0}”, 问候);
方法返回字符串
string[] sarray = { “Hello”, “From”, “Tutorials”, “Point” };
string message = 字符串。加入(“ ”, sarray));
控制台。WriteLine(“消息:{0}”, message));
用于转化值的格式化方法
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
字符串 chat = 字符串。format(“消息在 {0:D} 的 {0:t} 发送”,
等待);
控制台。WriteLine(“消息:{0}”, chat));
控制台。读取键
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
字符串,字符串连接
string fname, lname;
fname = “萝婉”;
lname = “阿特金森”;
字符串 fullname = fname + lname;
控制台。WriteLine(“全名:{0}”, fullname));
通过使用 string 构造函数
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
控制台。WriteLine(“问候:{0}”, 问候);
方法返回字符串
string[] sarray = { “Hello”, “From”, “Tutorials”, “Point” };
string message = 字符串。加入(“ ”, sarray));
控制台。WriteLine(“消息:{0}”, message));
用于转化值的格式化方法
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
字符串 chat = 字符串。format(“消息在 {0:D} 的 {0:t} 发送”,
等待);
控制台。WriteLine(“消息:{0}”, chat));
控制台。读取键

最低0.47元/天 解锁文章
1万+

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



