Regexp, force text vertical

i have this text:"i like stackoverflow", and want this result with regexp (separated with \n):

i
l
i
k
e
s
....

how i can do that with c#?

Answers


I'm really not sure regex is ideal for this (see Donut's answer), but if you really want one...

Regex.Replace("i like stackoverflow", "([^\\s]\\s?)", "$1\n");

You don't need a regex for this, you can just do this:

string input = "i like stackoverflow";

string result = string.Join("\n", input.Replace(" ", "").ToCharArray());

This code does the following:

  1. Removes all spaces in your string (input.Replace(" ", ""))
  2. Splits the string into a character array (.ToCharArray()).
  3. Joins the elements in the character array back into a single string, with a newline character separating each of them (string.Join("\n", ...))

Regular expressions are incredibly useful when their use is warranted. When it's not, though, keep this jwz quote in mind:

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.


Need Your Help

Object initialization failed in static block

java oop java-ee constructor static-block

I want to create an array of class objects and initialize it also without using any method so i wrote code like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.