如何通过字符串数组删除第一个单词
本文关键字:第一个 单词 删除 数组 何通过 字符串 | 更新日期: 2023-09-27 18:35:14
我有一个文件,我已经读入了一个字符串数组,看起来像这样
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) })
有人很可能提供更好的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[] 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);
};
林克:(像罗希特·瓦茨略有改动)
myArray = myArray.Select(x => x.Substring(Math.Max(0, x.IndexOf(' ') + 1))).ToArray();
myArray = myArray.ToList().Select(x => Regex.Replace(x, @"^.+?'s", "")).ToArray();
它必须解决你的任务。
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);