-1

Can you tell me if there is a way to create one string from another one using the options?

For example:

{Hi|Hello|Hey}, how are you {today|doing}?

This string is the base for randomly generated string using these values.

So it can be:

Hello, how are you today? or Hi, how are you doing? or any variation.

Is there a simple way to do so?

The main thing that the initial string is not "fixed", so it can be any string with any {..|..} values, that can be found on any place instide this string.

Oleksii
  • 1,479
  • 1
  • 19
  • 35
  • Yeah it's pretty simple, just need to parse the string... try something, then come back if you get stuck – musefan May 22 '15 at 10:30
  • 1
    yes, there is a simple way to do it. – Banana May 22 '15 at 10:30
  • You might be using a shuffle approach like here http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp – Cristian E. May 22 '15 at 10:30
  • yup.u can put them `{Hi|Hello|Hey}` and `{today|doing}` in Array and use random Integer with in range of `0` to `array.lenght-1` – TheCurious May 22 '15 at 10:31
  • 2
    Why is it that simple C# questions always get upvotes even though they break all the rules of showing any research effort??? – musefan May 22 '15 at 10:38
  • 4
    From your profile: "Looking for a job of .NET developer. Ready for full-time remote employment starting from 1000$/mo." - questions like this one won't land you a job. – Ondrej Tucny May 22 '15 at 10:38
  • @OndrejTucny: Could easily land a management job with such world class delegation skills as this – musefan May 22 '15 at 10:47
  • @musefan Lol, you're right! – Ondrej Tucny May 22 '15 at 10:48
  • You're wrong, guys :) I was asking for the simple solution. Actually, Dmitry Bychenko have provided it. It was just what I was looking for. Now I understand how such tasks can be accomplished better. This is a website, where developers share their experience with one another. And this is an example of such a share. – Oleksii May 22 '15 at 11:00
  • We might be wrong, but you should read the How to ask section... – Ondrej Tucny May 22 '15 at 11:31
  • @Ondrej Tucny, yes, that was a real mistake of mine I'll avoid doing in future. Tnx. – Oleksii May 22 '15 at 11:36

3 Answers3

9

It could be something like that:

  private static Random gen = new Random();

  ...
  String source = "{Hi|Hello|Hey}, how are you {today|doing}?";

  String result = Regex.Replace(source, @"\{(\w|\|)*\}", (MatchEvaluator) (
    (match) => {
       var items = match.Value.Trim('{','}').Split('|');

       return items[gen.Next(items.Length)];
     }));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You'll need to write your own parsing engine, but yes - this is relatively simple.

Here's a rather naive implementation to get you started:

public string ParseRandomOptionsString(string input)
{
    var random = new Random();
    var output = string.Empty;
    var currentOption = string.Empty;
    var currentOptions = new List<string>();
    var optionsStarted = false;

    foreach (var c in input)
    {
        if (optionsStarted)
        {
            if (c == '}')
            {
                optionsStarted = false;
                if (!string.IsNullOrEmpty(currentOption))
                    currentOptions.Add(currentOption);

                output += currentOptions[random.Next(currentOptions.Count)];
                currentOptions.Clear();
                currentOption = null;
            }
            else if (c == '|')
            {
                if (!string.IsNullOrEmpty(currentOption))
                    currentOptions.Add(currentOption);

                currentOption = null;
            }
            else
            {
                currentOption += c;
            }
        }
        else
        {
            if (c == '{')
            {
                optionsStarted = true;
            }
            else
            {
                output += c;
            }
        }
    }

    return output;
}
Steve Lillis
  • 3,263
  • 5
  • 22
  • 41
0

Answer given by Dmitry Bychenko is best but if you dont want to use Regex, In java You can use following code-

public static void main(String s[]) {
        System.out.println(meth());
            }
public static String meth(){
     String source = "{Hi|Hello|Hey}, how are you {today|doing}?";
     List<String> list1=new ArrayList<String>();
     List<String> list2=new ArrayList<String>();
     String[] strg=source.split("\\{");
     for(String str: strg){
         if(str.contains("}")){
         String[] data=str.split("\\}");
         list1.add(data[0]);
         list2.add(data[1]);
     }
     }
     Random random = new Random();
     String[] req=new String[list1.size()];
     int i=0;
     for(String str: list1){
         req[i++]= str.split("\\|")[random.nextInt(str.split("\\|").length)];
     }
     int j=0;
     String result="";
     for(String str: list2){
         result=result+req[j++]+str;
     }
     return result;
 }
TheCurious
  • 593
  • 1
  • 4
  • 29