The online racing simulator
Replacing string to null C#
(6 posts, closed, started )
Replacing string to null C#
I have string:
I'm bad men.

and I will have:
I'm men.

How can I replace anything to null?
string withBad = "I'm bad men.";
string withoutBad = withBad.Replace(" bad", "");
-
(NotAnIllusion) DELETED by Victor : no longer needed
Quote from AndroidXP :string withBad = "I'm bad men.";
string withoutBad = withBad.Replace(" bad", "");

methods ftw


<?php 
string replace
(string instring tokenstring replaceWith)
{
     return 
in.Replace(tokenreplaceWith);
}
?>

Then you can do:


<?php 
string myString 
"I'm bad men.";
myString replace(myString" bad""");
?>

Sorry, I'm a minimal variable freak But when working with games, doing it like I showed is much better practice.
Sorry, but wrapping a framework method just for that seems like a really bad idea and at least to me your example doesn't make any sense whatsoever. You're just unnecessarily creating a new method call that needs it's own space in memory plus all the parameters, etc. etc. The only thing you're fixing (badly, I might add) is code that was put there intentionally for illustration purposes. I could've just assigned the result of the replace method back to the withBad variable, but that would've defeated the variable name.

I mean someone will rarely ever get a hardcoded string literal where he has to replace something out of it. Why write "I'm bad men." when you could write "I'm men." to begin with? So the point is that there will always be some variable or method that gives us the original string that needs parts of it replaced, wherever the string comes from (from a file, maybe from user chat, etc. - not hardcoded). Did you consider the case that maybe original and replaced string will both be needed at some point?

Also, what you apparently didn't know, this works too:

<?php 
string myString 
"I'm bad men.".Replace(" bad""");
?>

It's completely nonsensical but since you're a minimalist freak it might satisfy your desires
Thanks all for reply. All of this (of course) works. That's help me.

For mods: to close.
#6 - Woz
Quote from Xodus989 :methods ftw


<?php 
string replace
(string instring tokenstring replaceWith)
{
     return 
in.Replace(tokenreplaceWith);
}
?>

Then you can do:


<?php 
string myString 
"I'm bad men.";
myString replace(myString" bad""");
?>

Sorry, I'm a minimal variable freak But when working with games, doing it like I showed is much better practice.

As andriod pointed out, your "better" solution is actually slower.

1) You have an extra function call in the chain.
2) You need to pass 3 variables via the stack.
3) You need to perform stack cleanup on exit.

myString = myString.Replace(" bad", "");

is MUCH faster than your "minimal' preferred solution which is actually more bloated. I am interested in finding out why you feel you should replace class based calls with a library function?

That goes against OO principles.
This thread is closed

Replacing string to null C#
(6 posts, closed, started )
FGED GREDG RDFGDR GSFDG