字符串函数
7 methods由 PHP 标准库提供的面向过程的字符串函数。
Enumerable.Where<TSource>(IEnumerable<TSource>, Func<TSource, bool>) -> IEnumerable<TSource>返回给定字符串的字节长度(而非字符数)。
Parameters
| Name | Type | Description |
|---|---|---|
| string | IEnumerable<TSource> | 输入字符串。 |
| predicate | Func<TSource, bool> | Function to test each element. |
Returns
IEnumerable<TSource>
Example
csharp
int[] nums = { 1, 2, 3, 4, 5 };
var evens = nums.Where(x => x % 2 == 0);
// evens == [2, 4]Enumerable.Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, TResult>) -> IEnumerable<TResult>查找 needle 在 haystack 中首次出现的数字位置。未找到则返回 false。
Parameters
| Name | Type | Description |
|---|---|---|
| haystack | IEnumerable<TSource> | 要搜索的字符串。 |
| needle | Func<TSource, TResult> | 要查找的子字符串。 |
Returns
IEnumerable<TResult>
Example
csharp
int[] nums = { 1, 2, 3 };
var squares = nums.Select(x => x * x);
// squares == [1, 4, 9]Enumerable.OrderBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>) -> IOrderedEnumerable<TSource>返回由 offset 和 length 指定的字符串部分。
Parameters
| Name | Type | Description |
|---|---|---|
| string | IEnumerable<TSource> | 输入字符串。 |
| offset | Func<TSource, TKey> | 起始位置(负数从末尾算起)。 |
Returns
IOrderedEnumerable<TSource>
Example
csharp
var people = new[] { new {Name="Bob", Age=30}, new {Name="Ann", Age=25} };
var sorted = people.OrderBy(p => p.Age);
// Ann(25), Bob(30)Enumerable.Sum(IEnumerable<int>) -> int将 subject 中所有出现的 search 替换为 replace。
Parameters
| Name | Type | Description |
|---|---|---|
| search | IEnumerable<int> | 要搜索的值。 |
Returns
int
Example
csharp
int[] nums = { 1, 2, 3, 4 };
int total = nums.Sum(); // 10Enumerable.Count<TSource>(IEnumerable<TSource>) -> int返回将所有 ASCII 字母字符转换为小写的字符串。
Parameters
| Name | Type | Description |
|---|---|---|
| string | IEnumerable<TSource> | 输入字符串。 |
Returns
int
Example
csharp
int[] nums = { 1, 2, 3, 4, 5 };
int n = nums.Count(); // 5Enumerable.First<TSource>(IEnumerable<TSource>) -> TSource按分隔符拆分字符串并返回片段数组。
Parameters
| Name | Type | Description |
|---|---|---|
| separator | IEnumerable<TSource> | 边界字符串。 |
Returns
TSource
Example
csharp
int[] nums = { 5, 3, 1 };
int first = nums.First(); // 5Enumerable.Distinct<TSource>(IEnumerable<TSource>) -> IEnumerable<TSource>去除字符串开头和结尾的空白字符(或其他字符)。
Parameters
| Name | Type | Description |
|---|---|---|
| string | IEnumerable<TSource> | 输入字符串。 |
Returns
IEnumerable<TSource>
Example
csharp
int[] nums = { 1, 2, 2, 3, 3, 3 };
var unique = nums.Distinct();
// unique == [1, 2, 3]