Skip to content

C# System.String API

C# List<T> —— 一个强类型的、可动态调整大小的列表,由内部数组支撑。

1 class · 8 methods

List<T>

8 methods

表示可通过索引访问的强类型对象列表。提供用于搜索、排序和操作列表的方法。

String.Length -> int

将一个对象添加到 List<T> 的末尾。

Returns

int

Example

csharp
string s = "hello";
int len = s.Length;  // 5
String.Substring(int startIndex, int length) -> string

获取 List<T> 中包含的元素数量。

Parameters

NameTypeDescription
startIndexintZero-based starting character position.
lengthintNumber 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

NameTypeDescription
itemstring要从列表中移除的对象。

Returns

int

Example

csharp
string s = "Hello, World";
int i = s.IndexOf("World");  // 7
int j = s.IndexOf("xyz");    // -1
String.Replace(string old, string new) -> string

确定某元素是否在 List<T> 中。

Parameters

NameTypeDescription
itemstring要在列表中定位的对象。
newstringString 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

NameTypeDescription
separatorchar[]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) -> string

Concatenates two specified string instances.

Parameters

NameTypeDescription
astringFirst string to concatenate.
bstringSecond string to concatenate.

Returns

string

Example

csharp
string r = string.Concat("Hello, ", "World");  // "Hello, World"