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: []
dude
  • 572
  • 1
  • 5
  • 22

6 Answers6

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);
Kote
  • 2,116
  • 1
  • 19
  • 20
2

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

cont = cont.Remove(split);
MikeDub
  • 5,143
  • 3
  • 27
  • 44
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);
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
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]));
Community
  • 1
  • 1
lc.
  • 113,939
  • 20
  • 158
  • 187
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);
Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42
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)); 
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35