目录
C#字符串(String)
创建 String 对象
实例练习
String 类的属性
String 类的方法
比较字符串实例练习
字符串包含字符串实例练习
获取子字符串实例练习
连接字符串实例练习
在 C# 中,您可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量。string 关键字是 System.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 类有以下两个属性:
序号 | 属性名称&描述 |
---|---|
1 | Chars |
2 | Length |
String 类有许多方法用于 string 对象的操作。下面的表格提供了一些最常用的方法:
序号 | 方法名称&描述 |
---|---|
1 | public static int Compare( string strA, string strB ) |
2 | public static int Compare( string strA, string strB, bool ignoreCase ) |
3 | public static string Concat( string str0, string str1 ) |
4 | public static string Concat( string str0, string str1, string str2 ) |
5 | public static string Concat( string str0, string str1, string str2, string str3 ) |
6 | public bool Contains( string value ) |
7 | public static string Copy( string str ) |
8 | public void CopyTo( int sourceIndex, char[] destination, int destinationIndex, int count ) |
9 | public bool EndsWith( string value ) |
10 | public bool Equals( string value ) |
11 | public static bool Equals( string a, string b ) |
12 | public static string Format( string format, Object arg0 ) |
13 | public int IndexOf( char value ) |
14 | public int IndexOf( string value ) |
15 | public int IndexOf( char value, int startIndex ) |
16 | public int IndexOf( string value, int startIndex ) |
17 | public int IndexOfAny( char[] anyOf ) |
18 | public int IndexOfAny( char[] anyOf, int startIndex ) |
19 | public string Insert( int startIndex, string value ) |
20 | public static bool IsNullOrEmpty( string value ) |
21 | public static string Join( string separator, string[] value ) |
22 | public static string Join( string separator, string[] value, int startIndex, int count ) |
23 | public int LastIndexOf( char value ) |
24 | public int LastIndexOf( string value ) |
25 | public string Remove( int startIndex ) |
26 | public string Remove( int startIndex, int count ) |
27 | public string Replace( char oldChar, char newChar ) |
28 | public string Replace( string oldValue, string newValue ) |
29 | public string[] Split( params char[] separator ) |
30 | public string[] Split( char[] separator, int count ) |
31 | public bool StartsWith( string value ) |
32 | public char[] ToCharArray() |
33 | public char[] ToCharArray( int startIndex, int length ) |
34 | public string ToLower() |
35 | public string ToUpper() |
36 | public string Trim() |
上面的方法列表并不详尽,请访问 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