0

If I know the position of a certain character in a spannable how to I get the corresponding position in the html? For example if I have the string:

<b>example</b>

and I know the letter 'p' is at position 4 in the spannable, how do I find out that it is at position 7 in the html?

odiggity
  • 4,117
  • 7
  • 34
  • 41
  • http://jsoup.org/cookbook/extracting-data/attributes-text-html. Use jsoup to extract text from html tag. – Raghunandan Mar 24 '13 at 18:57
  • @Raghunandan can you elaborate? I don't see how extracting the text will help, I already know what the text is (in this case the letter 'p') I need to find what position in the HTML this is. – odiggity Mar 24 '13 at 23:05

1 Answers1

0

You can extract text from tags and then find the position of the character.

    String mystring= "<b>examplep</b>";
    for( int i=0; i<mystring.length(); i++ )
    {
      if( mystring.charAt(i) == 'p' ) 
       {
        System.out.println("index is"+i);//will print 7 and 10 
       }
    }  

Parsing with jsoup

    Document doc = Jsoup.parse(mystring);// can parse html tags uisng jsoup
    Elements elements = doc.select("b");
    String s= elements.text();
    System.out.println("String is"+s);
    for( int i=0; i<s.length(); i++ ) 
    {
    if( s.charAt(i) == 'p' ) 
     {
     System.out.println("index is"+i);
     }
    }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256