1

I'm working on to read a textfile which contains this line of string. And to fetch its value to my integer variables.. I want to try to avoid using Class or Arrays.

string cont = "[]val1:1val2:0val3:1";

int split = cont.IndexOf("val3:");
int val3 = Int32.Parse(cont.Substring(split + 5)); // this line successfully convert string to int
cont.Remove(split);
Console.WriteLine("Value3: " + val3 + " Content: " + cont);

split = cont.IndexOf("val2:");
int val2 = Int32.Parse(cont.Substring(split + 5)); // LINE 21
cont.Remove(split);
Console.WriteLine("Value2: " + val2 + " Content: " + cont);

split = cont.IndexOf("val1:");
int SilverCoins = Int32.Parse(cont.Substring(split + 5));
cont.Remove(split);
Console.WriteLine("Value1: " + val1 + " Content: " + cont);

When I run this code, I get an Unhandled Exception which states Input string was not in a correct format, at System.Int32.Parse(String s), line 21. :(

So, my desired output should be

Value3: 1 Content: []val1:1val2:0
Value2: 0 Content: []val1:1
Value1: 1 Content: []
3
  • Can you add the full exception to your question? Commented Feb 26, 2016 at 3:47
  • What exactly are you trying to extract from your string? Many values or just one particular value? Commented Feb 26, 2016 at 3:48
  • It seems the value returned from cont.Substring(split + 5) is not a valid number and when it tries to convert, it fails. Commented Feb 26, 2016 at 3:49

6 Answers 6

2

Your problem is in this code cont.Remove(split);
Strings are immutable, so you need to reassign new value.
In order to fix it you need to write

cont = cont.Remove(split);
Sign up to request clarification or add additional context in comments.

Comments

2

You aren't putting the remove result back into the string, try doing this:

cont = cont.Remove(split);

Comments

2

When we perform some action on string it does not make changes in the same string instance. Instead it returns a new string instance. This concept is known as Immutability.

When you do

cont.Remove(split);

it does not update cont. Instead it returns updated string which you need to capture like this

cont = cont.Remove(split);

Comments

1

Restoring the cont value after calling the Remove method is missing.

Modified Code:

string cont = "[]val1:1val2:0val3:1";

int split = cont.IndexOf("val3:");
int val3 = Int32.Parse(cont.Substring(split + 5)); // this line successfully convert string to int
cont = cont.Remove(split);
Console.WriteLine("Value3: " + val3 + " Content: " + cont);

split = cont.IndexOf("val2:");
int val2 = Int32.Parse(cont.Substring(split + 5)); // but this line fails to convert from string to int..
cont = cont.Remove(split);
Console.WriteLine("Value2: " + val2 + " Content: " + cont);

split = cont.IndexOf("val1:");
int val1 = Int32.Parse(cont.Substring(split + 5));
cont = cont.Remove(split);
Console.WriteLine("Value1: " + val1 + " Content: " + cont);

Comments

1

Two ways you can fix the problem.

C# strings are immutable, so you have to modify your code to

cont = cont.Remove(split);

Another approach(might be best for your case), when using the SubString specify length, so you get specified number of characters.

split = cont.IndexOf("val2:");
int val2 = Int32.Parse(cont.Substring(split + 5, 1)); 

Comments

1

0val3:1 is not a valid number, therefore int.Parse fails. If you need to extract the 0 from the beginning of this string, you can refer to Parse an integer from a string with trailing garbage .

Also, the result of cont.Remove(split); is never used in your code snippet, and thus does nothing but waste CPU cycles. Considering you notice this behavior in the Console.WriteLine, and simply complain about an exception, I can only assume this was intentional?


Note that it looks like you're trying to extract key-value pairs from a string. A regular expression similar to val([^:]+):([0-9]+) might be a better tool here.

Alternatively, you can split on val and take the left/right of the colon. Something like:

var vals = cont.Split(new[] {"val"}, StringSplitOptions.None)
               .Skip(1) //ignore any garbage at the beginning
               .Select(x => x.Split(':'))
               .ToDictionary(x => x[0], x => int.Parse(x[1]));

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.