-5

How to replace spaces with new lines. My text file looks like:

1 2 3 4 5 6

but I want to myString look like:

1  
2  
3  
4  

Code:

StreamReader myReader = new StreamReader("TextFile1.txt");
string myString = myReader.ReadLine();
myString = myString.Replace(' ', '\n');

at current state it is only adding \n over spaces.

crthompson
  • 15,653
  • 6
  • 58
  • 80
mackOne
  • 9
  • 1
  • 1

3 Answers3

2

Use:

myString = String.Join(Environment.NewLine, 
                myString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));

This will split the string based on space and then join them using Environment.NewLine. So in case of multiple spaces only one new line will appear.

Habib
  • 219,104
  • 29
  • 407
  • 436
0

You can use Environment.NewLine:

myString = myString.Replace(' ', Environment.NewLine);

This is a platform-independent line-replace. Don't use '\n'.

Environment.NewLine basically outputs "\r\n" for non-Unix platforms, and "\n" for Unix platforms. You are not on a Unix platform, so it is not outputting correctly.

You will run into a lot of weird problems if you use \n, and/or \r\n\ in the future. It's best to just use Environment.NewLine. This works on MessageBox.Show() dialogs, in StreamWriter, StreamReader, etc.

You can port the code anywhere, whereas if you were to use \n or \r\n, you'd be restricted to the platform which supports it.

-2

Use regular expressions to replace each spaces with new lines.

using System;
using System.Text.RegularExpressions;

public class Program {
    public static void Main() {
        var input = "1 2 3 4 5 6";
        Console.WriteLine("input:");
        Console.WriteLine(input);
        var output = Regex.Replace(input, " ", "\n");
        Console.WriteLine("output:");
        Console.WriteLine(output);
    }
}

https://dotnetfiddle.net/F6vmj4

Alex
  • 5,909
  • 2
  • 35
  • 25
  • Way overkill, considering regular expressions are not required. – grovesNL Dec 03 '14 at 18:52
  • Regular expressions are much better and more flexible than string replace, no matter how complex the replacement. – Alex Dec 03 '14 at 18:58
  • Define better. You mean slower? Harder to maintain? – crthompson Dec 03 '14 at 19:05
  • @Alex: How is it "better", especially when it is [way slower](http://blogs.msdn.com/b/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx) when performing a simple character replacement? – grovesNL Dec 03 '14 at 19:20
  • In general, I would say using regular expressions are better than string replacement because it is easier to maintain and understand, more extensible/flexible, not technology specific, conventional/standard, with very expected behavior. I agree though, in this case, it's probably not necessary for one character. However, I almost always hit regular expressions first because they are quicker and I usually end up extending the replacement or making it more advanced. – Alex Dec 03 '14 at 19:31