ReplaceAll help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Energizer100
    New Member
    • Nov 2007
    • 60

    ReplaceAll help

    In the code below, I'm trying to convert a sentence into shorthand, meaning I take away all the vowels, plus I convert "and" to "&", "to" to "2", "you" to "U", and "for" to "4".

    public static String shorthandString (String shstr)
    {
    String shortHandString = "";

    shortHandString = Pattern.compile ("and").matcher (shortHandStrin g).replaceAll(" &");
    shortHandString = shortHandString .replaceAll("to ", "2");
    shortHandString = shortHandString .replaceAll("yo u", "U");
    shortHandString = shortHandString .replaceAll("fo r", "4");

    //Check string and replace vowel.
    for (int n = 0; n < shstr.length(); n++)
    {
    if (!checkVowel(sh str.charAt(n)) && (shstr.charAt(n ) != 'U'))
    {
    shortHandString += shstr.charAt(n) ;
    }
    }

    return shortHandString ;
    }

    I'm having trouble with the replace all because, for example, if i input "and", it returns "nd" not "&".
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Energizer100
    In the code below, I'm trying to convert a sentence into shorthand, meaning I take away all the vowels, plus I convert "and" to "&", "to" to "2", "you" to "U", and "for" to "4".

    public static String shorthandString (String shstr)
    {
    String shortHandString = "";

    shortHandString = Pattern.compile ("and").matcher (shortHandStrin g).replaceAll(" &");
    shortHandString = shortHandString .replaceAll("to ", "2");
    shortHandString = shortHandString .replaceAll("yo u", "U");
    shortHandString = shortHandString .replaceAll("fo r", "4");

    //Check string and replace vowel.
    for (int n = 0; n < shstr.length(); n++)
    {
    if (!checkVowel(sh str.charAt(n)) && (shstr.charAt(n ) != 'U'))
    {
    shortHandString += shstr.charAt(n) ;
    }
    }

    return shortHandString ;
    }

    I'm having trouble with the replace all because, for example, if i input "and", it returns "nd" not "&".
    Just call String's replaceAll method. It does all the compiling, matching and replacing for you.
    [CODE=java]shortHandString = shstr.replaceAl l("and", "&");
    //e.t.c
    [/CODE]

    Comment

    Working...