Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp, force text vertical

Tags:

c#

regex

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#?

like image 939
Stefhan Avatar asked Nov 23 '25 09:11

Stefhan


2 Answers

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.

like image 133
Donut Avatar answered Nov 24 '25 22:11

Donut


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");
like image 33
BoltClock Avatar answered Nov 24 '25 21:11

BoltClock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!