BirthOfTragedy
BirthOfTragedy

Reputation: 103

How do I replace more than 1 character in blackberry

I know how to replace a certain character with another character in a string:

System.out.println(replaceAll("Where are you??", "?", ""))

public static String replaceAll(String front, String pattern, String back){

    if (front == null)
        return "";

    StringBuffer sb = new StringBuffer(); //A StringBuffer is created
    int idx = -1;
    int patIdx = 0;

    while ((idx = front.indexOf(pattern, patIdx)) != -1)
    {
        sb.append(front.substring(patIdx, idx));
        sb.append(back);
        patIdx = idx + pattern.length();
    }

    sb.append(front.substring(patIdx));
    return sb.toString();
}      

This code will replace all ? with an empty space and would print out ("Where are you")

Now what I want to know is how can I replace more than 1 character. In Java I can just use simple regex, but if in blackberry I write something like:

System.out.println(replaceAll("Henry!! Where are you??", "!?", ""))

then blackberry doesn't pick it up. So how do I overcome this limitation that blackberry has?

Upvotes: 3

Views: 366

Answers (2)

Pshemo
Pshemo

Reputation: 124265

If you want to replace any character in String front that is used in String pattern you can use toCharArray(), iterate over all characters in pattern, check which is first to replace (which is nearest) and replace it. What I mean is something like this

public static String replaceAll(String front, String pattern, String back) {

    if (front == null)
        return "";

    StringBuffer sb = new StringBuffer(); // A StringBuffer is created
    int idx = -1;
    int patIdx = 0;


    boolean end = true;
    int tmp = -1;

    do {
        end = true;
        for (char c : pattern.toCharArray()) {
            //System.out.println("searching for->"+c+" from patIdx="+patIdx+" idx="+idx);
            if ((tmp = front.indexOf(c, patIdx)) != -1) {
                //System.out.println("FOUND->"+c+" from patIdx="+patIdx+" idx="+idx+" tmp="+tmp);
                if (idx == -1 || idx == patIdx-1 || (idx > patIdx && tmp < idx)){
                    end = false;
                    idx = tmp;
                    //System.out.println("setting replacement to ->"+c+" patIdx="+patIdx+" idx="+idx);
                }
            }
        }
        if (!end && idx != -1) {
            //System.out.println("replacing patIdx="+patIdx+" idx="+idx);
            sb.append(front.substring(patIdx, idx));
            sb.append(back);
            patIdx = idx+1;
        }
        //System.out.println("----");
    }while(!end);

    sb.append(front.substring(patIdx));
    return sb.toString();
}

Upvotes: 2

BBdev
BBdev

Reputation: 4942

There is not any method to do it what you want. But i can suggest you that make an array of string of the pattern which you want to replace. Loop through the array of string get the string by their position and pass that in your

public static String replaceAll(String front, String pattern, String back). Hope this will help you .

Upvotes: 4

Related Questions