Reputation: 180
Good evening,
I'm trying to find all occurrences of a string which starts with "{", and ends with "}", then replace them with a different string. To help illustrate, following is a short example.
string exampleString = "My name is {Name}. I was born as {Race}. I am {Height} tall. My hair is {HairLength}.";
In the above string, I want to remove {Name}, {Race}, {Height}, and {HairLength}, and replace them with something else. I'm having trouble with substrings right now, I think, in that I can't seem to split the string in the right place, so as only to remove the words in brackets.
I think I've kinda burnt myself out with this, and have made some silly errors, but I can't see them right now. The (messy) code I'm using to split the string is below.
public static string ForPlayer ( string originalString )
{
string richText = originalString;
PropertyInfo propertyInfo;
int openingInstance = 0;
int closingInstance = 0;
int length = 0;
foreach ( char c in originalString.ToCharArray ())
{
if ( c == '{' )
{
openingInstance = length;
}
if ( c == '}' && openingInstance > closingInstance )
{
int lastBrace = closingInstance;
closingInstance = length;
int lengthToNextBrace = originalString.IndexOf ( "{", closingInstance ) - closingInstance - 1;
string richInstance = originalString.Substring ( openingInstance + 1, closingInstance - openingInstance - 1 );
propertyInfo = Player.player.GetType ().GetProperty ( richInstance );
if ( propertyInfo != null )
{
string beginning;
string end;
if ( lastBrace == 0 )
{
beginning = originalString.Substring ( 0, openingInstance );
} else
{
beginning = originalString.Substring ( closingInstance + 1, openingInstance - lastBrace );
}
if ( lengthToNextBrace > -1 )
{
end = originalString.Substring ( closingInstance + 1, lengthToNextBrace );
} else
{
end = originalString.Substring ( closingInstance + 1, originalString.Length - closingInstance - 1 );
}
richText = beginning + propertyInfo.GetValue ( Player.player, null ) + end;
UnityEngine.Debug.Log ( beginning + propertyInfo.GetValue ( Player.player, null ) + end );
}
}
length += 1;
}
return richText;
}
I'm getting:
DEBUG: "My name is default. I was born as "
DEBUG: ". I am {Height} tdefault. I am "
DEBUG: " tall. Mdefault tall. My hair is "
ERROR: "ArgumentOutOfRangeException: startIndex + length > this.length"
I would like:
My name is defaultName. I was born as
defaultRace. I am
defaultHeight tall. My hair is
defaultHairLength.
===Edit===
Thank you for the answers, everyone! Unfortunately, I don't know what the string that I want to replace will be, and there are so many possibilities, that I'd rather use my current method of finding it via reflection, if possible.
Upvotes: 0
Views: 1391
Reputation: 86778
Here's a clean, simple version. It uses the IndexOf
method find braces rather than iterating over characters. It also uses a StringBuilder
to avoid copying strings where possible.
public static string ForPlayer(string originalString, object player)
{
var type = player.GetType();
var sb = new StringBuilder(originalString.Length);
var lastEnd = 0; // after the last close brace
var start = originalString.IndexOf('{'); // start brace
while (start != -1) // go until we run out of open braces
{
var end = originalString.IndexOf('}', start + 1); // end brace
if (end == -1) // if there's a start brace but no end, just quit
break;
// copy from the end of the last string to the start of the new one
sb.Append(originalString, lastEnd, start - lastEnd);
// get the name of the property to look up
var propName = originalString.Substring(start + 1, end - start - 1);
// add in the property value
sb.Append(type.GetProperty(propName).GetValue(player, null));
lastEnd = end + 1; // move the pointer to the end of the last string
start = originalString.IndexOf('{', lastEnd); // find the next start
}
// copy the end of the string
sb.Append(originalString, lastEnd, originalString.Length - lastEnd);
return sb.ToString();
}
Upvotes: 2
Reputation: 117154
Try this:
public static string ForPlayer(string originalString)
{
return
Regex
.Matches(originalString, "{(.*?)}")
.Cast<Match>()
.Select(x => x.Groups[1].Value)
.Select(x => new
{
From = x,
To = Player.player
.GetType()
.GetProperty(x)
.GetValue(Player.player)
.ToString()
})
.Aggregate(originalString, (a, x) => a.Replace("{" + x.From + "}", x.To));
}
I tested this on your sample input, "My name is {Name}. I was born as {Race}. I am {Height} tall. My hair is {HairLength}."
, using the following test class:
public class Player
{
public string Name {get; set; }
public string Race {get; set; }
public string Height {get; set; }
public int HairLength {get; set; }
}
var player = new Player()
{
Name = "Fred", Race = "English", Height = "Tall", HairLength = 33
};
And got this result:
"My name is Fred. I was born as English. I am Tall tall. My hair is 33."
This is even better:
public static string ForPlayer(string originalString)
{
return
Regex
.Replace(originalString, "{(.*?)}", m =>
Player.player
.GetType()
.GetProperty(m.Groups[1].Value)
.GetValue(Player.player)
.ToString());
}
Upvotes: 2
Reputation: 3558
If the placeholders are in fact property names of your objects, then you could utilize FormatWith
to do the job.
string exampleString = "My name is {Name}. I was born as {Race}. I am {Height} tall. My hair is {HairLength}.";
string replacedString = exampleString.FormatWith(Player.player);
Upvotes: 2
Reputation: 11
Here is my approach that uses Regular Expressions.
using System.Text.RegularExpressions;
...
class Program
{
static int occurence = 0;
static string[] defValues = new string[] { "DefName", "DefRace", "DefHeight", "DefHair" };
static string ReplaceWithDefault(Match m)
{
if (occurence < defValues.Length)
return defValues[occurence++];
else
return "NO_DEFAULT_VALUE_FOUND";
}
static void Main(string[] args)
{
string exampleString = "My name is {Name}. I was born as {Race}. I am {Height} tall. My hair is {HairLength}.";
string replaced = Regex.Replace(exampleString, "\\{[^\\}]+\\}", new MatchEvaluator(ReplaceWithDefault));
occurence = 0;
Console.WriteLine(exampleString);
Console.WriteLine(replaced);
}
}
Upvotes: -1
Reputation: 50210
var replacement=new Dictionary<string,string>{
{"Name","defaultName"},
{"Race","defaultRace"},
{"Height","defaultHeight"},
{"HairLength","defaultHairLength"}
};
string exampleString="My name is {Name}. I was born as {Race}. I am {Height} tall. My hair is {HairLength}.";
foreach(var kv in replacement)
{
exampleString = exampleString.Replace("{" + kv.Key + "}", kv.Value);
}
stealing the first part from PetSerAl
Upvotes: 0