我有一个文件,我已读入一个看起来像这样的字符串数组
string[] myArray = { "1x Car green", "2x Plane red", "3x boat blue","etc","etc" };
我想要做的是从字符串数组中的任何地方删除数字、x 和空格,所以唯一剩下的就是
string[] myArray = { "car green","Plane red","boat blue"};
你可以这样做 -
myArray = myArray.Select(x => x.Substring(x.IndexOf(' ') + 1)).ToArray();
假设- 数组中的所有单词都以Nx
.
你可以这样做:
myArray.map(function(e){ return e.substring(e.indexOf(' ') + 1) })
你可以这样做:
老套:
string[] myArray = { "1x Car green", "2x Plane red", "3x boat blue", "etc", "etc" };
for (int i = 0; i < myArray.Length; i++)
{
int indexOfSpace = myArray[i].IndexOf(' ')+1;
if (indexOfSpace >= 0 && indexOfSpace < myArray[i].Length)
myArray[i] = myArray[i].Substring(indexOfSpace);
};
Linq:(像 Rohit Vats 略有改动)
myArray = myArray.Select(x => x.Substring(Math.Max(0, x.IndexOf(' ') + 1))).ToArray();
有人很可能会提供更好的regex
,但你明白了。
string[] myArray = { "1x Car green", "2x Plane red", "3x boat blue", "etc", "etc" };
foreach (var item in myArray)
{
Console.WriteLine(Regex.Replace(item, @"([\d]{1,}x )", ""));
}
它必须解决你的任务。
string pattern = "^(\\d*x* )";
string[] myArray = { "1x Car green", "2x Plane red", "3x boat blue", "etc", "etc" };
var result = myArray.Select(p => Regex.Replace(p, pattern, String.Empty).ToLower()).ToArray();
foreach (string name in result)
Console.WriteLine(name);
myArray = myArray.ToList().Select(x => Regex.Replace(x, @"^.+?\s", "")).ToArray();