开发者

how to get words inside a string

well i am working with xml but it is not important now, the problem is the next it returns me something so

<xml>blalbalblal asfjñs
fasdf
iduser=dmengelblack; name=angel; lastname开发者_如何学Go=uc;
blablal
iduser=cccarlos; name=carlos; lastname=uc;

how do i get (dmengelblack, angel, uc, carlos, uc)

i want to save every row...

remember all it is inside a string how do i get "dmengelblack", "angel", "uc" save it, everyone in a variable, and save all this in a variable too.. for example

string id="dmengelblack";
string name="angel";
string lastname="uc";

all="dmengelblack angel uc"

and i need to save the other row too, and all rows it can have

what do i know?

  • i know before than username it is "id="
  • i know before name it is "name="
  • i know before lastname it is "lastname="
  • i kwnow everyone finish with ";"


Simple way in java is to read the file as a stream, iterate through that and get the substring between

iduser= and ;

and

name= and ;

and

lastname= and ;

EDIT: With this code you will get the list of all the filed you want as

OUTPUT:

[iduser=dmengelblack, iduser=cccarlos]
[name=angel, name=carlos]
[lastname=uc, lastname=uc]

So now you interate through these list, split the each entry on =, you will the value you wanted at the second index on split.

CODE:

String str = "<xml>blalbalblal asfjñs" + "fasdf" 
                    +   "iduser=dmengelblack; name=angel; lastname=uc;"
                    +   "blablal"
                    +   "iduser=cccarlos; name=carlos; lastname=uc;";
        List<String> iduser = new ArrayList<String>();
        List<String> name = new ArrayList<String>();
        List<String> lastname = new ArrayList<String>();
        int i = 1;
        while(str.indexOf("iduser=", i) > 0) {
            i=str.indexOf("iduser=",i);
            iduser.add(str.substring(i, str.indexOf(";", i)));
            name.add(str.substring(str.indexOf("name=", i), str.indexOf(";", str.indexOf("name=", i))));
            lastname.add(str.substring(str.indexOf("lastname=", i), str.indexOf(";", str.indexOf("lastname=", i))));
            i=str.indexOf("lastname=",i);
        }
        System.out.println(iduser);
        System.out.println(name);
        System.out.println(lastname);

hope this helps.


I would use RegEx to extract the pattern of key values into a hast table (dictionary), then use a know key mapping to assign it to the variables you have (iteration with a switch statement or something)


In c# (and other languages, but they will have different syntax) you can split a string into an array of strings like this:

string myString = "item1;item2;item3";
string[] separateStrings = myString.Split(';');

This will give you a string array like:

string[0] = "item1";    
string[1] = "item2";    
string[2] = "item3";

I would also suggest you clean up you tags to only tag what you really care about.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜