How to eliminate ALL line breaks in string?

后端 未结 12 1786
傲寒
傲寒 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: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\.@-]", ""); 
        }
    

提交回复
热议问题