This discussion is archived
5 Replies Latest reply: Mar 4, 2010 7:58 AM by 807580 RSS

select words with in a string?

807580 Newbie
Currently Being Moderated
Hello

This is a bid hard to explain so I'll use an example. Say we have the following

String s = "I would like to (see|meet) you";

I'm trying to find a way to write strings which selects one of the words inside the brackets, so when the string is initialized "I would like to meet you" would go into s and another time "I would like to see you" would go into s. What is the best way to achieve this? how can I write it in a good clean way?


Best Regards!
  • 1. Re: select words with in a string?
    807580 Newbie
    Currently Being Moderated
    One option would be to place the alternative words inside an array or a List. Then using a random number select one of those alternative words and insert into your String.

    Or even better just make the complete sentences the alternative in your list and randomly select one of them.

    Edited by: flounder on Mar 4, 2010 4:35 PM
  • 2. Re: select words with in a string?
    807580 Newbie
    Currently Being Moderated
    String[] myValues = new String[2];
    myValues[0] = "I would like to meet you";
    myValues[1] "I would like to see you";

    Then based on your logic you can use myValues[0] or myValues[1]. For example, if you still have to set your string to the value you can do this
    String s = myValues[0];

    It all depends on what your doing with the variable. I myself would pass the array around instead of setting it to a string, but again, it all depends what you are trying to do.
  • 3. Re: select words with in a string?
    YoungWinston Expert
    Currently Being Moderated
    xpro wrote:
    I'm trying to find a way to write strings which selects one of the words inside the brackets, so when the string is initialized "I would like to meet you" would go into s and another time "I would like to see you" would go into s. What is the best way to achieve this? how can I write it in a good clean way?
    First of all, you have to be very clear about what the rules are:
    Can your string contain brackets in other places, or does it only qualify as a selection if there is at least one "|" inside the brackets?

    Seems to be something I'd look at regexes for. Have a look at Pattern, Matcher or String.matches(); and String.split() might be useful for extracting the words inside the brackets.

    Winston
  • 4. Re: select words with in a string?
    807580 Newbie
    Currently Being Moderated
    xpro wrote:
    how can I write it in a good clean way?
    Apart from what's been suggested you could introduce a "static" class, like
    public class MyStrUtils {
       private MyStrUtils() {} // never instantiate this class
       private static Random rnd = new Random(); // source of randomness
       public static String randomize(String template) {
          // interprets the template String and returns a converted String
       }
    }
    //
    String s = MyStrUtils.randomize("I would like to (see|meet) you");
    You would still have to write code that analyses the template String in randomize.
  • 5. Re: select words with in a string?
    807580 Newbie
    Currently Being Moderated
    Hi,

    You can maintain the list of all available options "see,meet" in your case . And the rest String content you can store in the String object.
    And you can use the [String.format|http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#format%28java.util.Locale,%20java.lang.String,%20java.lang.Object...%29] to get the final output.

    For Example :
    String[] myValues = new String[2];
    myValues[0] = "meet";
    myValues[1] = "see";
    
    String s = String.format  ("I would like to %s you", myValues[0]);
    Hope this helps.
    Thanks