0

I want to remove a white space(\n) from a string but the below code is not working

var store_tran2 = "1220 3rd St ,                      Santa Monica,                                      CA 90401";
var store_tran1 = Trim(store_tran2);

Console.Write(store_tran1);

How to remove the one line char or white spaces?

Expected output:

1220 3rd St ,Santa Monica, CA 90401
Mounarajan
  • 1,357
  • 5
  • 22
  • 43

5 Answers5

0

Try that

string res =
    Regex.Replace(original, @"^\s*$\n", string.Empty, RegexOptions.Multiline)
         .TrimEnd();
Asmaa Rashad
  • 593
  • 5
  • 28
0

Try this :

var store_tran1  = Regex.Replace(store_tran2, " {2,}", " ");
Abdellah OUMGHAR
  • 3,627
  • 1
  • 11
  • 16
0

You need to use string.Replace Your code be like this

var store_tran2 = "1220 3rd St ,                      Santa Monica,                                      CA 90401";
var store_tran1 = store_tran2.Replace("  ",string.Empty);

Your output shoould be like

1220 3rd St ,Santa Monica,CA 90401

Note

Put two spaces inside quotes " ".

Muhammad Hassan
  • 475
  • 2
  • 14
0
store_tran2 = store_tran2.Replace("\n","").Replace(" ","");
mohsen
  • 1,763
  • 3
  • 17
  • 55
0

This answers works very good may be eaxct fit for the question

http://stackoverflow.com/questions/37381525/removing-white-spaces-from-string-is-not-working-in-c-sharp/37381559#37381559
Mounarajan
  • 1,357
  • 5
  • 22
  • 43