String whitespace - c#

I have a string that will have multiple whitespace characters in it and I'm wanting to seperate each word by 1 whitespace character. Say if the string is "Hi! My name is troy and i love waffles!", I want to trim that so it is "Hi! My name is troy and I love waffles!". How would I do this?

Use the regular expression \s+ (one or more whitespace) with the Regex.Replace method from the System.Text.RegularExpressions namespace:
s = Regex.Replace(s, #"\s+", " ");
If you just want to replace spaces you can change the "\s" to a space "":
s = Regex.Replace(s, #" +", " ");

string.Join(" ","Hi! My name is troy and i love waffles!"
.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries)
.Select (s => s.Trim()))

Try this:
var input = "Hi! My name is troy and i love waffles!";
var output = Regex.Replace(input, #"\s{2,}", string.Empty);
Console.WriteLine(output); //Hi! My name is troy and I love waffles!

Related

How to Fix Malformed Whitespace Separator using C#

I have the following string:
string testString = ",,,The,,,boy,,,kicked,,,the,,ball";
I want to remove the unwanted commas and have the sentence as this (simply printed to the console):
The boy kicked the ball
I tried the below code:
string testString = ",,,The,,,boy,,,kicked,,,the,,ball";
string manipulatedString = testString.Replace(",", " "); //line 2
Console.WriteLine(manipulatedString.Trim() + "\n");
string result = Regex.Replace(manipulatedString, " ", " ");
Console.WriteLine(result.TrimStart());
However, I end up with a result with double whitespaces as so:
The boy kicked the ball
It kind of makes sense why I am getting such an anomalous output because in line 2 I am saying that for every comma (,) character, replace that with whitespace and it will do that for every occurrence.
What's the best way to solve this?
This is a simple solution using Split and Join
string testString = ",,,The,,,boy,,,kicked,,,the,,ball";
var splitted = testString.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
string result = string.Join(" ", splitted);
Console.WriteLine(result);
You could use regex to replace the pattern ,+ (one or more occurrences of a comma) by a whitespace.
var replacedString = Regex.Replace(testString, ",+", " ").Trim();
Added Trim to remove white spaces at beginning/end as I assume you want to remove them.

How to remove white spaces from sentence?

I want to remove all white spaces from string variable which contains a sentence.
Here is my code:
string s = "This text contains white spaces";
string ns = s.Trim();
Variable "sn" should look like "Thistextcontainswhitespaces", but it doesn't(method s.Trim() isn't working). What am I missing or doing wrong?
The method Trim usually just removes whitespace from the begin and end of a string.
string s = " String surrounded with whitespace ";
string ns = s.Trim();
Will create this string: "String surrounded with whitespace"
To remove all spaces from a string use the Replace method:
string s = "This text contains white spaces";
string ns = s.Replace(" ", "");
This will create this string: "Thistextcontainswhitespaces"
Try this.
s= s.Replace(" ", String.Empty);
Or using Regex
s= Regex.Replace(s, #"\s+", String.Empty);

Replace all non-word characters with a space

I have a regex
[A-Za-z]
and a string, such as
Hi! This is a string.
I want to replace all charcters that are not in the Regex with space. So, I'll end up with
Hi This is a string
How is this done?
var cleaned = Regex.Replace(given, "[^A-Za-z]", " ");
Try:
string output = Regex.Replace(input, "[^A-Za-z]", " ");

Replace - if preceeded by alphabet

I want to replace hyphen character with space if it is NOT enclosed by digits on both sides.
string str = "Hefer 789-567 dfg-5mh";
str = Regex.Replace(str, #"[a-zA-Z]\-(\d+)", "$1");
Output
Hefer 789-567 df5mh
Desired output
Hefer 789-567 dfg 5mh
You can use negative lookahead and lookbehind: (?<!\d)-|-(?!\d) says "match a - that is not preceeded by a \d or a - that is not followed by a \d".
Thus your regex would be something like
string str = "Hefer 789-567 dfg-5gh";
str = Regex.Replace(str, #"(?<!\d)-|-(?!\d)", " ");
Edit: Note that this also replaces hyphens at the start or end of the string. If you want to avoid this you can use (?<!\d|^)-(?=.)|(?<=.)-(?!\d|$) or (?<=[^\d])-(?=.)|(?<=.)-(?=[^\d]).
The problem you are describing in your title can be solved using this:
Regex.Replace(str, #"(?<=[A-Za-z])-", " ");
The problem you are describing in the body of your question can be solved using this:
Regex.Replace(str, #"(?<!\d)-|-(?!\d)", " ");
Or without lookaround:
Regex.Replace(str, #"([^\d])-|-([^\d])", "$1 $2");

How to convert a space into non breaking space entity in c#

I want to convert more that spaces in a string to through c#?
Like if string is
My name is this.
then output should be
My name is this.
Replace the "regular" space with the "non-breaking space" Unicode character:
string outputString = "Input text".Replace(" ", "\u00A0");
Try with RegEx if you need to convert multiple spaces to a single non-breaking-space:
string convertedText =
new Regex("[ ]{2,}").Replace(textToConvert, " ");
Example:
My Name is this
^ ^^^ ^
It'll be changed to:
My Name is this
UPDATE
If you need to preserve extra spaces (and to replace with nbsp only multiple spaces) you may use this regex:
string convertedText =
new Regex(" (?= )|(?<= ) ").Replace(textToConvert, " ");
Example:
My Name is this
^ ^^^ ^
It'll be changed to:
My Name is this
For the second case, as alternative, you may even do not use regex at all (just loop) but they should be faster if you have to do it often with the same regex.
Correction the line below will not work
Please use Server.HtmlEncode for it
You will have to do it by code
string s = " ";
if(s == " ")
{
s = " "
}
Or use "My name is this".Replace(" ", " ");
Try this
string myString = "My name is this".Replace(" ", " ");

Categories

Resources