还是直接看代码吧!
using System;
namespace SString
{
class Program
{
void UnderLine()
{
Console.WriteLine("-----------------------------------------------");
}
public void jichu()
{
//字符串+字符串链接
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
string fullname = fname + lname;
Console.WriteLine("Fullname : {0}", fullname);
//通过使用string构造函数
char[] letters = { 'H', 'e', 'l', 'l', 'o' };
string greetings = new string(letters);
Console.WriteLine("Greeting : {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);
}
//字符串比较
public void Compare()
{
string str1 = "This is text";
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");
}
}
//判断一个字符串中是否包含某个子串
void Contain()
{
string str = "This is text";
if(str.Contains("text"))
{
Console.WriteLine("The Sequence 'text' was found");
}
else
{
Console.WriteLine("The Sequence 'text' was not found");
}
if (str.Contains("texts"))
Console.WriteLine("The Sequence 'texts' was found");
else
Console.WriteLine("The Sequence 'texts' was not found");
}
//获取子串
void GetSubStr()
{
string str = "Last night I dreamt of San Pedro";
Console.WriteLine(str);
//从第23个开始截取、截取到最后;
string substr = str.Substring(23);
Console.WriteLine(substr);
}
//链接一个字符串数组中的所有元素、使用指定的分隔符分割每个元素
void Join()
{
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);
}
static void Main(string[] args)
{
Program P = new Program();
P.jichu();
P.UnderLine();
P.Compare();
P.UnderLine();
P.Contain();
P.UnderLine();
P.GetSubStr();
P.UnderLine();
P.Join();
P.UnderLine();
Console.ReadKey();
// Console.WriteLine("Hello World!");
}
}
}