I have a very long string in my Android project, so I split it across multiple lines:
<string name="my_str">
AAAAAAAAAAA
BBBBBBBBBBB
</string>
When I use this string, I get AAAAAAAAAAA BBBBBBBBBBB. Is there a way to get rid of the space in the middle? (I want AAAAAAAAAAABBBBBBBBBBB)
Obviously I could put the entire string on one line, but that's not very maintainable.
Edit: The string (AAA...BBB) can contain spaces; I only want to remove the spaces created by the newline in the XML.
To remove all whitespaces and non-visible characters (e.g. tab, newline,..):
mystring.replaceAll("\\s+", "");
or to remove new-lines
mystring.replaceAll("\n","");
So remove all white spaces from beginning and from end:
myString = myString.replaceAll("^\\s+","").replaceAll("\\s+$","");
Check out the documentation: ^ is for beginning, $ for end. \s means any white space like tabs, spaces, etc.
myString = myString.replaceAll(" ","");
Remove white space in string array
It's very simple to remove from white space in string array.
for example:-
String rahul = 12, 13, 14, 15, 16
Solution :
rahul.trim().replaceAll("\\s","");
output is : 12,13,14,15,16
Related
First of all, I have gone through questions similar to the problem I am facing and those solutions are not working for me.
I have a TextView field on my Android app which is supposed to display multiple paragraphs i.e multiple new lines. I am getting this string from a database present in my online server as a JSON.
The text contains \n in it and I am expecting it to create new lines once it is received by the app. But it displays the whole text without any breaks along with "\n" character.
Below is the text present in my database.
First line. \nSecond line. \nThird line.
JSON string received by me inside the app.
{
"server_response": [{
"news_expand": "First line. \\nSecond line. \\nThird line."
}]
}
Code to extract string from JSON. I have left out the code to get get JSONArray and JSONObject for simplicity.
na_expand = gna_jo.getString("news_expand");
String extracted from the JSON. Got this by printing the na_expand string.
First line. \nSecond line. \nThird line.
Code to display the text in the TextView. Note the below 'na_expand' is an SparseArray present in a different activity hence the 'get(position)' code.
art_expand.setText(na_expand.get(position));
Below is the text I get on the emulator.
First line. \nSecond line. \nThird line.
What am I doing wrong here?
I think you should replace \n with \n in your string before setting test to your textview same below
b= b.replaceAll("\\n","\n");
So I found a workaround to the problem. As I was not sure where the issue was happening with \n, I modified my text present in the database to have a symbol other than \n. For eg: ~
First line.~Second line.~Third line.
You can use a website like this - https://www.gillmeister-software.com/online-tools/text/remove-line-breaks.aspx to replace the line breaks with any symbol you want.
Next, I used the StringSplitter class to break the string received in JSON and then again join it together with \n.
String joined;
String expand_temp = na_expand.get(position);
TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter('~');
splitter.setString(expand_temp);
StringBuilder stringBuilder = new StringBuilder();
for (String s_temp : splitter) {
stringBuilder.append(s_temp + "\n");
}
joined = stringBuilder.toString().trim();
This worked! I used this string in setText.
art_expand.setText(joined);
Try below code
myTextView.setText(Html.fromHtml("yourString with additional html tags"));
It will resolve all the html tags accordingly and effects of the tags will be reflected as well.
NOte: For devices greater than Nougat use below code
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
Hope that helps
The \ character is an escape character in JSON. So, when you get \\n, it actually means \n, not the newline character, which should have been just \n. So what you see is an expected behaviour. The JSON you get should have ideally been:
{
"server_response": [{
"news_expand": "First line. \nSecond line. \nThird line."
}]
}
Get your server to respond properly, otherwise you'll have to strip the unnecessary \.
Do you haveandroid:singleLine="true" on your TextView? If yes it will ignore the \n and will place the text in a single line.
You can just add replaceAll("\\n","\n") when you set value to your art_expand EditText. It should be:
art_expand.setText(na_expand.get(position).replaceAll("\\n","\n"));
I want to put a Font-Awesome icon inside textview text. After I set the text, android shows me a string sequence instead of font-icon.
My code is:
String formatedSection = formatedSection + sections.get(i).getContent() +getResources().getString(R.string.icon_ref);
I define icon_ref in string.xml as below:
<string name="icon_ref"></string>
I followed these instructions to add font-icon. What am I doing wrong?
If you did step 5 in that short guid your problem seem to forget that there is a diff between java and XML.
In XML you use XML escape (&...;), in Java you'll probably have to use Java escape (\u...). – Biffen May 20 at 10:10
Try to hard code the string into "\uf075" and it would work like a charm.
I want to show registered or trademark symbol in android string.I tried using Unicode characters like
® and #reg; also.But it's not working.Can somebody please tell me how to display these symbols.Thanks in advance
Add this line in your TextView code
android:text="\u00AE"
Also try this Ⓡ symbol use ® and trademark ™ symbol ™
Add this line in your strings.xml
<string name="registered_symbol">®</string>
Variant 1 -- just paste the appropriate character
make sure you have the strings.xml in correct encoding: <?xml version="1.0" encoding="utf-8"?>
get the character from e.g. here:
REGISTERED SIGN: http://www.fileformat.info/info/unicode/char/ae/index.htm
TRADE MARK SIGN: http://www.fileformat.info/info/unicode/char/2122/index.htm
Variant 2 -- use html entities
In the linked pages you can also find the corresponding HTML entities ® and ™.
you have to set the text as HTML in your code, like this:
yourTextView.setText(Html.fromHtml(getString(R.string.your_text_entry)));
I hope this helped.
Declare a String in string.xml file like below,
<string name="registered">®</string>
Now you can access it by calling this String variable.
If you want to do it by html then try this, way
String htmlRegistered = "<html><body>My Trade mark is ®</body></html>";
Update from your Comment:
private String[] m_listImmunisations = new String[]
{ "<html><body>Hepatitis B (H-B-Vax® II)</body></html>",
"<html><body>HPV (Cervarix®)</body></html>",
"<html><body>HPV (Gardasil®)</body></html>"
}
Use "\u00AE" to replace that symbol.
To make it on top right corner, you can use SuperscriptSpan like following, replace the startIndex and endIndex with the position of your trademark character. Be careful with the size of the text, you may need some density independent size here probably to make it work on all screens.
Spannable span = new SpannableString(title);
span.setSpan(new TextAppearanceSpan(null, 0, 60, null, null), (int)startIndex, (int)endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new SuperscriptSpan(), (int)startIndex, (int)endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);
I need to split the \n or \t or \s and other spaces of data to a paragraph using regular expression. I got the information from XML and need to display it using paragraph structure.
Any idea?
Try this code:
Code
String str = "foo\t bar\rboo far\nbaz";
String[] parts = str.split("\\s+");
System.out.println(Arrays.asList(parts));
Output
[foo, bar, boo, far, baz]
I know that if you do something like
myTextView.setText("This is on first line \n This is on second line");
Then it will display properly like this:
This is on first line
This is on second line
When I store that string in a database and then set it to the view it displays as such:
This is on first line \n This is on second line
Here is the line of code I use to extract the string from the database:
factView.setText(factsCursor.getString(MyDBAdapter.FACT_COLUMN));
I simply populate the database from a text file where each line is a new entry into the table so a line would look like this "This is on first line \n This is on second line" and it is stored as text.
Is there a reason that it isn't displaying the \n characters properly? It must be something to do with the string being in the database. Any suggestions?
I found this question Austyn Mahoney's answer is correct but here's a little help:
private String unescape(String description) {
return description.replaceAll("\\\\n", "\\\n");
}
description being the string coming out of your SQLite DB
As Falmarri said in his comment, your string is being escaped when it is put into the database. You could try and unescape the string by calling String s = unescape(stringFromDatabase) before you place it in your TextView.
As a side note, make sure you are using DatabaseUtils.sqlEscapeString() on any kind of data that is from the user or an unknown changeable source when inserting data into the database. This will protect you from errors and SQL Injection.
Try \\n instead of \n. If it throws an exception than use newline keyword in place of \n....newline is one character, ascii 10; it's often entered in a string literal...and will serve your purpose....:)
"This is on first line"||x'0A'||"This is on second line"
The || concatenates strings and the x'0A' is an unescaped newline.
If you're inserting records you'll have to replace every newline with "||x'0A'||" (If your string is double quoted). This may seem clumsy compared to the other asnswers. However if your lines are in separate columns this also works in a select:
SELECT firstline||x'0A'||secondline FROM wherever;
I found this while having the same problem you are: http://www.mail-archive.com/sqlite-users#sqlite.org/msg43557.html
A text area can be in multi line or single line mode. When it is in single line mode newline characters '\n' will be treated as spaces. When in doubt, to switch multi line mode on you can use the following code:
setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
I had the problem that the same code did not work on honeycomb and on froyo, which seem to have different defaults. I am now also excluding the flag when I want to force a field to be single lined.
From the Android doc:
public static final int TYPE_TEXT_FLAG_MULTI_LINE Added in API level 3
Flag for TYPE_CLASS_TEXT: multiple lines of text can be entered into
the field. If this flag is not set, the text field will be
constrained to a single line. Constant Value: 131072 (0x00020000)
http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_FLAG_MULTI_LINE
You have to set the flag before you populate the field.