How to eliminate ALL line breaks in string?

后端 未结 12 1774
傲寒
傲寒 2021-01-30 10:22

I have a need to get rid of all line breaks that appear in my strings (coming from db). I do it using code below:

value.Replace(\"\\r\\n\", \"\").Replace(\"\\n\"         


        
相关标签:
12条回答
  • 2021-01-30 11:05

    Have you tried string.Replace(Environment.NewLine, "") ? That usually gets a lot of them for me.

    0 讨论(0)
  • 2021-01-30 11:06

    8232 (0x2028) and 8233 (0x2029) are the only other ones you might want to eliminate. See the documentation for char.IsSeparator.

    0 讨论(0)
  • 2021-01-30 11:10

    Here are some quick solutions with .NET regex:

    • To remove any whitespace from a string: s = Regex.Replace(s, @"\s+", ""); (\s matches any Unicode whitespace chars)
    • To remove all whitespace BUT CR and LF: s = Regex.Replace(s, @"[\s-[\r\n]]+", ""); ([\s-[\r\n]] is a character class containing a subtraction construct, it matches any whitespace but CR and LF)
    • To remove any vertical whitespace, subtract \p{Zs} (any horizontal whitespace but tab) and \t (tab) from \s: s = Regex.Replace(s, @"[\s-[\p{Zs}\t]]+", "");.

    Wrapping the last one into an extension method:

    public static string RemoveLineEndings(this string value)
    {
        return Regex.Replace(value, @"[\s-[\p{Zs}\t]]+", "");
    }
    

    See the regex demo.

    0 讨论(0)
  • 2021-01-30 11:11

    Check out this link: http://msdn.microsoft.com/en-us/library/844skk0h.aspx

    You wil lhave to play around and build a REGEX expression that works for you. But here's the skeleton...

    static void Main(string[] args)
    {
    
            StringBuilder txt = new StringBuilder();
            txt.Append("Hello \n\n\r\t\t");
            txt.Append( Convert.ToChar(8232));
    
            System.Console.WriteLine("Original: <" + txt.ToString() + ">");
    
            System.Console.WriteLine("Cleaned: <" + CleanInput(txt.ToString()) + ">");
    
            System.Console.Read();
    
        }
    
        static string CleanInput(string strIn)
        {
            // Replace invalid characters with empty strings.
            return Regex.Replace(strIn, @"[^\w\.@-]", ""); 
        }
    
    0 讨论(0)
  • 2021-01-30 11:13

    Below is the extension method solving my problem. LineSeparator and ParagraphEnding can be of course defined somewhere else, as static values etc.

    public static string RemoveLineEndings(this string value)
    {
        if(String.IsNullOrEmpty(value))
        {
            return value;
        }
        string lineSeparator = ((char) 0x2028).ToString();
        string paragraphSeparator = ((char)0x2029).ToString();
    
        return value.Replace("\r\n", string.Empty)
                    .Replace("\n", string.Empty)
                    .Replace("\r", string.Empty)
                    .Replace(lineSeparator, string.Empty)
                    .Replace(paragraphSeparator, string.Empty);
    }
    
    0 讨论(0)
  • 2021-01-30 11:13

    personally i'd go with

        public static String RemoveLineEndings(this String text)
        {
            StringBuilder newText = new StringBuilder();
            for (int i = 0; i < text.Length; i++)
            {
                if (!char.IsControl(text, i))
                    newText.Append(text[i]);
            }
            return newText.ToString();
        }
    
    0 讨论(0)
提交回复
热议问题