.net - Reading file in C# based on new line -


i have file in c# has keywords each present on new line

1234-hello here 121-where 

i want read file , write 1234-hello 121-where file, have found lot of codes on internet based on comma seperated couldn't find code extract values on basis of new line.

the - seperates number , keyword want extract, 1234-hello

you can use this:

string[] lines = file.readalllines(path); for(int = 0; < lines.length; i++) {     string line = lines[i];     string[] tokens = line.split(new char[]{'-', ' '});     int number = int.parse(tokens[0]);     string text = tokens[1];     lines[i] = number + "-" + text; }  file.writealllines(path2, lines); 

you want add error handling

  • check if lines returned
  • check if tokens.length >= 2
  • etc.

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -