1

我有一个文件,我已读入一个看起来像这样的字符串数组

string[] myArray = { "1x Car green", "2x Plane red", "3x boat blue","etc","etc" };

我想要做的是从字符串数组中的任何地方删除数字、x 和空格,所以唯一剩下的就是

string[] myArray = { "car green","Plane red","boat blue"};
4

6 回答 6

2

你可以这样做 -

myArray = myArray.Select(x => x.Substring(x.IndexOf(' ') + 1)).ToArray();

假设- 数组中的所有单词都以Nx.

于 2013-10-14T16:55:05.823 回答
1

你可以这样做:

myArray.map(function(e){ return e.substring(e.indexOf(' ') + 1) })
于 2013-10-14T16:52:48.937 回答
0

你可以这样做:

老套:

 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();
于 2013-10-14T16:56:11.640 回答
0

有人很可能会提供更好的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 )", ""));
}
于 2013-10-14T16:58:52.987 回答
0

它必须解决你的任务。

            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);
于 2013-10-14T17:20:16.037 回答
0
myArray = myArray.ToList().Select(x => Regex.Replace(x, @"^.+?\s", "")).ToArray();
于 2013-10-14T17:53:05.097 回答