C#之字符串

1 Replace

string sayHello = "Hello World!";
Console.WriteLine(sayHello);
sayHello = sayHello.Replace("Hello", "张涛");
Console.WriteLine(sayHello);

Hello World!
张涛 World!
Replace

2.ToLower() 全部小写

ToUpper()全部大写

3.Contains

string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
Console.WriteLine(songLyrics.Contains("greetings"));
True
False

string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.StartsWith("You"));
Console.WriteLine(songLyrics.StartsWith("goodbye"));

Console.WriteLine(songLyrics.EndsWith("hello"));
Console.WriteLine(songLyrics.EndsWith("goodbye"));

True
False
True
False
Contains()

 4. IndexOf() 可能无法确定列表是否包含某项,因此,应始终检查 IndexOf 返回的索引。 如果索引为 -1,表明找不到相应项。

var names = new List<string> { "<name>", "Ana", "Felipe" };
names.Add("Maria");
names.Add("Bill");
names.Remove("Ana");

var index = names.IndexOf("Felipe");
if (index != -1)
  Console.WriteLine($"The name {names[index]} is at index {index}");

var notFound = names.IndexOf("Not Found");
  Console.WriteLine($"When an item is not found, IndexOf returns {notFound}");

The name Felipe is at index 1
When an item is not found, IndexOf returns -1
IndexOf

5. Sort() 还可以对列表中的项进行排序。 Sort 方法按正常顺序(按字母顺序,如果是字符串的话)对列表中的所有项进行排序

 

 

 



posted @ 2018-03-29 17:32  日月山川  阅读(258)  评论(0编辑  收藏  举报