How to remove white spaces from sentence? - c#

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);

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.

Regex for only some chars

I have this text:
33113 1;3;\"windlight \"\"Feeling\"\"\r\nmetal, handmade\r\ninside: gold
metallic\r\noutisde: anthracite brushed\r\nH. 14 cm - B. 11,5
cm\";7,95;4001250331131;218,625;262,35;579;21;0004;0001;KUS\r\n
And this regex:
;\\"[^;]*\\";
Which gives the result:
;\"windlight \"\"Feeling\"\"\r\nmetal, handmade\r\ninside: gold metallic\r\noutisde: anthracite brushed\r\nH. 14 cm - B. 11,5 cm\";
Where I like to remove the new-line characters \r\n.Have you any ideas please?
I need something like this:
var replaced = Regex.Replace(
"a regex pattern which removes \r\n from the selected text", " ");
This is what I want to remove from text:
http://postimg.org/image/5hbtd1czx/
So for example,
String str = "Text contains \r \n string here";
str = str.Replace("(\r\n", " "); // with blank space
You can try to use replace:
myString = text.Replace("\r\n", "")
EDIT:
myString = text.Replace(System.Environment.NewLine, " ")
or
myString = text.Replace("\r\n", " ").Replace("\r", " ").Replace("\n", " ");
Also note that
string[] myString = File.ReadAllLines(yourTextFile);
This will automatically remove all the \n and \r from your text.

C# .Trim not working for some reason

Alright, so basically, I'm trimming a string then making it lowercase. The lowercase part works perfectly well, but the sentence doesn't get trimmed. Any clue?
var result12 = TrimTheSentence(" John. Doe# gmaiL . cOm");
//the method is
public static string TrimTheSentence(string givenString)
{
givenString = givenString.Trim();
return givenString.ToLower();
This is what you are looking for, you could shorten your method to just one line:
return givenString.Replace(" ", "").ToLower();
Trim() removes blank spaces from front and end of the string. It will not remove spaces that are in the string.
Examples:
" Test String".Trim(); //Output: "Test String", it will remove only the leading spaces, but not the space between Test and String.
" Test String ".Trim(); //Output: "Test String", it will remove leading and trailing spaces.
MSDN link: http://msdn.microsoft.com/en-us/library/system.string.trim.aspx
Trim removes spaces from the start/end, not from the entire string. Try:
return givenString.Replace(" ", "");
You can use a string extension method to remove white space within a string.
public static string RemoveWhiteSpaces(this string input)
{
return Regex.Replace(input, #"\s+", "");
}

String whitespace

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!

Remove non-alphanumerical characters excluding space

I have this statement:
String cap = Regex.Replace(winCaption, #"[^\w\.#-]", "");
that transforms "Hello | World!?" to "HelloWorld".
But I want to preserve space character, for example: "Hello | World!?" to "Hello  World".
How can I do this?
just add a space to your set of characters, [^\w.#- ]
var winCaption = "Hello | World!?";
String cap = Regex.Replace(winCaption, #"[^\w\.#\- ]", "");
Note that you have to escape the 'dash' (-) character since it normally is used to denote a range of characters (for instance, [A-Za-z0-9])
Here you go...
string cap = Regex.Replace(winCaption, #"[^\w \.#-]", "");
Try this:
String cap= Regex.Replace(winCaption, #"[^\w\.#\- ]", "");

Categories

Resources