This question already has answers here:
How do I replace multiple spaces with a single space in C#?
(28 answers)
How to replace multiple white spaces with one white space
(17 answers)
Trying to replace all white space with a single space
(2 answers)
c# Fastest way to remove extra white spaces
(29 answers)
How to replace multiple spaces with single space?
(2 answers)
Closed 3 years ago.
Refactoring existing code.
They are trying to collapse any series of spaces in string passed in to a single space.
Surely there is a better way.
for (int i = 0; i < 25; i++)
{
str = str.Replace(" ", " ");
}
System.Text.RegularExpressions.Regex.Replace(str,#"\s+"," ");
Split the string using
List<string> spiltList = yourStr.Split(' ').ToList();
Remove empty string from collection.
spiltList.RemoveAll(e => string.IsNullOrWhiteSpace(e));
Join the list of strings to a single string
string result = string.Join(" ", spiltList);
Related
This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
C# Splitting Strings on `#` character
(3 answers)
Closed 4 years ago.
I want to convert string to string array using "," separator. I'm using this code below but when string has an ampersand cuts the end of string.
string example = "one,two,three&four";
return new []{ example };
// result ["one,two,three"]
How can I get the result: ["one","two","three&four"] ?
You should use Split not Join.
Please try to this:
string[] result = example.Split(',');
This question already has answers here:
Regex split string but keep separators
(2 answers)
Closed 7 years ago.
Let's say I have the following string: "This is a test. Haha.". I want to split it so that it becomes these there lines:
Hey.
This is a test.
Haha.
(Note that the space after the dot is preserved).
I tried to split the string using the Split method, and split by the dot, but it returns 3 new strings with the space before the string, and it removes the dots. I want to keep the space after the dot and keep the space.
How can I achieve this?
EDIT: I found a workaround, but I'm sure there's a simpler way:
string a = "Hey. This is a test. Haha.";
string[] splitted = a.Split('.');
foreach(string b in splitted)
{
if (b.Length < 3)
{
continue;
}
string f = b.Remove(0, 1);
Console.WriteLine(f + ". ");
}
I can't test this but due to the post of Darin Dimitrov :
string input = "Hey. This is a test. Haha.";
string result = input.Replace(". ", ".\n");
This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 7 years ago.
Hi have a string which is: 2109389
I want to only keep the first two characters.
So my result will be 21.
var s = "2109389";
var sub = s.SubString(0,2);
var str = "2109389";
return str.SubString(0, 2);
This question already has answers here:
How to remove all white space from the beginning or end of a string?
(7 answers)
Closed 9 years ago.
I have the following string with spaces before and after it:
string str = " hello world ";
How can I remove all spaces before and after the "hello world"? But not the space between hello world?
You can use Trim function
string str = " hello world ";
str=str.Trim();
Trim removes the spaces from both the ends only not in between
use can call Trim() function on your string to remove all white spaces in your string.
Try This:
str=str.Trim();
This question already has answers here:
How do you remove repeated characters in a string
(7 answers)
Closed 10 years ago.
I have a string given by user. After the user entry i want the character '-' to appear only once even if appears twice or more.
DF--JKIL-L should be DF-JKIL-L
`DF-----JK-L-` should be `DF-JK-L-`
A simple regular expression should do the trick:
string originalString = "DF-----JK-L-";
string replacedString = Regex.Replace(originalString, "-+", "-");
You can use Split with option StringSplitOptions.RemoveEmptyEntries, then Join again:
var result = string.Join("-",
input.Split(new[] {'-'}, StringSplitOptions.RemoveEmptyEntries));