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:
- Removes all spaces in your string (input.Replace(" ", ""))
- Splits the string into a character array (.ToCharArray()).
- 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
I want to create an array of class objects and initialize it also without using any method so i wrote code like this:Why is sleeping thread stopping layout showing?
android multithreading android-layout android-activity concurrency
Why does this not draw the layout first, then sleep?