Using Jsoup is it possible to remove text characters after white space? - android

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.

Related

Add new line character in remote config text

I'm doing Firebase RemoteConfig integration. In one of the scenarios, I need to break a text line, so I tried to use new line character (\n).
But this is not working, it is neither displaying as an extra character nor creating another line.
My solution is replace \n manually (assuming that in Firebase Console you put property for TITLE as "Title\nNewLine"):
FirebaseRemoteConfig.getInstance().getString(TITLE).replace("\\n", "\n")
Try using an uncommon character like two pipes || and then replacing every occurance of those with a newline after you do getString() in the code.
You can insert encoded text(with Base64) to Firebase panel.
After, decode the String from your Java class and use it.
Like
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
The trick (which actually works for all HTML tags supported on your target platform) is to wrap the String in a JSON Object on RemoteConfig, like so:
{
"text":"Your text with linebreaks...<br><br>...as well as <b>bold</b> and <i>italic</i> text.
}
On the target platform you then need to parse the JSON and convert it back to a simple string. On Android this looks like this:
// extract value from JSON
val text = JSONObject(remoteConfig.getString("remoteConfig_key")).getString("text")
// create Spanned and use it
view.text = HtmlCompat.fromHtml(text)
So what worked for me is to use "||" (or some other character combination you are confident will not be in the string) as the new line character. Then replace "||" with "\n". This string will then display properly for me.
For some reason sending "\n" in the string doesn't get recognized as expected but adding it manually on the receiving side seems to work.
To make the suggestion mentioned above, you can try this code(that can be generalized to "n" number of elements). Simply replace the sample text with yours with the same format and add the amount of elements
String text="#Elemento1#Elemento2#Elemento3#";
int cantElementos=3;
arrayElementosFinales= new String[cantElementos];
int posicionNum0=0;
int posicionNum1;
int posicionNum2;
for(int i=0;i<cantElementos;i++){
posicionNum1=text.indexOf("#",posicionNum0);
posicionNum2=text.indexOf("#", posicionNum1+1);
char [] m = new char[posicionNum2-posicionNum1-1];
text.getChars(posicionNum1+1, posicionNum2,m,0);
arrayElementosFinales[i]=String.valueOf(m);
posicionNum0=posicionNum2;
}
Use Cdata in the remote config in combination with "br" tag and HTML.fromHtml() .. for eg.
<![CDATA[ line 1<br/>line 2]]>

is it possible to cut text from string like this?

I want to put extra value from intent to other intent. But in other intent, app get all value. Example:
mAddress.setText(" from " + address);
String put_address = mAddress.getText().toString();
editIntent.putExtra("put_address", put_address);
is it possible to cut text "from" and get only address variable ???
you can split a string like
str = "From address#dd.com";
String modified = str.replace;
now splitstr contain your split strings
splitStr[1] contains "address#dd.com"
Can also use
str.substring(str.indexOf(" ")+1);
By the way, you can use jagapathi's answer. In his example he uses regular expression.
Regular expressions can help to parse, find, cut substrings using a particular pattern. In his code he splits string by any space character.
But, imho, the simplest solution is to create a substring using this code:
'put_address.substring(7);'
use one of these solutions:
String input = put_address.trim().substring(5);
*** note: 5 is index of real address first character;
String input = put_address..split(" ")[1];

how to find and replace character in textview in android?

I use this code for replace this • character with \n in textview in android
TextView tvcontent=(TextView) row.findViewById(R.id.row_comment_content);
tvcontent.setText(content[position].replace("•", "\n"));
Now I want to replace this char in the image http://i.stack.imgur.com/cnJeI.jpg
but I don't know what is the ASCII code of that char in the image to replace in android.
If you know what is ASCII code of the char please help.
If you mean a middot char, its code is 183.
Also, maybe this link can help.
Ok, now I got what you want.
This page would answer your question. Brief extract from this page:
this is OBJECT REPLACEMENT CHARACTER and it's code in Java is \uFFFC.
Also I typed it in Android Studio - it works (screen below):
If you possibly need it in HTML - it's code is  ().
And don't forget to use single quote when you will replace this char!
Use this code:
String a = "your content with dots";
String b = a.replace("dots", "\n");
textview.settext(b);
Try running this code to find the ascii value of any character. But for this bullet I don't think there is any ascii code.
public class HelloWorld{
public static void main(String []args){
String s= "•";
int a = s.charAt(0);
System.out.println("Ascii code is "+ a);
}
}
Here when we run this code then compiler shows an error:
unmapped character for encoding ascii.
But in future if you want to know ascii code of any character that is actually ascii, just use this function.

Taking first and last characters from string variable and inserting xxx in between them in android

I have a value in a string variable. I need to take first and last letters from the string variable. Append it with xxx in between them. For example, if the string variable value is "googleuser", then i should get the output as "gxxxr". How is this made? I tried many ways, suggested by google, but still didn't find anything which is helpful to me. Can some one suggest me a way for this. I tried the charat(index) function but it returned wrong results.
You could do that using the StringBuilder:
StringBuilder s = new StringBuilder();
s.append(text.charAt(0));
s.append("xxx");
s.append(text.charAt(text.length -1));
s.toString();
You could use regexp
String s = "aaa";
s.repalceAll("(.){1}(.)+(.){1}", "$1xxx$3");

How do i parse and format Google Directions JSON?

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.

Categories

Resources