1

I have a text file that contains numbers in this format :

84  152  100       
86  149   101     
83   149   99    
86  142   101 

How can I remove the spaces and bring it in this shape :

84 152 100       
86 149 101     
83 149 99    
86 142 101  

This is what I have tried so far :

string path = Directory.GetCurrentDirectory();
string[] lines = System.IO.File.ReadAllLines(@"data_1_2.txt");
string[] line = lines[0].Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

But the result of this input is :

84
152
100
1
  • 1
    Did you tried anything so far? Commented Apr 14, 2013 at 18:07

2 Answers 2

8

Use a bit of LINQ magic:

lines = lines.Select(l => String.Join(" ", l.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))).ToArray();

It will split each line using space as a separator, remove empty entries and join them back using space as a separator again.

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

5 Comments

@FarhanAfzalSaifee You're welcome! And just because you're new here, you should read How does accepting an answer work? :) Welcome on StackOverflow!
Just a question for you @MarcinJuraszek, when I use such a thing. Console.WriteLine(line.Length); Console.WriteLine(lines.Length); Its giving me same dimension, I tried to add a new coloum, but there is no affect.
I don't really understood what you're asking about right now.
Now after I have omitted all the spaces, I would like to know its dimension, how would I do that?
You've omitted the spaces, but it's still an array of strings, not a multidimensional array of numbers.
0

You can use a simple regular expression:

lines = lines.Select(line => Regex.Replace(line, @"\s+", " ")).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.