I need to display a string of text with select words in bold. This is for a web application which uses jsf icefaces, but the problem is with the java. The string needs to be passed to the ui already formatted.
It should return a string such as: Hello Universe! Greetings Earthing!
Example code:
public String displayMessage(){
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append("Universe!");
sb.append("Greetings Earthing!");
return sb.toString();
}
Everything I've found so far uses either swing, which I can't use, or is for android, which I'm obviously not using. I've tried html tags, but it obviously doesn't work. I've also tried String.format() with html tags and Font.BOLD, but that doesn't seem to work either.
Help?
Related
If the text is already plain text and passed to the function new HtmlToPlainText().getPlainText() then the new line character is getting added to the result text.
It looks like Jsoup is doing some formatting and adding a line break.
HtmlToPlainText htmlToPlainText = new HtmlToPlainText();
htmlToPlainText.getPlainText(Jsoup.parse(inputString));
I tried outputSettings.prettyPrint(false); but it is not helping.
Input text can be HTML or plain text.
I want the text to be returned as it is(no extra new line) if it is already plain text.
Input: This is the subject for test cnirnv cniornvo cojrpov nmcofrjpv mcprfjv mpcjfpv pvckfpv jvpfkvp cnirv
Output: This is the subject for test cnirnv cniornvo cojrpov nmcofrjpv mcprfjv mpcjfpv \npvckfpv jvpfkvp cnirv.
A new line character is added after mpcjfpv
We can do string replacement but I am looking for a way to do it as part of the library itself.
HtmlToPlainText resides in package org.jsoup.examples, which is not included in the library jar file on Maven Central. In other words, this class is not part of the jsoup API and is only meant for demonstration purposes.
If you want to output the plaintext of a parsed document, try something like this instead:
Document doc = Jsoup.parse("This is the subject for test cnirnv cniornvo cojrpov nmcofrjpv mcprfjv mpcjfpv pvckfpv jvpfkvp cnirv");
System.out.println(doc.text());
I want to send bold text via a bot.
To send it as a normal person you would have to type 2 stars in front and behind the message, but this doesn't work for the bot. I have searched for a solution here but most bots are developed in PHP or Python.
`String a = emoji+"**dump alert**\n";
String b = "Date and time: ";
String c = month+" "+date.format(format1)+" / "+date.format(format2)+"\n";
String d = "Exchange: "+exc;
return a+b+c+d;`
For work with html tags in Text you need ON this options.
You can do it edit this flag:
message.enableHtml(true);
After this you can set bold text use this example:
String text = "<b>Bold text</b>";
When you work with Markdown you should use only one star *bold*
It should work with html tags. So instead of
String a = emoji+"**dump alert**\n";
try using this
String a = emoji+"<b>dump alert</b>\n";
I'm quite new to Android and I'm trying to display some chemical formulas in a textView which is contained in a customListView.
I'm fetching all datas from xml parsing, then I wish to display the formula, such as C₉H₈O₄.
But I can see only 1-4 digits.
I'm converting from "normal" to "subscript" in this way
str = str.replaceAll("0", "\u2080");
str = str.replaceAll("1", "\u2081");
str = str.replaceAll("2", "\u2082");
str = str.replaceAll("3", "\u2083");
str = str.replaceAll("4", "\u2084");
str = str.replaceAll("5", "\u2085");
str = str.replaceAll("6", "\u2086");
str = str.replaceAll("7", "\u2087");
str = str.replaceAll("8", "\u2088");
str = str.replaceAll("9", "\u2089");
str contains the formula fetched from the xml file.
The strange behavior is that I can see in the Logcat the formula as it should be.
I also tried with customs fonts but nothing.
Here are two results:
the first is with normal font, the second with a custom one
https://www.dropbox.com/s/jyk64p700up14db/cella.jpg
https://www.dropbox.com/s/ab9h1b45j2hrods/Schermata%2003-2456370%20alle%2022.05.45.png
Over the web I can read as a solution using something like
setText(Html.fromHtml("X<sub>2</sub>"));
but I really don't know how to use it in my case.
Any suggestion?
It will not be easy trying to solv that problem with Html.fromHtml("X<sub>2</sub>")
you need a lib that can help you to achieve that
(JEuclid is a complete MathML rendering solution, consisting of:) http://jeuclid.sourceforge.net/
Look at the example and you'll get a way to resolve your issue
Other alternatives for rendering math expressions with TeX:
http://jmathtex.sourceforge.net/
http://sourceforge.net/projects/snuggletex/
http://forge.scilab.org/index.php/p/jlatexmath/
Finally I solved the problem: It was a font issue.
I just used Calibri and It works!
I wanted to change width="xyz" , where (xyz) can be any particular value to width="300". I researched on regular expressions and this was the one I am using a syntax with regular expression
String holder = "width=\"340\"";
String replacer="width=\"[0-9]*\"";
theWeb.replaceAll(replacer,holder);
where theWeb is the string
. But this was not getting replaced. Any help would be appreciated.
Your regex is correct. One thing you might be forgetting is that in Java all string methods do not affect the current string - they only return a new string with the appropriate transformation. Try this instead:
String replacement = 'width="340"';
String regex = 'width="[0-9]*"';
String newWeb = theWeb.replaceAll(regex, replacement); // newWeb holds new text
Better use JSoup for manipulating and extracting data, etc. from Html
See this link for more details:
http://jsoup.org/
I'm cleaning some text from unwanted HTML tags (such as <script>) by using
String clean = Jsoup.clean(someInput, Whitelist.basicWithImages());
The problem is that it replaces for instance å with å (which causes troubles for me since it's not "pure xml").
For example
Jsoup.clean("hello å <script></script> world", Whitelist.basicWithImages())
yields
"hello å world"
but I would like
"hello å world"
Is there a simple way to achieve this? (I.e. simpler than converting å back to å in the result.)
You can configure Jsoup's escaping mode: Using EscapeMode.xhtml will give you output w/o entities.
Here's a complete snippet that accepts str as input, and cleans it using Whitelist.simpleText():
// Parse str into a Document
Document doc = Jsoup.parse(str);
// Clean the document.
doc = new Cleaner(Whitelist.simpleText()).clean(doc);
// Adjust escape mode
doc.outputSettings().escapeMode(EscapeMode.xhtml);
// Get back the string of the body.
str = doc.body().html();
There are already feature requests on the website of Jsoup. You can extend source code yourself by adding a new empty Map and a new escaping type. If you don't want to do this you can use StringEscapeUtils from apache commons.
public static String getTextOnlyFromHtmlText(String htmlText){
Document doc = Jsoup.parse( htmlText );
doc.outputSettings().charset("UTF-8");
htmlText = Jsoup.clean( doc.body().html(), Whitelist.simpleText() );
htmlText = StringEscapeUtils.unescapeHtml(htmlText);
return htmlText;
}
Answer from &bmoc is working fine, but you could use a shorter solution :
// Clean html
Jsoup.clean(someInput, "yourBaseUriOrEmpty", Whitelist.simpleText(), new OutputSettings().escapeMode(EscapeMode.xhtml))
A simpler way to do this is
// clean the html
String output = Jsoup.clean(html, Whitelist.basicWithImages());
// Parse string into a document
Document doc = Jsoup.parse(output);
// Adjust escape mode
doc.outputSettings().escapeMode(EscapeMode.xhtml);
// Get back the string
System.out.println(doc.body().html());
I have tested this and it works
The accepted answer is using Jsoup.parse which seems more heavyweight than what is going on in Jsoup.clean after a quick glance at the source.
I copied the source code of Jsoup.clean(...) and added the line to set the escape mode. This should avoid some unecessary steps done by the parse method because it doesn't have to parse a whole html document but just handle a fragment.
private String clean(String html, Whitelist whitelist) {
Document dirty = Jsoup.parseBodyFragment(html, "");
Cleaner cleaner = new Cleaner(whitelist);
Document clean = cleaner.clean(dirty);
clean.outputSettings().escapeMode(EscapeMode.xhtml);
return clean.body().html();
}
Simple way:
EscapeMode em = EscapeMode.xhtml;
em.getMap().clear();
doc.outputSettings().escapeMode(em);
This will remove ALL html entities, including these: ', ", & ,< and >. The EscapeMode.xhtml allows these entities.
Parse the HTML as a Document, then use a Cleaner to clean the document and generate another one, get the outputSettings of the document and set the appropriate charset and the escape mode to xhtml, then transform the document to a String. Not tested, but should work.