locked
Removing Both Multiple Spaces And NewLine Character In A String RRS feed

  • Question

  • Supppose I Have A String Like This

    "xyz                                               

                                           abc"

    and i want to turn this text into "xyzabc".

    How Can I Do So.

    In All The Related Questions Only Mutiple Spaces Exist And Not Multiple/Single New Line Character.

    Thank You.

    Saturday, January 12, 2013 6:29 PM

Answers

  • Try this as well:

    s = Regex.Replace(s, @"\s+", string.Empty);


    Sunday, January 13, 2013 11:17 AM
  • Raghav :

    Does this help ?

     
     string s = @"xyz                                               
    
                                           abc";
                s = s.Replace("\r\n", "").Replace(" ","");
                Console.WriteLine(" *{0}*",s);

    • Proposed as answer by JMCF125 Saturday, January 12, 2013 7:11 PM
    • Marked as answer by Jason Dot Wang Monday, January 21, 2013 9:38 AM
    Saturday, January 12, 2013 7:02 PM
  • You could also go like:

    static void Main(string[] args)
            {
                string stringTest = "xyz     "+"\r\n"+"       abc";
                Console.Write(Regex.Replace(stringTest, "[ \n\r\t]", ""));
                Console.ReadLine();
            }
    You can also add more escape sequences later to exclude more kinds of spaces 


    Don't miss to 'Vote As Helpful' or 'Mark As Answer' a deserving post.

    Sunday, January 13, 2013 7:16 AM

All replies

  • Raghav :

    Does this help ?

     
     string s = @"xyz                                               
    
                                           abc";
                s = s.Replace("\r\n", "").Replace(" ","");
                Console.WriteLine(" *{0}*",s);

    • Proposed as answer by JMCF125 Saturday, January 12, 2013 7:11 PM
    • Marked as answer by Jason Dot Wang Monday, January 21, 2013 9:38 AM
    Saturday, January 12, 2013 7:02 PM
  • You could also go like:

    static void Main(string[] args)
            {
                string stringTest = "xyz     "+"\r\n"+"       abc";
                Console.Write(Regex.Replace(stringTest, "[ \n\r\t]", ""));
                Console.ReadLine();
            }
    You can also add more escape sequences later to exclude more kinds of spaces 


    Don't miss to 'Vote As Helpful' or 'Mark As Answer' a deserving post.

    Sunday, January 13, 2013 7:16 AM
  • Try this as well:

    s = Regex.Replace(s, @"\s+", string.Empty);


    Sunday, January 13, 2013 11:17 AM