[英]Remove substring from a string, split the result and store it in an array
My code is: 我的代码是:
public void processData(string data)
{
string com = data.Split(' ')[0];
string[] val = data.Remove(0,com.Length).Split(' ');
}
What I want to achieve using this code is that when com
stores the first word from the data
variable, the val
array should store the remaining words of the data
variable. 我想使用此代码实现的是,当com
存储data
变量中的第一个单词时, val
数组应存储data
变量的其余单词。 In 4th line of code, I achieved this by first removing the character from 0 index to the length of the first word. 在第4行代码中,我首先从0索引中删除了字符到第一个单词的长度,从而实现了这一点。 This will remove the first word and then it will split it according to whitespaces and then store the result in array. 这将删除第一个单词,然后根据空格将其拆分,然后将结果存储在数组中。 The problem is that this in not happening. 问题是这种情况没有发生。 the com
is storing the first word but the val
is showing always null. com
存储第一个单词,但val
始终显示为空。 Please some one tell me what to do? 请有人告诉我该怎么办? I can achieve this using foreach
loop or other techniques. 我可以使用foreach
循环或其他技术来实现。 But I don't want much code due to performance issues. 但是由于性能问题,我不需要太多代码。
My example strings are like this: 我的示例字符串如下所示:
begin main
timesteps 1750
weeks 250
campaigns 6
scenario 10
epsilon 0.01
powerplant1 11
powerplant2 10
constraint13 46
constraint14 7
constraint15 0
constraint16 1
constraint17 3
constraint18 0
constraint19 1
constraint20 1
constraint21 1
durations 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24
There are fields on left and values on right. 左边有字段,右边有值。 I want to separately store them. 我想分开存放它们。
Example: field is timesteps
and value is 1750
示例:字段是timesteps
,值是1750
Solution: 解:
This was pretty dumb solution but I just restarted my Visual Studio and it worked fine. 这是一个非常愚蠢的解决方案,但我只是重新启动了Visual Studio,但效果很好。
Thanks all for your nice responses. 谢谢大家的好评。 I +1 all answers and marked Blachshma answer since the suggestion to restart or recreate the project was came from him. 我为所有答案+1,并标记了Blachshma答案,因为重新启动或重新创建项目的建议来自他。
Use LINQ's Skip() 使用LINQ的Skip()
string[] val = data.Split(' ').Skip(1).ToArray();
To emphasis with a string from your example: 要强调示例中的字符串:
string data = "timesteps 1750";
string com = data.Split(' ')[0]; // Returns timesteps
string[] val = data.Split(' ').Skip(1).ToArray(); // Returns 1750
Another of your examples: 您的另一个示例:
string data = "durations 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24";
com
will have "durations" and val
will have an array of 16 elements, each with the value of "24" com
将具有“ durations”,而val
将具有由16个元素组成的数组,每个元素的值为“ 24”
string[] val = data.Remove(0, data.IndexOf(' ') + 1).Split(' ');
I assume that it val
does not show null
but the first word is empty since you have removed the substring of the first word from the string, but you have not removed the separator(white-space). 我假设它的val
不会显示为null
但第一个单词为空,因为您已从字符串中删除了第一个单词的子字符串,但尚未删除分隔符(空格)。
So this should work(if you just want to remove the first word): 所以这应该工作(如果您只想删除第一个单词):
string[] val = data.Split().Skip(1).ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.