0

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. In 4th line of code, I achieved this by first removing the character from 0 index to the length of the first word. 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. Please some one tell me what to do? I can achieve this using foreach loop or other techniques. 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

Solution:

This was pretty dumb solution but I just restarted my Visual Studio and it worked fine.

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.

4
  • That should work. Can you show the following lines of code? What means "is showing null", where? Commented Apr 6, 2013 at 14:08
  • there is no lines of code after that. All of the required code is this. The code that is calling this perfectly passing the string values to it. Commented Apr 6, 2013 at 14:11
  • If you're worried about performance issues, calling a string split isn't exactly incredibly efficient. Why are you worried about optimizing this step right now though? You gave no background on that... Seems like premature optimization. Commented Apr 6, 2013 at 14:13
  • @SMT see my question I edited it. Actually there is this type of data that contains some fields and there values. My task is to compile and store them in memory. The data is unordered. That means no predefined value of how many values would be there for a field. Commented Apr 6, 2013 at 14:22

3 Answers 3

5

Use LINQ's 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"

Sign up to request clarification or add additional context in comments.

3 Comments

You say that val is null after calling this??? What is the string in your data variable?
I know that both of our code is correct. But I don't know why the heck they are not working?
Ok, how bout this - create a fresh new Visual Studio project. Just add this code... Check if it works now - Whatever the issue is, first check if it might be some problem from some other place in your existing code.
3
string[] val = data.Remove(0, data.IndexOf(' ') + 1).Split(' ');

Comments

1

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).

So this should work(if you just want to remove the first word):

string[] val = data.Split().Skip(1).ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.