List<T>
8 methods表示可通过索引访问的强类型对象列表。提供用于搜索、排序和操作列表的方法。
String.Length -> int将一个对象添加到 List<T> 的末尾。
Returns
int
Example
csharp
string s = "hello";
int len = s.Length; // 5String.Substring(int startIndex, int length) -> string获取 List<T> 中包含的元素数量。
Parameters
| Name | Type | Description |
|---|---|---|
| startIndex | int | Zero-based starting character position. |
| length | int | Number of characters to take. |
Returns
string
Example
csharp
string s = "Hello, World";
string sub = s.Substring(7, 5); // "World"String.IndexOf(string value) -> int移除特定对象的首次出现。如果成功移除 item 则返回 true。
Parameters
| Name | Type | Description |
|---|---|---|
| item | string | 要从列表中移除的对象。 |
Returns
int
Example
csharp
string s = "Hello, World";
int i = s.IndexOf("World"); // 7
int j = s.IndexOf("xyz"); // -1String.Replace(string old, string new) -> string确定某元素是否在 List<T> 中。
Parameters
| Name | Type | Description |
|---|---|---|
| item | string | 要在列表中定位的对象。 |
| new | string | String to replace all occurrences of old. |
Returns
string
Example
csharp
string s = "Hello, World";
string r = s.Replace("World", "C#"); // "Hello, C#"String.Split(char[] separator) -> string[]使用默认比较器对整个 List<T> 中的元素进行排序。
Parameters
| Name | Type | Description |
|---|---|---|
| separator | char[] | Character array that delimits substrings. |
Returns
string[]
Example
csharp
string s = "a,b,c";
string[] parts = s.Split(',');
// parts == ["a", "b", "c"]String.ToUpper() -> string将 List<T> 的元素复制到新数组。
Returns
string
Example
csharp
string s = "hello";
string upper = s.ToUpper(); // "HELLO"String.Trim() -> string搜索与指定谓词定义的条件相匹配的元素。
Returns
string
Example
csharp
string s = " hello ";
string t = s.Trim(); // "hello"String.Concat(string a, string b) -> stringConcatenates two specified string instances.
Parameters
| Name | Type | Description |
|---|---|---|
| a | string | First string to concatenate. |
| b | string | Second string to concatenate. |
Returns
string
Example
csharp
string r = string.Concat("Hello, ", "World"); // "Hello, World"