I am trying to parse the "html_instructions" string from the "steps" array at this link:
I have no trouble parsing the string, but it returns with bits of code mixed in. For Example, the parsed string of:
"html_instructions" : "Head \u003cb\u003esouthwest\u003c/b\u003e toward \u003cb\u003eCapitol Square SW\u003c/b\u003e",
Appears as:
Head<br>southwest"</br>"towards<br>...
Instead of appearing simply as:
Head southwest towards...
Is there a way i can format the string to remove the "breaks"? Any help is greatly appreciated.
You can use a regex to remove HTML tags from your content.
String htmlString = "Head<br>southwest</br>towards<br>...";
String noHtml = htmlString.replaceAll("<[^>]*>", "");
Look into answer for this question.
How to convert unicode in JavaScript?
Basically the response is in Unicode, hence the < is represented by \u003c & > by \u003e you can use String manipulation to replace these strings with appropriate charterers.
Try doing something like this
String parseResponse = response.replaceAll("\u003c","<");
This will get your string in the proper html format.
Related
Can anyone please tell me how to parse the below string?
"Testing the parser - <tag><name>ANKIT</name><id>7</id></tag> <tag><name>VIKRAM</name><id>8</id></tag>. Some random text here"
How can I get the name "ANKIT" which is inside the <tag><name> ?
I tried SAX parser.
I think the XML parsers works only when the starting line is <?xml version="1.0"?>.
Is my understanding correct?
Since your text isn't valid XML but reminds more structure like HTML which isn't as strict, consider using HTML parser like https://jsoup.org/. With this library your code can look like
String myXML = "Testing the parser - <tag><name>ANKIT</name><id>7</id></tag> <tag><name>VIKRAM</name><id>8</id></tag>. Some random text here";
Document doc = Jsoup.parse(myXML, "", Parser.xmlParser());
String tag_name_text = doc.select("tag name")//CSS query to find <name> elements inside <tag> elements
.first()//take first result
.text();//get text it would generate in browser
System.out.println(tag_name_text);
Output: ANKIT
If I understand you right, try
STRINGNAME.substring(STRINGNAME.lastIndexOf(""), STRINGNAME.indexOf(""));
I'm getting a JSON response string similar to this:
<strong>B.<\/strong> Because there is no indication of Miss Manette’s feelings
The string text that I'm receiving is full of tags like <strong>, <em> and ’
“
” etc. How can I parse it to a plain String with same features?
The only way I could think of is replacing such characters and using Html.fromHtml() method. Is there a built-in parser available? How could I parse such HTML text?
Use Html.fromHtml only. It'll parse most of the tags supplied and give you the formatted output. The point to note here is that not all of the HTML tags are supported by this method. Checkout this link for more information about what tags are supported. Also check this, though it's a bit old.
If you know what text you'll be parsing, and you have tags that aren't parsed by fromHtml, your best bet would be to replace them with empty string and then use this method.
Using Jsoup is it possible to remove text characters after whitespace?
For example:
<td> 4.9 ft</td>
Is it possible to remove the "ft" from the result?
Thank you.
Jsoup will not help you with that. However, you can parse the Element(s) into a String, and then replace part of the string with another. An example is below:
String parsedstring = YourElement.text();
String replacedstring = parsedstring.replace(" ft","");
Here's another question that may help you: Android - how to replace part of a string by another string?
Try this:
1) Save the text as String.
2) Get the length of the String, then use the substring method to remove the last two characters.
Here's an example
String result = Element.text();
int resultLength = result.length();
result = result.substring(0, resultLength -2);
Please note: This is a beginner's advice.
I want to grab img tag from text returned from json data like that
#رصد| #انقلاب_3يوليو| اليوم ... مبني المركبات العسكري في صلاح سالم<br /> <br /> تصوير المواطن الصحفي : عبدالرحمن النحاس<br/><br/><img class="img" src="https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-frc3/1239478_598075296936250_1910331324_s.jpg" alt="" />
i want to grab this
<img class="img" src="https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-frc3/1239478_598075296936250_1910331324_s.jpg" alt="" />
what the reqular expression i must use in android to match it
I used this code but it is not working
String content = e.getString("content");
String img = "";
Pattern p = Pattern
.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
Matcher m = p.matcher(content);
if (m.matches()) {
Log.d("true", m.group(0).toString());
img = m.group(0).toString();
}
Log.d("image", "image : " + content);
Using regular expressions to parse HTML is a very bad idea.
Better to use a true HTML parser and walk the DOM tree to get what you want.
You also need to be careful about proper encoding, since you want Arabic text.
Well... you know you can get the JSON object and parse that without regex? that is probably the best approach. Then you can just strip out the content without worrying about parsing anything from a string because it automatically puts it into variables for you.
How to parse JSON
It can become very messy to mess around with regex for the reasons #duffymo posted above me.
edit:
I see what you are trying to do.... parse the image out of the content section correct? There needs to be two things involved here yes.. regular expressions and also json parsing. You need to grab all the content fields from the json parser then use regex on those to extract the images. That's what you are trying to do correct?
Hey, how can I read a value of a cookie?
Example:
String cs = CookieManager.getInstance().getCookie();
System.out.println("Cookies string: "+cs);
This will give me a string which has to be parsed with split on ';' and '='. Is there a "cookie string reader" or smth? Is there any other way of reading the value of only one particular cookie in the webview?
Thx!
Well, I suggest that you parse the string into an Array yourself. That would then be something along these lines in standard Java:
String[] x = Pattern.compile(";").split(CookieManager.getInstance().getCookie());
Now you have an Array of name - value pairs, which you can further parse and then store.