3

we are trying to create a app that shows the user messages. Here are some example messages:

  • "You have an offer for {PlayerName} from {Offerclub}. They would pay {Offer}".
  • "{Offerclub} would like to buy {Playername} for {Offer}".

We are getting these strings from our database. (Our first attempt was to make a class, with different functions, that gave us back the strings. We used string interpolation. Then we realized that this is no good option, because the users can't create contents/strings by themselves and we have to update our app every time we create a few more strings in this class.)

The options we know:

  1. String.format: something like that:

    string result = string.Format("{1} blalbal...- {2}blabla... {3}", value1, value2, value3);

The problem here: We want to use strings with different positions of the input like in our examples. Sometimes the first one is the playername, sometimes it is the clubname...

  1. String.replace: We thought about fixed placeholder names and would then use replace to make our changes.
  2. String Builder: We thought to simply concenate our strings. We would first read them from the database and use String.split to create an array of the strings.

Questions:

  • Are there better options? (We are talking about some messages. Maybe a few hundred messages in the worst case) We would prefer to use String.replace because it sounds like the easiest solution.
3
  • There is probably already a library for this, have you searched NuGet or GitHub? Commented Jan 23, 2019 at 20:56
  • String.Replace is an efficient way to go about it. It will not slow the code down at all. I'd say, since that's what you know, use that. As long as the program works and works efficiently, this should be your goal. Commented Jan 23, 2019 at 20:59
  • 2
    nuget.org/packages/StringTokenFormatter and nuget.org/packages/SmartFormat.NET/2.3.1.1 are 2 that came up in my search. There are probably more... Commented Jan 23, 2019 at 21:07

2 Answers 2

5

You can simply store the string format in the database, the order of the variables doesn't need to be consistent?

SQL Database

"You have an offer for {0} from {1}. They would pay {2}."
"{1} would like to buy {0} for {2}."

You could even leave out arguments if you wanted.

"{1} would like to buy {0}, they would like to discuss pricing."

c#

string format = GetStringFromDatabase(someIdentifier);
string message = String.Format(format, PlayerName, Offerclub, Offer);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, do you know if this is more efficient than using replace multiple times? (it's embarrassing that we have used this wrong ;)
It's unlikely string building on any scale is going to be a performance issue, it's certainly going to be many many times faster then the database lookup. I would recommend String.Format because it's concise and easily readable. I imagine most programmers would prefer to see that over .Replace().Replace().Replace()
0

There's a better and easy way to make those strings. You can try using this example:

string result = $"{value1} blalbal...- {value2}blabla... {value3}";

This "$" symbol allows you to insert the variables directly.

7 Comments

But we don' t know the strings in our code. We read them from our database. (Our first attempt was to make a class, with different functions, that gave us back the strings. We used string interpolation. Then we realized that this is no good option, because the users can't create contents/strings by themselves and we have to update our app every time we create a few more strings in this class.)
Do you already try storing the data that you read from the DataBase into a variable to then reuse it?
@basti12354 this syntax above is just a syntactic sugar for String.Format. So at least don't downvote it.
@IvanIčin I know. Have you read my question? I think I can not use this syntax in my case, do I? nevertheless the downvote is not from me ;) (He is trying to help me, I see no point to downvote his answear even though I think I can't use this in my case.
@basti12354 I've read, you asked whether to use String.Format or something else. If String.Format works then this above works too as there it is the same thing just written in different way.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.