简体   繁体   English

如何在黑莓中替换多个字符

[英]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??", "?", "")) System.out.println(replaceAll(“你在哪里??”,“?”,“”))

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. 现在我想知道的是如何替换超过1个字符。 In Java I can just use simple regex, but if in blackberry I write something like: 在Java中我可以使用简单的正则表达式,但如果在黑莓中我写了类似的东西:

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? 那么我该如何克服黑莓的这种局限呢?

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 . 但我可以建议你制作一个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) . public static String replaceAll(String front, String pattern, String back) Hope this will help you . 希望这会帮助你 。

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. 如果要替换String pattern中使用的String front任何字符,可以使用toCharArray() ,遍历模式中的所有字符,检查哪个是第一个要替换的(最近的)并替换它。 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM