Escape sequence string processing in android - android

I have a string variable in my android ap which contains some url http://www.google.com. The string variable is declared as shown below:
String html="";
But there is an error saying that I have to put (semicolon); after the a href=". But actually I want the entire line content ie
(href="http://www.google.com") in the string variable. How can it be achieved?? Thanks in advance.

doubles quotes needs to escaped.
try below one
String html="<a href='http://www.google.com'></a>";
else
String html= "";

You need to escape every " with \ if you want to put it in one String.
For example:
String html="";
If you don't do this, compiler assumes, that you end this String after href= and tries to understand what http://www.google.com means in Java ;)

Related

How can I add a backspace to a textView Android

I read the oracle escape sequence and realize that if I want to make a backspace in a textView I need to use "\b", the same way as we do for inserting a new line (/n). I've tried this line of code:
textView.setText("Hellos\bWorld");
Then, when I run the app, the textView shows this:
Hellos World
Intead of what I expected:
HelloWorld
I wish you can help me, how I can make a backspace within a textView. Any suggestion will be welcome.
Simplest method remove space in string is replace() method. It accepts two arguments 1st what word/char you want to search in string and 2nd what you want to replace with.
String dummy = "Hellos World";
String newText = dummy.replace("s ","");
textView.setText(newText);
//output > HelloWorld
String regex = "\\s*\\bis\\b\\s*";
String str = "Hellos World";
str = str.replaceAll(regex, "");
textView.setText(str);
\b doesn't work the way you are thinking. Basically,
\b allows you to perform a "whole words only". It matches the empty string at the beginning or end of a word.
So you can match \bword\b with \b. So to remove the character you want you either need to use substring or replace particular character.

How to avoid nul value in URL in android?

I have a strange issue about using Retrofit2 in my android project. I got the issue about the server error since the request is something like that.
https://www.example.com/api/v1/skills?q=Good%00
Since the invalid value "%00" is not acceptable in our server, so it showed error on my activity.
API service
#GET("skills")
Observable<SearchItem> getSkills(#Query("q") String keyword);
In my fragment, I just get the text using following simple statement.
String keyword = editText.getText().toString()
api.getSkills(keyword);
What I want to know is the following:
Is it possible to have a word can be converted to "%00" ?
How to avoid this "Good%00" before I send to getSkills function?
To enable compile time checks on nullity add #NonNull annotation,
#GET("skills")
Observable<SearchItem> getSkills(#NonNull #Query("q") String keyword);
Another way is to change each "%00" in your string, using .replace()
Replace the string with "" if it contains %00
if (text.toString().contains("%00")){
text = text.replace("%00", "");
}
and then call getSkills(text) with updated value
Try this
Use trim()
The java string trim() method eliminates leading and trailing spaces. The unicode value of space character is '\u0020'. The trim() method in java string checks this unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.
SAMPLE CODE
String keyword = editText.getText().toString().trim();
api.getSkills(keyword)
or your can use
replace()
The java string replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence.
SAMPLE CODE
url =url.replace("%00", "");
or You can use URLEncoder
Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding
String encodedurl = URLEncoder.encode(yourURL,"UTF-8");
There is two way to solve this
1. you restrict the entry write below code
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
you can add more thing which you want
String keyword = editText.getText().toString().replace("%00", "");
api.getSkills(keyword);

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.

Using a question mark as a String in android

Im trying to use a question mark as a variable for a string.
I've tried...
strings.xml
<string name="questionMark">\?</string>
.class
String questionMark;
questionMark = getResources().getString(R.string.questionMark);
String delim4 = (questionMark);
This causes a fource close regex error.
and
String delim4 = (\?);
This gets an error Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )
and also
I've tried putting 2 backslashes in front of it
String delim4 =(\\?)
System.out.println("delim "+ delim4);
But that just escapes the second slash and sometimes force closes as well.
the output for that was
delim \?
Can any tell me how to put in the question mark as the string. I'm using it as variable to spit a string. The String Im splitting can not be changed.
plz help
Edit added split code
if (FinishedUrl.contains(questionMark)){
String delim3 = (".com/");
String[] parts3 = FinishedUrl.split(delim3);
String JUNK3= parts3[0];
String fIdStpOne = parts3[1];
String fIdStpTwo = fIdStpOne.replaceAll("=#!/","");
String delim4 = (questionMark);
String[] parts4 = fIdStpTwo.split(delim4);
String fIdStpThree= parts3[0];
String JUNK4 = parts3[1];
FId = fIdStpThree;
}
As pointed out by user laalto, ? is a meta-character in regex. You must work around that.
Let's see what's happening here. Firstly, some ground rules:
`?` is not a special character in Java
`?` is a reserved character in regex
This entails:
String test = "?"; // Valid statement in java, but illegal when used as a regex
String test = "\?"; // Illegal use of escape character
Why is the second statement wrong? Because we are trying to escape a character that isn't special (in Java). Okay, we'll get back to this.
Now, for the split(String) method, we need to escape the ? - it being a meta-character in regex. So, we need \? for the regex.
Coming back to the string, how do we get \?? We need to escape the \(backslash) - not the question mark!
Here's the workflow:
String delim4 = "\\?";
This statement gives us \? - it escapes the \(backslash).
String[] parts4 = fIdStpTwo.split(delim4);
This lets us use \? as a regex in the split() method. Since delim4 is being passed as a regex, \? is used as ?. Here, the prefix \ is used to escape ?.
Your observations:
String delim4 = (\?);
This gets an error Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )
I covered this above. You are escaping ? at the java level - but it isn't a special character and needs no escaping - hence the error.
String delim4 =(\\?)
System.out.println("delim "+ delim4);
But that just escapes the second slash and sometimes force closes as well. the output for that was
delim \?
This is what we want. It is easier to think of this as a two stage process. The first stage deals with successfully placing a \(backslash) in front of the ?. In the second stage, regex finds that the ? has been prefixed by a \ and uses ? as a literal instead of a meta-character.
And here's how you can place the regex in your res/values/strings.xml:
<string name="questionMark">\\?</string>
By the way, there's another option - not something I use on a regular basis these days - split() works just fine.
You can use StringTokenizer which works with delimiters instead of regex. Afaik, any literal can be used as a delimiter. So, you can use ? directly:
StringTokenizer st = new StringTokenizer(stringToSplit, "?");
while (st.hasMoreTokens()) {
// Use tokens
String token = st.nextToken();
}
Easiest way is to quote or backslash them:
<string name="random">"?"</string>
<string name="random">\?</string>
The final code.
String startDelim = ("\\?");
String realDelim = (startDelim);
String[] parts4 = fIdStpOne.split(realDelim);
String fIdStpTwo= parts4[0];
String JUNK4 = parts4[1];
Normally you'd just put it literally, like
String q = "?";
However, you say you're using it to split a string. split() takes a regular expression and ? is a metacharacter in a regex. To escape it, add a backslash in front. Backslash is a special character in Java string literals so it needs to be escaped, too:
String q = "\\?";

Categories

Resources