I have 2 strings, A and B. A contains “HelloHowAreYou”. B contains “Bingo”. I want to replace string A with string B resulting to “BingoHowAreYou”. Any tips on how to do this? Even a keyword is suffice.
I know this is kinda newbie, but i dont want to hunt all msdn doc just to find this simple things. As i said, a simple guide/keyword pointing to the right direction is enough.
Edit:
Assuming i dont know the contents of the strings. can i just use the replace? Thanks for the fast reply.
Fortunately, you don’t need to hunt down all of MSDN… just the String class.
As others have said,
String.Replace(string, string)is probably what you’re looking for:Alternatively, if you’re trying to just replace the first characters of
awith the same number of characters inb, something like this would do instead, usingSubstringand string concatenation:(Depending on exactly what you want to do when
bis longer thanaof course.)A few things worth knowing though:
usingstatements that know about theIDisposabletype) most of the time, if you’re interested in something which sounds like a library feature, you should be looking in the library documentation rather than looking for a keyword.String.Replacedoesn’t change the existing string – it returns a new string with the appropriate result. Some other types follow the same pattern – in particular value types such asDateTimeare typically immutable, soDateTime.AddDaysdoesn’t change the existing value, but returns a new value.