PatternSyntaxException: String.replaceAll() android - android

I want to remove all { } as follow:
String regex = getData.replaceAll("{", "").replaceAll("}", "");
but force close my app with log.
java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX
what have i done wrong ?

You need to escape {:
String regex = getData.replaceAll("\\{", "").replaceAll("\\}", "");

Curly brackets are used to specify repetition in regex's, therefore you will have to escape them.
Furthermore, you should also consider removing all the brackets in one go, instead of called replaceAll(String, String) twice.
String regex = getData.replaceAll("\\{|\\}", "");

For what you want to do you don't need to use a regex!
You can make use of the replace method instead to match specific chars, which increases readability a bit:
String regex = getData.replace("{", "").replace("}", "");
Escaping the \\{ just to be able to use replaceAll works, but doesn't make sense in your case

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];

Check string for illegal characters in path

Users can choose a own downloadpath for their data and i want the 'path' or 'directory' string to be checked on illegal characters (!##$%^&*, etc) and if possible replace them.
Can somebody help pls?
Thank you in advance.
public void onDownloadLocationChanged(final String newLocation) {
//
final String original = settings.getDownloadsLocation();
//
if (!newLocation.equals(original)) {
//
if (new File (newLocation).isDirectory() && (new File(newLocation).exists())) {
// DoSomething
}}
You could achieve this by using the replaceAll String method. For example, if you wanted to remove the "#" character from your string, you could do the following:
original.replaceAll("[\\#]", "");
The call above will replace all of the occurences of "#" within the string with a "", which essentially means that the character was deleted. Furthermore, you can add any character you wish to remove to the call above. If you wish to use a different replacement for each bad character, then you would need to consider multiple replaceAll statements, but if you simply want to remove illegal characters from your string then this is the way to go.

How to use regex in android

I need help to match a string with a regex. An example of the string is
"Longitude: 34.847368\nLatitude: 30.435345\nAltitiude: 130.34554"
So in this string, the numbers can change, and its possible there is no decimal value.
When I try this code,
Pattern pattern = Pattern.compile("Longitude: -?\\d+(\\.\\d+)?\nLatitude: -?\\d+(\\.\\d+)?\nAltitude: -?\\d+(\\.\\d+)?");
I get an error saying \. is an invalid escape sequence, can any one help?
You have to use a double slash, otherwise Java sees it as a String escape sequence, not a Regex escape sequence. Try this:
Pattern pattern = Pattern.compile("Longitude: \\d+(\\.\\d+)?\nLatitude: \\d+(\\.\\d+)?\nAltitude: \\d+(\\.\\d+)?");
Soxxeh and aroth are almost definitly right, but in future, maybe this will help:
http://gskinner.com/RegExr/
I use it all the time :D

Categories

Resources