Remove certain empty spaces in a string in C#

remove space between string in c#
remove all whitespace from string c
c# remove extra spaces
remove multiple spaces from string c
c remove spaces and special characters from string
c# remove whitespace and newlines from string
how to remove empty space in string array c#
c# remove whitespace from end of string

I have a string as following:

strMyString = "4654564-|@$@|-Jennifer Austin    -$@%@$-646565546-|@$@|-Dutchin Henry LLC    -$@%@$-444309386-|@$@|-Winston Cooper LLC   ";

I want to remove empty spaces after the name.

I tried using

string.Replace(" ", string.Empty)

But this will remove all the empty spaces in the string, I wanted to remove only empty spaces after the name part.

My final output should like this

strMyString = "4654564-|@$@|-Jennifer Austin-$@%@$-646565546-|@$@|-Dutchin Henry LLC-$@%@$-444309386-|@$@|-Winston Cooper LLC";

The space in between the names should be intact.

Any help? Thanks in advance.

    string strMyString = "4654564-|@$@|-Jennifer Austin    -$@%@$-646565546-|@$@|-Dutchin Henry LLC    -$@%@$-444309386-|@$@|-Winston Cooper LLC   ";
    string pattern = "\\s+-";
    string replacement = "-";
    string result = Regex.Replace(strMyString, pattern, replacement);

Remove Whitespace from C# Strings : C# 411, Try the replace method of the string in C#. XML.Replace(" ", string.Empty); If you need to strip all white spaces from a string, the following method works fastest  Trimming and Removing Characters from Strings in .NET. 03/30/2017; 5 minutes to read +8; In this article. If you are parsing a sentence into individual words, you might end up with words that have blank spaces (also called white spaces) on either end of the word.

        string strMyString = "4654564-|@$@|-Jennifer Austin    -$@%@$-646565546-|@$@|-Dutchin Henry LLC    -$@%@$-444309386-|@$@|-Winston Cooper LLC   ";
        var subStrings = strMyString.Split('-').ToList();
        var newSubstrings = new List<string>();
        subStrings.ForEach(substring => newSubstrings.Add(substring.Trim(' ')));
        var newString = string.Join("-",newSubstrings);

Efficient way to remove ALL whitespace from String?, How can I remove last character from a string in C#? Tags for Remove all spaces in a string in C#. Remove blanks; how to remove all whitespace from datatable in linq; how to remove all spaces from the String in c; how to ignore the white spaces in datatable in c; deleting empty spaces in linq; datatable trim whitespace c; csharp string remove space; csharp string remove blanks

Assuming that the pattern is a set of number/name pair, internally separated by "-|@$@|-", with the pairs separated by "-$@%@$-", this seems to work:

 string input =
     "4654564-|@$@|-Jennifer Austin    -$@%@$-646565546-|@$@|-Dutchin Henry LLC    -$@%@$-444309386-|@$@|-Winston Cooper LLC   ";
 const string separator = "-$@%@$-";
 const string pattern = @"\s+-\$@%@\$-";
 var regex= new Regex(pattern);
 var firstPass = regex.Replace(input, separator);
 var result = firstPass.TrimEnd(' ');

After running, result looks like:

"4654564-|@$@|-Jennifer Austin-$@%@$-646565546-|@$@|-Dutchin Henry LLC-$@%@$-444309386-|@$@|-Winston Cooper LLC"

The Regex keys off the separator between the pairs, removing any spaces before the separator. Then, the trailing spaces are still there, so I remove that with TrimEnd

How to trim whitespace between characters, How do I remove the first character of a string in C#? Remove spaces from a string C# VB.Net. A string is a sequential collection of Unicode characters that is used to represent text. String objects are immutable that is they cannot be changed after they have been created.

Following your comments the following should do:

var newText = string.Join("-$@%@$", text.Split(new[] {"-$@%@$"}, 
                                        .Select(t => t.TrimEnd()));

Remove the last three characters from a string, Removes all leading and trailing white-space characters from the current string. public: System::String ^ Trim();. C# Copy. Introduction to Trim() in C#. In c#, Trim() method is used for removing certain types of data in the given string. For removing the spaces in a string from the start and end of the string characters, the syntax used is ‘public string Trim()’, and for removing the specific characters in a given string, the syntax used is ‘public string Trim(char[] chararr)’, where ‘char[] chararr

The simplest and easiest to understand solution that I can think of is this:

strMyString = strMyString.Replace(@"    -$@%@$", @"-$@%@$").TrimEnd(' ');

As it has been pointed out, if there are varying amounts of whitespaces, this will not work and this would be a better solution:

RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
strMyString = regex.Replace(strMyString, "");

Fastest way to remove first char in a String, We can remove all the white space characters in a string by the following code. stringtext.Replace(" ", string.Empty);. Example: using System;; using System. The string that remains after all occurrences of the characters in the trimChars parameter are removed from the start and end of the current string. If trimChars is null or an empty array, white-space characters are removed instead. If no characters can be trimmed from the current instance, the method returns the current instance unchanged.

String.Trim Method (System), There are several ways to remove whitespaces from a string in C# which consists of the space character, the horizontal tab \t , the carriage return \r , line feed \n ,  String.Remove() method removes a given number of characters from a string at a specified position. The position is a 0 index position. That means the 0 th position is the first character in the string.

Remove all spaces in a string in C#, C# Trim() is a string method. This method is used to removes all leading and trailing white-space characters from the current String object. This method can be​  Java Project Tutorial - Make Login and Register Form Step by Step Using NetBeans And MySQL Database - Duration: 3:43:32. 1BestCsharp blog Recommended for you

Remove all whitespace from a string in C#, The space character (” “) is specified in this parameter to separate the string whenever a space occurs. The join() method is used to join an array of strings using a  Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted. Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been

  • Your problem can't be solved as is. What is a name in the context of your string and how would it be identified as such? Would removing spaces preceding a - be a valid option? Because that is solvable, the former isn't unless you provide more information about the structure of your data.
  • Ok..Removing spaces preceding -$@%@$ should work.
  • @user326608 do keep in mind though that this will also add spaces if a name contains -, e.g. "jean-philippe" would result into "jean - philippe"
  • Split gives an error: Too many characters in character literal
  • @Santosh Yeah sorry, forgot the exact overload, typing on a phone here ;)
  • After you edit: Now Split cannot convert from string to char
  • @Santosh Changed the code before your last commentary and it should work just fine. Not sure what error you are referring to.
  • Yes. Your solution is valid only for a certain amount of whitespaces. If there are less it won’t do anything, if there are more it won’t remove all of them.
  • @InBetween, for or the provided input there is always a consistent number of whitespaces. This is a valid solution. Even still, see additional solution that could be used in the case of varying amounts of whitespaces.
  • Now if there are consecutive whitespaces anywhere in the string they will be all replaced, that isn't the requirement either. I didn't downvote but I'm fairly sure why your answer did get a -1; your solution is very specific and assumes something the OP never said. All other answers are either dead wrong or consider a variable number of whitespaces.
  • I took the only input that was given and provided a solution to produce the desired output. We kind of have to assume that the input always follows the same format since that is the only format we were given. What happens to our solutions if the substring -$@%@$ occurs after any whitespace that isn't succeeding a name?
  • I don't care, the OP has explicitly said that removing all whitespaces prior to -$@%@$ is a valid approach. You are assuming things, the other answers that are correct don't. Really, its not worth discussing anymore.