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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
AlanRank #669 · 3.6K reputationPosted Jun 25, 2007, 12:48 AM EDT
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();
}
}
Posted Jun 25, 2007, 7:30 AM EDT
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 :)
AlanRank #669 · 3.6K reputationPosted Jun 25, 2007, 8:36 AM EDT
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, 9:37 AM EDT
Mike GoldRank #50 · 30.6K reputationPosted Jun 25, 2007, 7:11 PM EDT
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