简体   繁体   中英

Is it possible to prevent Trim() from removing a space?

This might seem like an odd question, but is there any way of putting space in a string in such a way that Trim() will not remove it?

eg I want the output to be, for example, "Hello, World " (this is how I want it to be displayed).

The obvious answer is, don't call Trim() , unfortunately in this case I am passing the string into a library (to which I don't have the source code) and that library is doing the trimming.

Maybe there's a character that looks like a space but isn't really and so won't be trimmed? Or some invisible character to put at the end of the string?

Use a different write space, it may or may not work for you

var str = "test    ";
var str2 = str.Replace(" ", "\u00A0");

Any of these really

U+00A0  NO-BREAK SPACE  
U+1680  OGHAM SPACE MARK
U+180E  MONGOLIAN VOWEL SEPARATOR
U+2000  EN QUAD 
U+2001  EM QUAD 
U+2002  EN SPACE (nut)  
U+2003  EM SPACE (mutton)   
U+2004  THREE-PER-EM SPACE (thick space)    
U+2005  FOUR-PER-EM SPACE (mid space)   
U+2006  SIX-PER-EM SPACE    
U+2007  FIGURE SPACE    
U+2008  PUNCTUATION SPACE   
U+2009  THIN SPACE  
U+200A  HAIR SPACE  
U+200B  ZERO WIDTH SPACE    
U+202F  NARROW NO-BREAK SPACE   
U+205F  MEDIUM MATHEMATICAL SPACE
U+3000  IDEOGRAPHIC SPACE   
U+FEFF  ZERO WIDTH NO-BREAK SPACE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM