Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Quotes in Java

I asked a similar question earlier for Swift, and I'm now facing the same problem in Android/Java.

Is there a way in Java for adding quotation marks to a String? The quotation marks should localize properly (see https://en.wikipedia.org/wiki/Quotation_mark) based on the user's language settings. The text is stored without quotes in a database. I'd like to show the string in a TextViewafter adding the quotes.

For instance:

String quote = "To be or not to be..."
// one or more lines of code that add localized quotation marks
// to the beginning and the end of the string
mMyTextView.setText(stringWithQuotes);

For a French user: «To be or not to be...»

For a German user: „To be or not to be...“

For an English (US) user: “To be or not to be...”

like image 666
jerry Avatar asked Aug 17 '16 19:08

jerry


People also ask

How do you add quotation marks in Java?

However, note that in a Java program, a double quotation mark requires the backslash escape character. Example: -- a single quotation mark is the escape character -- for a single quotation mark VALUES 'Joe''s umbrella' -- in ij, you don't need to escape the double quotation marks VALUES 'He said, "hello!"' n = stmt.

What does 2 quotation marks mean in Java?

For instance, in Java a double-quote is a string while a single-quote is a character. Defining char letter = "a" in Java will cause an error in the same way as String s = 'a bunch of letters' will. Always better to use double-quotes when storing strings.

How do you print quotes in Java?

You can print double quotes in java by escape double quotes using backslash character( \ ). When you print on console using System. out. println, you use double quotes and whatever value inside double quotes is printed.


2 Answers

I've not tried this, but you may be able to use Locale.getDefault. You could create an enum for each country with different quotations and supply that in the enum. Then it would just be a matter of finding the locale that matches an enum and substituting in the specified locale's quotation mark character.

like image 136
Jacob Avatar answered Sep 26 '22 01:09

Jacob


I'd recommend using this Stack Overflow answer to create the text in HTML which has the necessary mechanisms for handling the quotation marks across locales.

Then you can use this Stack Overflow answer to convert the HTML for display in the TextView.

like image 25
Scott Shipp Avatar answered Sep 24 '22 01:09

Scott Shipp