[android-beginners] Re: how to read specific part in a string
hi ralf, thanks for your answer. but also... your demo did not worked for me. You may try it yourself. thanks also for the links, will have a look. here now for all fellows, two routines that work - with regular tags.. if they have innertags or not - and a routine that looks specialy for innertags. public String GetXml(String xtag, String xhtml) { String x1tag = ""; String x2tag = ""; int xstart = 0; int dummy = 0; int xend = 0; x1tag = "<" + xtag; xstart = xhtml.indexOf(x1tag); dummy = xtag.indexOf(" "); if (dummy != -1) xtag = xtag.substring(0, dummy); x2tag = ""; xstart = xhtml.indexOf(">", xstart); if (xstart < 0) return ""; xend = xhtml.indexOf(x2tag, xstart); if (xend < 0) return ""; return xhtml.substring(xstart + 1, xend); } public String GetXmlInner(String xtag, String Attrib, String xhtml) { String x1tag = "<" + xtag; int x1len = x1tag.length(); String x2tag = ">"; int xstart; int xend; xstart = xhtml.indexOf(x1tag); if (xstart < 0) return ""; xend = xhtml.indexOf(x2tag, xstart); if (xend < 0) return ""; String Innerhtml = xhtml.substring(xstart + x1len, xend); // now search in the innerarea x1tag = Attrib + "=\""; x2tag = "\""; x1len = x1tag.length(); xstart = Innerhtml.indexOf(x1tag); if (xstart < 0) return ""; xend = Innerhtml.indexOf(x2tag, xstart + x1len); if (xend < 0) return ""; return Innerhtml.substring(xstart + x1len, xend); } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
I'd suggest to take that opportunity to learn on regexp and try a/ to understand what the one we gave you does and b/ how to modify it with your new requirement. Franky, regex are not that hard to come up with -- it is harder to read an existing one than to write one :-) I suggest the following reading: - Regex as implemented by Java: http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html - Any book on Perl will have a regex section. The canonical reference is here: http://perldoc.perl.org/perlretut.html Learning regex is an essential tool in your programming knowledge. Maybe this will convince you: http://xkcd.com/208/ :-D Hope this helps, R/ On Tue, Mar 17, 2009 at 1:39 PM, guruk wrote: > > I will try it... looks good :) i love this regex stuff, but still have > no clue about.. haha > but if it works.. respect.. > can you also do something like sm1 (serge) that includes also > attributes like: > > > So i guess we need something like. > > takeit (OrgString, Tagstring, AltTag) > like: takeit (myString, "status", "code") > > and best (than all is included) > takeit (myString, "status", "code",position) > -- for example if this Tag appears several times in the code I could > specify which one I would like to parse :) > > Greets > Chris > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
I will try it... looks good :) i love this regex stuff, but still have no clue about.. haha but if it works.. respect.. can you also do something like sm1 (serge) that includes also attributes like: So i guess we need something like. takeit (OrgString, Tagstring, AltTag) like: takeit (myString, "status", "code") and best (than all is included) takeit (myString, "status", "code",position) -- for example if this Tag appears several times in the code I could specify which one I would like to parse :) Greets Chris --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
Or the equivalent: public String takeit(String input, String tag) { Pattern p = Pattern.compile(".*?<(" + tag + ")>([^<]*)"); Matcher m = p.matcher(input); return m.matches() ? m.group(2) : null; } R/ On Thu, Mar 12, 2009 at 1:34 PM, EECOLOR wrote: > I think that would be something like this: > > public String takeit(String str, String tag) > { > return str.replace(".*?<" + tag + ">(.*?).*", $1); > } > > Greetz Erik > > On Thu, Mar 12, 2009 at 7:14 PM, guruk wrote: >> >> Hi, >> i have a long String and need to capture some text in between of some >> tags. >> >> for example: >> >> myString="Hello, this is a test 12345 and here i like >> to say oioidddad what is that notwise> opxmark> and now i close"; >> >> How would you do in java regex or any short thing like: >> >> starttag = takeit(myString,"start"); //result = "12345" >> marktag =takeit(myString,"marki"); //result = "oioidddad" >> opxmark=takeit(myString,"opxmark"); //result == "notwise" >> >> thanks a lot from your java newbie :) >> >> chris >> >> >> >> >> > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
thanks serge, great communication. right now its ok for me, because the code who does create the tags is also from me :) so i can be sure there is always a start and ending tag. just for anyone who is interested here an extended version that does allow to search for several tags in a long string (for example, the first appearance, the second ...) When there is nothing found, it returns the string "!NULL!" (because "" may also happen if there is just nothing between an open and end tag :) and finaly you can say it should short a long response ... Now its finished for my purpose... so just like to share with you, should u need something like that. //TakeIt ("yourtag", TheSourceString, WhatPosition, length of return) public String TakeIt(String xtag, String xhtml, int xpos, int xmax) { int xstart = 0; String xfound = ""; int xcounter = 0; // xhtml = "hallo, das ist ein testan dich do { xcounter++; String x1tag = "<" + xtag + ">"; String x2tag = ""; xstart = xhtml.indexOf(x1tag); if (xstart != -1) { int x1len = x1tag.length(); int xend = xhtml.indexOf(x2tag); if (xend != -1) { int x2len = x2tag.length(); if (xcounter == xpos) xfound = xhtml.substring(xstart + x1len, xend); // cut till here xhtml = xhtml.substring(xend + x2len); } else { xfound = "!NULL!"; } } else { xfound = "!NULL!"; } } while ((xfound != "!NULL!") && (xcounter < xpos)); if (xfound.length() > xmax) xfound = xfound.substring(0,xmax)+"..."; return xfound; } -- Also what I am thinking about, is there a Class List with several such simple Commands for Android that makes Live more easy? I really think so start a small project with a class everyone just can include in his own project and wih several simple functions. Right now I am still looking for a solution with my Listview and Gallery Update after a Thread.. http://groups.google.com/group/android-beginners/browse_thread/thread/1bdc014cee0d317b/4f8e097e30167641?hl=en#4f8e097e30167641 You are welcome to help and share your sources ( please if possible always with working source Code, special Beginners like me are very happy about :) Greets Chris On Mar 15, 3:55 pm, sm1 wrote: > Looks good guruk. It's quite fast. I timed it. > > fyi: The code I posted earlier can also work with input of xml tags > with attributes, such as > > "Hello, this is a test 12345 ..." > > which is common with tags, and it also avoids raising the nasty > RuntimeException IndexOutOfBoundsException (by method substring) when > a tag is not in the given string, which can happen in my apps. > Exceptions such as these are normally not indicated by Eclipse Europa > because, being RuntimeException children, they do not require a throws > clause in the method signature when they are not caught by a try-catch > in the method. These things often are what makes the difference > between a 3-star and a 4-star rating. > > For handling absent tags without having to manage the exception > downstream, I recommend the use of {if(xstart<0)return "";} > > String x1tag = "<" + xtag + ">"; > String x2tag = ""; > int xstart = xhtml.indexOf(x1tag); > if(xstart<0)return ""; > int x1len = x1tag.length(); > int xend = xhtml.indexOf(x2tag); > if(xend<0)return ""; > return xhtml.substring(xstart + x1len, xend); > > Or you could use a try-catch clause in the method if you know that the > exception will not occur frequently and you don't want to do anything > with the exception, and you want to keep going, such as: > > try{ > String x1tag = "<" + xtag + ">"; > String x2tag = ""; > int xstart = xhtml.indexOf(x1tag); > int x1len = x1tag.length(); > int xend = xhtml.indexOf(x2tag); > return xhtml.substring(xstart + x1len, xend); > }catch(Throwable resume){ > return ""; > } > > regards, > serge > > On Mar 14, 2:38 pm, guruk wrote: > > > > > Hi and thanks for all your great help > > > So I also like to share what I use now > > > public String TakeIt (String xtag, String xhtml) > > { > > String x1tag = "<" + xtag + ">"; > > String x2tag = ""; > >
[android-beginners] Re: how to read specific part in a string
Looks good guruk. It's quite fast. I timed it. fyi: The code I posted earlier can also work with input of xml tags with attributes, such as "Hello, this is a test 12345 ..." which is common with tags, and it also avoids raising the nasty RuntimeException IndexOutOfBoundsException (by method substring) when a tag is not in the given string, which can happen in my apps. Exceptions such as these are normally not indicated by Eclipse Europa because, being RuntimeException children, they do not require a throws clause in the method signature when they are not caught by a try-catch in the method. These things often are what makes the difference between a 3-star and a 4-star rating. For handling absent tags without having to manage the exception downstream, I recommend the use of {if(xstart<0)return "";} String x1tag = "<" + xtag + ">"; String x2tag = ""; int xstart = xhtml.indexOf(x1tag); if(xstart<0)return ""; int x1len = x1tag.length(); int xend = xhtml.indexOf(x2tag); if(xend<0)return ""; return xhtml.substring(xstart + x1len, xend); Or you could use a try-catch clause in the method if you know that the exception will not occur frequently and you don't want to do anything with the exception, and you want to keep going, such as: try{ String x1tag = "<" + xtag + ">"; String x2tag = ""; int xstart = xhtml.indexOf(x1tag); int x1len = x1tag.length(); int xend = xhtml.indexOf(x2tag); return xhtml.substring(xstart + x1len, xend); }catch(Throwable resume){ return ""; } regards, serge On Mar 14, 2:38 pm, guruk wrote: > Hi and thanks for all your great help > > So I also like to share what I use now > > public String TakeIt (String xtag, String xhtml) > { > String x1tag = "<" + xtag + ">"; > String x2tag = ""; > int xstart = xhtml.indexOf(x1tag); > int x1len = x1tag.length(); > int xend = xhtml.indexOf(x2tag); > return xhtml.substring(xstart + x1len, xend); > } > > And as Usual.. one Problem is solved, the next appear. > > Please if you know how to update a Gallery and also a Listview after a > Thread > answer > here:http://groups.google.com/group/android-beginners/t/1bdc014cee0d317b > > Great People!! > > Thanks --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
Albert, I don't think that it can be done using StringTokenizer. serge On Mar 14, 12:17 pm, Albert Hernández wrote: > I think that the methods proposed here are very heavy: > XML Parsing is too much for that easy task > "Check it manually" is not optimal > > I suggest you to have a look on the class > StringTokenizer:http://developer.android.com/reference/java/util/StringTokenizer.html > > Albert > > On Mar 13, 11:57 pm, sm1 wrote: > > > I do it this way (it works for me): > > > /** > > * @param tag The tag without the angle bracket, i.e., > > * the given value does not start with "<". > > * @param str The string containing the tags and data. > > * @return empty string when tag absent or str invalid. > > */ > > String takeit(String tag,String str){ > > try{ > > int i = str.indexOf("<"+tag+">"); > > if(i<0){ > > i = str.indexOf("<"+tag+" "); > > } > > if(i<0) { > > return "";//tag not found > > } > > String s = str.substring(i); > > i = s.indexOf(">"); > > if(i<0) { > > //no matching end-angle-bracket (>) > > return ""; > > } > > s = s.substring(i+1);//the returned string begins with the data > > of this tag > > int j = s.indexOf(""); //in this version, "" > > tags are not used. > > if(j<0) { > > //TODO Log.d(...) > > return ""; > > } > > s = s.substring(0,j); // j is the position of the end-tag. > > return s.trim(); > > }catch(Exception ex){ > > /* TODO maybe > > Log.d("myClass",ex+" - Thread.currentThread() > > +".takeit() returning empty string;" > > +" tag = "+tag > > +"; str = "+str > > ); > > */ > > return ""; > > } > > } > > > cheers > > serge > > > On Mar 12, 4:34 pm, EECOLOR wrote: > > > > I think that would be something like this: > > > > public String takeit(String str, String tag) > > > { > > > return str.replace(".*?<" + tag + ">(.*?).*", $1); > > > > } > > > > Greetz Erik > > > > On Thu, Mar 12, 2009 at 7:14 PM, guruk wrote: > > > > > Hi, > > > > i have a long String and need to capture some text in between of some > > > > tags. > > > > > for example: > > > > > myString="Hello, this is a test 12345 and here i like > > > > to say oioidddad what is that notwise > > > opxmark> and now i close"; > > > > > How would you do in java regex or any short thing like: > > > > > starttag = takeit(myString,"start"); //result = "12345" > > > > marktag =takeit(myString,"marki"); //result = "oioidddad" > > > > opxmark=takeit(myString,"opxmark"); //result == "notwise" > > > > > thanks a lot from your java newbie :) > > > > > chris --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
Hi and thanks for all your great help So I also like to share what I use now public String TakeIt (String xtag, String xhtml) { String x1tag = "<" + xtag + ">"; String x2tag = ""; int xstart = xhtml.indexOf(x1tag); int x1len = x1tag.length(); int xend = xhtml.indexOf(x2tag); return xhtml.substring(xstart + x1len, xend); } And as Usual.. one Problem is solved, the next appear. Please if you know how to update a Gallery and also a Listview after a Thread answer here: http://groups.google.com/group/android-beginners/t/1bdc014cee0d317b Great People!! Thanks --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
I think that the methods proposed here are very heavy: XML Parsing is too much for that easy task "Check it manually" is not optimal I suggest you to have a look on the class StringTokenizer: http://developer.android.com/reference/java/util/StringTokenizer.html Albert On Mar 13, 11:57 pm, sm1 wrote: > I do it this way (it works for me): > > /** > * @param tag The tag without the angle bracket, i.e., > * the given value does not start with "<". > * @param str The string containing the tags and data. > * @return empty string when tag absent or str invalid. > */ > String takeit(String tag,String str){ > try{ > int i = str.indexOf("<"+tag+">"); > if(i<0){ > i = str.indexOf("<"+tag+" "); > } > if(i<0) { > return "";//tag not found > } > String s = str.substring(i); > i = s.indexOf(">"); > if(i<0) { > //no matching end-angle-bracket (>) > return ""; > } > s = s.substring(i+1);//the returned string begins with the data > of this tag > int j = s.indexOf(""); //in this version, "" > tags are not used. > if(j<0) { > //TODO Log.d(...) > return ""; > } > s = s.substring(0,j); // j is the position of the end-tag. > return s.trim(); > }catch(Exception ex){ > /* TODO maybe > Log.d("myClass",ex+" - Thread.currentThread() > +".takeit() returning empty string;" > +" tag = "+tag > +"; str = "+str > ); > */ > return ""; > } > } > > cheers > serge > > On Mar 12, 4:34 pm, EECOLOR wrote: > > > I think that would be something like this: > > > public String takeit(String str, String tag) > > { > > return str.replace(".*?<" + tag + ">(.*?).*", $1); > > > } > > > Greetz Erik > > > On Thu, Mar 12, 2009 at 7:14 PM, guruk wrote: > > > > Hi, > > > i have a long String and need to capture some text in between of some > > > tags. > > > > for example: > > > > myString="Hello, this is a test 12345 and here i like > > > to say oioidddad what is that notwise > > opxmark> and now i close"; > > > > How would you do in java regex or any short thing like: > > > > starttag = takeit(myString,"start"); //result = "12345" > > > marktag =takeit(myString,"marki"); //result = "oioidddad" > > > opxmark=takeit(myString,"opxmark"); //result == "notwise" > > > > thanks a lot from your java newbie :) > > > > chris --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
I do it this way (it works for me): /** * @param tag The tag without the angle bracket, i.e., * the given value does not start with "<". * @param str The string containing the tags and data. * @return empty string when tag absent or str invalid. */ String takeit(String tag,String str){ try{ int i = str.indexOf("<"+tag+">"); if(i<0){ i = str.indexOf("<"+tag+" "); } if(i<0) { return "";//tag not found } String s = str.substring(i); i = s.indexOf(">"); if(i<0) { //no matching end-angle-bracket (>) return ""; } s = s.substring(i+1);//the returned string begins with the data of this tag int j = s.indexOf(""); //in this version, "" tags are not used. if(j<0) { //TODO Log.d(...) return ""; } s = s.substring(0,j); // j is the position of the end-tag. return s.trim(); }catch(Exception ex){ /* TODO maybe Log.d("myClass",ex+" - Thread.currentThread() +".takeit() returning empty string;" +" tag = "+tag +"; str = "+str ); */ return ""; } } cheers serge On Mar 12, 4:34 pm, EECOLOR wrote: > I think that would be something like this: > > public String takeit(String str, String tag) > { > return str.replace(".*?<" + tag + ">(.*?).*", $1); > > } > > Greetz Erik > > On Thu, Mar 12, 2009 at 7:14 PM, guruk wrote: > > > Hi, > > i have a long String and need to capture some text in between of some > > tags. > > > for example: > > > myString="Hello, this is a test 12345 and here i like > > to say oioidddad what is that notwise > opxmark> and now i close"; > > > How would you do in java regex or any short thing like: > > > starttag = takeit(myString,"start"); //result = "12345" > > marktag =takeit(myString,"marki"); //result = "oioidddad" > > opxmark=takeit(myString,"opxmark"); //result == "notwise" > > > thanks a lot from your java newbie :) > > > chris --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
I think that would be something like this: public String takeit(String str, String tag) { return str.replace(".*?<" + tag + ">(.*?).*", $1); } Greetz Erik On Thu, Mar 12, 2009 at 7:14 PM, guruk wrote: > > Hi, > i have a long String and need to capture some text in between of some > tags. > > for example: > > myString="Hello, this is a test 12345 and here i like > to say oioidddad what is that notwise opxmark> and now i close"; > > How would you do in java regex or any short thing like: > > starttag = takeit(myString,"start"); //result = "12345" > marktag =takeit(myString,"marki"); //result = "oioidddad" > opxmark=takeit(myString,"opxmark");//result == "notwise" > > thanks a lot from your java newbie :) > > chris > > > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: how to read specific part in a string
I think you need to use parse for that. 2009/3/12 guruk > > Hi, > i have a long String and need to capture some text in between of some > tags. > > for example: > > myString="Hello, this is a test 12345 and here i like > to say oioidddad what is that notwise opxmark> and now i close"; > > How would you do in java regex or any short thing like: > > starttag = takeit(myString,"start"); //result = "12345" > marktag =takeit(myString,"marki"); //result = "oioidddad" > opxmark=takeit(myString,"opxmark");//result == "notwise" > > thanks a lot from your java newbie :) > > chris > > > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---