simple string manipulation
Hi Guys,
I'm a newbie here in C#.net and i just want to how to do this thing...
i have here "you got ___".... and the ____ can be any string... Can you help me how to do this... thanks!
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mike GoldPosted Jun 25, 2007, 11:11 PM
e.g.
string mystatement = "you've got {0}";
string myCompletedString = String.Format(mystatement, replacementString);
If you are actually trying to display a blank and replace it, Alan's solution is probably the best.
-Mike
Posted Jun 25, 2007, 1:37 PM
AlanPosted Jun 25, 2007, 12:36 PM
The problem with both overloads is that they replace all occurrences of a string or char with another. Although that doesn't matter in this example, you sometimes just want to replace the first occurrence which can be done by using the Regex.Replace() method instead.
Posted Jun 25, 2007, 11:30 AM
I was gonna post something useful instead of using Console.Readline(); use System("pause"); but then I remembered that was C++ and wont work in C#, lol. Sorry...
But Alan, can you really replace and entire string with another string? I thought .Replace() was only used to replace a single character with another character. Hm... I think I just learned something useful :)
AlanPosted Jun 25, 2007, 4:48 AM
Something like this perhaps:
using System;
class Program
{
static void Main()
{
string s = "you got ___";
string t = "Rex's string";
s = s.Replace("___", t);
Console.WriteLine(s);
Console.ReadLine();
}
}