我需要一个方法,返回从第一个字符开始的字符串中的每个其他字符.例如,使用("Java-language")的方法调用返回"Jv-agae".
private static void NewMethod(string word)
{
// here comes the code
}
Run Code Online (Sandbox Code Playgroud)
Yur*_*kiy 10
var str = "Java-language";
var xx = new string(str.Where((ch, index) => index % 2 == 0).ToArray());
Console.WriteLine(xx);
Run Code Online (Sandbox Code Playgroud)
或者这一个:
var xx = string.Join<char>("", str.Where((ch, index) => (index & 1) == 0));
Run Code Online (Sandbox Code Playgroud)
可能与其他人差别不大::-)
protected static IEnumerable<char> EverySecondChar(string word)
{
for(int i = 0; i < word.Length; i += 2)
yield return word[i];
}
string result = new string(EverySecondChar("Java-language").ToArray());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2559 次 |
| 最近记录: |