I'm asking about the right way to concatenate a url with values !
here my example "http://10.0.2.2/myFolder/page.php?value1=!!&value2=!!,+value1,+value2"
I hope you will understand what I'm saying cause I'm new to Android and thank you.
The other answer is almost correct, but you should be URL encoding the keys and values:
String uri = "http://" + address + "/myFolder/page.php?" + URLEncoder.encode(value1) + "=" + URLEncoder.encode(value2);
you can use stringbuilder and append the as many lines and values you want to add
like wise in your case
StringBuilder sb = ("http://").append(address).append("/myfolder/page.php?").append(value1).append("=").append(value2);
and then use the following
sb.toString();
hope this might helps you.I have used this in my one of the projects
Jamal, Are you trying to concatenate the String: "http://10.0.2.2/myFolder/page.php?value1=!!&value2=!!,+value1,+value2" ?
If so, Please try the follwing:
String uri = "http://" + address + "/myFolder/page.php?" + value1 + "=" + value2;
Hope this helps
Related
I am fetching number from contact book and sending it to server. i get number like this (+91)942 80-60 135 but i want result like this +9428060135.+ must be first character of string number.
Given your example you want to replace the prefix with a single + character. You also want to remove other non-numeric characters from the number string. Here's how you can do that:
String number = "(+91)942 80-60 135";
number = "+" + number.replaceAll("\\(\\+\\d+\\)|[^\\d]", "");
The regex matches any prefix (left paren followed by a + followed by one or more digits, followed by a right paren) or any non digit character, and removes them. This is concatenated to a leading + as required. This code will also handle + characters within the number string, e.g. +9428060135+++ and +(+91)9428060135+++.
If you simply wanted to remove any character that is not a digit nor a +, the code would be:
String number = "(+91)942 80-60 135";
number = number.replaceAll("[^\\d+]", "");
but be aware that this will retain the digits in the prefix, which is not the same as your example.
You can use String.replace(oldChar, newChar). Use the code below
String phone = "(+91)942 80-60 135"; // fetched string
String trimmedPhone = phone.replace("(","").replace(")","").replace("-","").trim();
I hope it will work for you.
check this. Pass your string to this function or use as per code goes
String inputString = "(+91)942 80-60 135";
public void removeSpecialCharacter(String inputString) {
String replaced = inputString.replaceAll("[(\\-)]", "");
String finalString = replaced.replaceAll(" ", "");
Log.e("String Output", " " + replaced + " " + second);
}
I want use register in my application and i should send password and verifyCode with SMS to users phones.
But i should read verifyCode from message and set automatically number into verifyCode EditText.
My message format :
Hi, welcome to our service.
your password 12345
your verifyCode 54321
How can i do it? Please help me <3
Assuming that the number of digits are fixed in password and verify codes (Generally they are same as default values), We can extract digits from the string and then find substring which has verify code. This assumption is for simplicity.
String numberOnly= str.replaceAll("[^0-9]", "");
String verifyCode = numberOnly.substring(6);
Here String verifyCode = numberOnly.substring(6); is getting last 5 digits of the string which is your verification code. You can also write numberOnly.substring(6,10); to avoid confusions.
But this is prone to errors like StringIndexOutOfBoundsException, So whenever you want to get substring which is starting from index i till the end of the string, always write numberOnly.substring(i).
There are a lot ways to do this. You can use some complicated regex or use a simple spilt method.
Try this,
String str = "Hi, welcome to our service.\n"
+ "\n"
+ "your password \n"
+ "12345\n"
+ "\n"
+ "your verifyCode \n"
+ "54321";
// Solution #1
String[] parts = str.split("\n");
System.out.println(parts[3]);
System.out.println(parts[6]);
// Solution #2
String PAT = "(password|verifyCode)\\s+(\\d+)";
Pattern pats = Pattern.compile(PAT);
Matcher m = pats.matcher(str);
while (m.find()) {
String grp = m.group(2);
System.out.println(grp);
}
Hello I have a long string and its having html tags like "" and "" and ,I want to get values from this strings,can anyone give me solution how to get values from it?
my string is:
<strong>1 king bed</strong><br /> <b>Entertainment</b> - Wired Internet access and cable channels <br /><b>Food & Drink</b> - Refrigerator, minibar, and coffee/tea maker<br /><b>Bathroom</b> - Shower/tub combination, bathrobes, and slippers<br /><b>Practical</b> - Sofa bed, dining area, and sitting area<br />
my try
int start = description_long.indexOf("Food");
int end = description_long.indexOf("<br />");
String subString = description_long.substring(start,
end);
System.out
.println("===============MY SUB STRING FROM STRING============="
+ start
+ ""
+ "============end======="
+ end + "");
i want to get values of Food & Drink and Bathroom,can any one please tell me how to get these values in a seperate string in android programatically.
Better you could use a regular expression or try to parse.
<a[^>]*>([^<]*)<[^>]*>(.*)
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
Try this but values must end with <br />
String key = "<b>Bathroom</b>"; //<b>Food & Drink</b>
int start = htmlInput.lastIndexOf(key);
String value = htmlInput.substring(start + key.length(), htmlInput.indexOf("<br />", start));
System.out.println(value);
I am working on android. I have a string containing huge data. In that string I want to replace a particular character to another character. I got the index of the character which I want to replace. But I am unable to replace that character.
How can I do that?
String str = "data1data2mdata2test1test2test3dd"
int ind = str.indexOf("m");
System.out.println("the index of m" + ind);
Now in the above string I want to replace the character "m"(after data2) to "#".
Now how can I replace the m to #. Please help me in this reagard.
You can use substring:
String newStr = str.substring(0, ind) + '#' + str.substring(ind + 1);
Try this:
str = str.replaceFirst("m", "#");
It will replace the first m to #
String str1 = "data1data2mdata2test1test2test3dd"
String str = str1.replace("m", "#");
System.out.println(str);
So you are getting 10 as system out,
so this way you can replace it like,
Str.replace('m', '#')--->when you want all occurrences of it to replace it,
Or if you want only first occurrence to be replaced by # then you can do following trick,
StringBuffer buff=new StringBuffer();
buff.append(Str.substring(0,ind)).append("#").append(Str.substring(ind+1));
i hope it would help
I construct a URL with the following bit of code:
String login = rootActivity.getString(R.string.url_authentication);
login = login + "user=" + mySharedPreferences.getString("username", "invalid") + "&" + "key=" + mySharedPreferences.getString("key", "invalid");
login = login.toLowerCase(Locale.US);
System.out.println("Logging in at " + login);
new HttpConnection(handler).get(login);
The URL is valid, as far as I can see visually, but the HttpConnection fails because there's an illegal character in the URL at the index of the ampersand. What really flummoxes me is, the app has between 1,000 and 5,000 installs, and we have a total of two reports of this over the past year – both from American users using Samsung devices, so I doubt it's a character encoding issue.
Don't forget to urlEncode your parameters.
login = login + "user=" + URLEncoder.encode( mySharedPreferences.getString("username", "invalid") ) + "&" + "key=" + URLEncoder.encode( mySharedPreferences.getString("key", "invalid") );
Don't know what login is, but is there a question mark on the end of it? If you're don't something like www.example.comuser="something"&key="somethingElse" then that won't work.
Having a raw & (amplisand) will lead to an error, you should encode your special characters.
Do this:
String login = URLEncoder.encode(login);
http://developer.android.com/reference/java/net/URLEncoder.html#encode(java.lang.String)