How to trim whitespace between characters

后端 未结 9 1820
天命终不由人
天命终不由人 2020-12-05 07:17

How to remove whitespaces between characters in c#?

Trim() can be used to remove the empty spaces at the beginning of the string as well as at the end.

相关标签:
9条回答
  • 2020-12-05 07:49
    var str="  c sharp  "; str = str.Trim();
            str = Regex.Replace(str, @"\s+", " ");  ///"c sharp"
    
    0 讨论(0)
  • 2020-12-05 07:50

    If you want to keep one space between every word. this should do it..

     public static string TrimSpacesBetweenString(string s)
        {
            var mystring  =s.RemoveTandNs().Split(new string[] {" "}, StringSplitOptions.None);
            string result = string.Empty;
            foreach (var mstr in mystring)
            {
                var ss = mstr.Trim();
                if (!string.IsNullOrEmpty(ss))
                {
                    result = result + ss+" ";
                }
            }
            return result.Trim();
    
        }
    

    it will remove the string in between the string so if the input is

    var s ="c           sharp";
    result will be "c sharp";
    
    0 讨论(0)
  • 2020-12-05 07:50
    string myString = "C Sharp".Replace(" ", "");
    
    0 讨论(0)
  • 2020-12-05 07:56

    You could use String.Replace method

    string str = "C Sharp";
    str = str.Replace(" ", "");
    

    or if you want to remove all whitespace characters (space, tabs, line breaks...)

    string str = "C Sharp";
    str = Regex.Replace(str, @"\s", "");
    
    0 讨论(0)
  • 2020-12-05 08:04

    if you want to remove all spaces in one word:

    input.Trim().Replace(" ","")

    And If you want to remove extra spaces in the sentence, you should use below:

    input.Trim().Replace(" +","")
    

    the regex " +", would check if there is one ore more following space characters in the text and replace them with one space.

    0 讨论(0)
  • 2020-12-05 08:08

    I found this method great for doing things like building a class that utilizes a calculated property to take lets say a "productName" and stripping the whitespace out to create a URL that will equal an image that uses the productname with no spaces. For instance:

        namespace XXX.Models
        {
            public class Product
            {
                public int ProductID { get; set; }
                public string ProductName { get; set; }
                public string ProductDescription { get; set; }
    
                public string ProductImage
                {
                    get { return ProductName.Replace(" ", string.Empty) + ".jpg"; }
                }
            }
        }
    

    So in this answer I have used a very similar method as w69rdy, but used it in an example, plus I used string.Empty instead of "". And although after .Net 2.0 there is no difference, I find it much easier to read and understand for others who might need to read my code. I also prefer this because I sometimes get lost in all the quotes I might have in a code block.

    0 讨论(0)
提交回复
热议问题