C# 教程
- 第1节:C# 简介
- 第2节:C# 环境
- 第3节:C# 程序结构
- 第4节:C# 基本语法
- 第5节:C# 数据类型
- 第6节:C# 类型转换
- 第7节:C# 变量
- 第8节:C# 常量
- 第9节:C# 运算符
- 第10节:C# 判断
- 第11节:C# 循环
- 第12节:C# 封装
- 第13节:C# 方法
- 第14节:C# 可空类型(Nullable)
- 第15节:C# 数组(Array)
- 第16节:C# 字符串(String)
- 第17节:C# 结构(Struct)
- 第18节:C# 枚举(Enum)
- 第19节:C# 类(Class)
- 第20节:C# 继承
- 第21节:C# 多态性
- 第22节:C# 运算符重载
- 第23节:C# 接口(Interface)
- 第24节:C# 命名空间(Namespace)
- 第25节:C# 预处理器指令
- 第26节:C# 正则表达式
- 第27节:C# 异常处理
- 第28节:C# 文件的输入与输出
- 第29节:C# 特性(Attribute)
- 第30节:C# 反射(Reflection)
- 第31节:C# 属性(Property)
- 第32节:C# 索引器(Indexer)
- 第33节:C# 委托(Delegate)
- 第34节:C# 事件(Event)
- 第35节:C# 集合(Collection)
- 第36节:C# 泛型(Generic)
- 第37节:C# 匿名方法
- 第38节:C# 不安全代码
- 第39节:C# 多线程
C# 字符串(String)
在 C# 中,您可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量。string 关键字是 System.String 类的别名。
创建 String 对象
您可以使用以下方法之一来创建 string 对象:
- 通过给 String 变量指定一个字符串
- 通过使用 String 类构造函数
- 通过使用字符串串联运算符( + )
- 通过检索属性或调用一个返回字符串的方法
- 通过格式化方法来转换一个值或对象为它的字符串表示形式
下面的实例演示了这点:
using System;
namespace StringApplication{
class Program{
static void Main(string[] args){
//字符串,字符串连接
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
//通过使用 string 构造函数
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//方法返回字符串
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//用于转化值的格式化方法
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
string chat = String.Format("Message sent at {0:t} on {0:D}",
waiting);
Console.WriteLine("Message: {0}", chat);
Console.ReadKey() ;
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
Full Name: RowanAtkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 17:58 on Wednesday, 10 October 2012
String 类的属性
String 类有以下两个属性:
String 类的方法
String 类有许多方法用于 string 对象的操作。下面的表格提供了一些最常用的方法:
上面的方法列表并不详尽,请访问 MSDN 库,查看完整的方法列表和 String 类构造函数。
实例
下面的实例演示了上面提到的一些方法:
比较字符串
using System;
namespace StringApplication{
class StringProg{
static void Main(string[] args){
string str1 = "This is test";
string str2 = "This is text";
if (String.Compare(str1, str2) == 0){
Console.WriteLine(str1 + " and " + str2 + " are equal.");
}
else{
Console.WriteLine(str1 + " and " + str2 + " are not equal.");
}
Console.ReadKey() ;
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
This is test and This is text are not equal.
字符串包含字符串:
using System;
namespace StringApplication{
class StringProg{
static void Main(string[] args){
string str = "This is test";
if (str.Contains("test")){
Console.WriteLine("The sequence 'test' was found.");
}
Console.ReadKey() ;
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
The sequence 'test' was found.
获取子字符串:
using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string str = "Last night I dreamt of San Pedro";
Console.WriteLine(str);
string substr = str.Substring(23);
Console.WriteLine(substr);
Console.ReadKey() ;
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
Last night I dreamt of San Pedro
San Pedro
连接字符串:
using System;
namespace StringApplication{
class StringProg{
static void Main(string[] args){
string[] starray = new string[]{"Down the way nights are dark",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"};
string str = String.Join("\n", starray);
Console.WriteLine(str);
Console.ReadKey() ;
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
Down the way nights are dark
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop