开发者

blackberry method to replace strings?

so, again, due to blackberry'开发者_运维技巧s api limitation, i have 2 questions:

  1. the response that i get back from my IOUtilities.streamToBytes(httpInput); call contains "&lt;" characters. so what blackberry class/method can i use to replace my "&lt;" with "<"? there is ONLY 1 replace() method in the String class and that method can only replace 1 character with another character. Again, since this is a blackberry project, i don't have access to anything above java 1.4

  2. the response that i also get back from my IOUtilities.streamToBytes(httpInput); call begins with the usual soap response "<?xml version="1.0" encoding="utf-8" ?><string xmlns="http://www.mydomain.com">". any blackberry class/method that i can use to tell the program to ONLY RETURN everything between "<?xml version="1.0" encoding="utf-8" ?><string xmlns="http://www.mydomain.com">" AND "</string>"?

again, thanks to all of you for your help. much appreciated. i'm beginning to learn that developing in the blackberry environment is a bit tedious at times because certain java classes (that can make coding easier) are not available for use.


What you are encountering is being forced to use a J2ME profile based on an earlier J2SE release.

I have a replace utility method for strings that I wrote when targeting earlier platforms, that might be of use to you. But do note that what you really want is an XML parser; just replacing strings will only work if the XML is very simple.

static public String replace(String val, String fnd, String rpl, boolean igncas) {
    int                                 fl=(fnd==null ? 0 : fnd.length());

    if(fl>0 && val.length()>=fl) {
        StringBuffer                    sb=null;                                // string buffer
        int                             xp=0;                                   // index of previous fnd

        for(int xa=0,mi=(val.length()-fl); xa<=mi; xa++) {
            if(val.regionMatches(igncas,xa,fnd,0,fl)) {
                if(xa>xp) { sb=append(sb,val.substring(xp,xa)); }               // substring uses private construct which does not dup char[]
                sb=append(sb,rpl);
                xp=(xa+fl);
                xa=(xp-1);                                                      // -1 to account for loop xa++;
                }
            }

        if(sb!=null) {
            if(xp<val.length()) { sb.append(val.substring(xp,val.length())); }  // substring uses private construct which does not dup char[]
            return sb.toString();
            }
        }
    return val;
    }

static private StringBuffer append(StringBuffer sb, String txt) {
    if(sb==null) { sb=new StringBuffer(txt.length()); }
    sb.append(txt);
    return sb;
    }

As to the second part of you question, you will need to use an XML parser to extract the information you want. Otherwise you will be in for some (probably kludgy) manual work.

A quick look at CLDC suggests it might be based on Java 1.1 (ugh!). DigiLife has a PDF document that has some more good information about J2ME.

Knowing which configuration (CDC or CLDC) and which profile (MIDP or PP) you are targeting is critical to knowing which APIs are available. And do note that even if you are using a profile based on J2SE 1.4, it might be missing all sorts of classes and methods, including the various XML parsing packages. Therefore you might have to provide alternatives yourself from a third party (or write them yourself).

EDIT: I note that the BlackBerry V5 doco does include XML parser packages.


Try this page:http://supportforums.blackberry.com/t5/Java-Development/String-Manipulation-split-replace-replaceAll/ta-p/620038

It supplies functions for some of the missing String functions in J2ME

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜