Hi, i loop the value on first column each row of datagrideview, and the format has "\" in the middle, how do we convert convert the string without "\"
ex. "Hello\World" to "HelloWorld" "Hi\There" to "HiThere""
etc
From stackoverflow
Rick
-
string hello = "Hello\\World"; string helloWithoutBackslashes = hello.Replace("\\",string.Empty);or, using the @ operator
string hi = @"Hi\There"; string hiWithoutBackslashes = hi.Replace(@"\",string.Empty);ChaosPandion : This won't compile unless you use @fixed.Vinko Vrsalovic : ChaosPandion: "@fixed", thanks. I always forget that's a keyword.pm100 : whats @fixed? plsChaosPandion : fixed is a keyword in C# that tells the garbage collected not to move around a block of memory. It can only be used in an unsafe context.tommieb75 : Ummm... there's no mention of anything in unsafe context here....is there pointers involved, if so, then fixed/unsafe keywords would be used. Can you please edit the question as you have confused me?tommieb75 : the @ is a verbatim string in which no escaping of back slashes occur so what has that to do with fixed keyword?ChaosPandion : Look at what I have done!Vinko Vrsalovic : @tommie: The thing is I had named the variable 'fixed' so my answer wouldn't compile. I edited the answer so it compiles. @ChaosPandion: What you've done is not very practical, at the very least receive the undesired characters as a parameter. Also it fits as at least a static method or an extension method.ChaosPandion : Actually What I meant was I caused confusion by mentioning unsafe code. As for my answer, I meant to post it as a form of protest to multiple people posting the same exact answer. I hope no one ever uses my code even though it would work.From Vinko Vrsalovic -
I thought I would mix it up a bit.
public class StringCleaner { private readonly string dirtyString; public StringCleaner(string dirtyString) { this.dirtyString = dirtyString; } public string Clean() { using (var sw = new System.IO.StringWriter()) { foreach (char c in dirtyString) { if (c != '\\') sw.Write(c); } return sw.ToString(); } } }Yuriy Faktorovich : Maybe use a StringWriter to mix it up more/speed up?ChaosPandion : Pure genius my friend.Yuriy Faktorovich : Man, you gotta cache the lazy execution.From ChaosPandion
0 comments:
Post a Comment