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);
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);
}
I have a string
String retail = c.getString(c.getColumnIndex("retail"));
The date is being passed as "99999", I need it to print out as "999.99", how can I do this?
If you always have to add the "." 2 character before the end, this should work:
retail = retail.substring(0, retail.length()-2) + "." + retail.substring(retail.length()-2,retail.length());
This will add a dot two character before the end of the String, as you need.
I want to show number in two digits format anyone please tell me how i can do this using string format
i am doing this but this give me error
String formatted = String.format("%02d", list.get(i).toString());
Use Integer.parseInt():
String formatted = String.format("%02d", Integer.parseInt(list.get(i).toString()));
Your second argument to String.format should be an integer here. Try
String formatted = String.format("%02d", list.get(i));
(Assuming you have a list of integers).
I'm calling Google Maps Intent from my activity with this code found on StackOverflow:
final String uriContent = String.format(Locale.ENGLISH, "http://maps.google.com/maps?q=loc:%s", pCoordinate);
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriContent));
pContext.startActivity(intent);
where pCooriante contains entirely address such as 1.23456,7.8901.
It works well when my phone is using English as its language, but when I change it to French or Vietnamese (which use comma , as its number decimal seperator), it can't work anymore, because the query proceeded by Google Maps look like: 1,000,2,000 (it is shown in the search bar, and after that, a message like Cannot find 1,0000,2,0000 appears), although the exact URI I sent to the intent is 1.000,2.000 (the coordinate is converted to String to prevent Locale problems, and therefore the Locale.ENGLISH in String.format is more or less just abundant).
In short, Uri.parse(uriContent) return exactly the request with the query is 1.000,2.000, but Google Maps itself changed it. However, the code for direction works well with either Locale:
final String uriContent = String.format(Locale.ENGLISH, "google.navigation:q=%s", pCoordinate);
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriContent));
pContext.startActivity(intent);
Is there anyway to prevent the conversion of Google Maps? If I use geo:<coordinate>, it's fine, but I need a marker at that position.
Addional information:
This code final String uriContent = String.format("geo:0,0?q=%s&z=19", pCoordinate); doesn't work too, the periods are converted into commas.
This code final String uriContent = String.format("geo:%s?q=%s&z=19", pCoordinate, pCoordinate); can bring the map center to the coordinate, but still cannot put the marker there, and with the error "Cannot find 'coordinate with periods replaced by commas'"
I am using a temporary solution to this problem, by converting the decimal form of coordinates to degree one. (For example, instead of sending 10.768717,106.651488, I send 10° 46' 7.3812",106° 39' 5.3568"). The conversion is just simple mathematics operation.
However, there was a problem with Java float and double precision, and that was a lot of distance when sending to Google Maps. Therefore I change my input data, convert data using C#'s decimal and my Android app just use it without manupilating anything. Here is the convesion (C#) code:
protected String convertDecimalToDegree(decimal pDecimal)
{
int degree = (int)Math.Floor(pDecimal);
pDecimal -= degree;
pDecimal *= 60;
int minute = (int)Math.Floor(pDecimal);
pDecimal -= minute;
pDecimal *= 60;
return degree + "° " + minute + "\' " + pDecimal + "\"";
}
Usage:
String[] coordinates = shop.MapCoordination.Split(',');
decimal n1 = Decimal.Parse(coordinates[0]);
decimal n2 = Decimal.Parse(coordinates[1]);
shop.MapCoordination = this.convertDecimalToDegree(n1) + "," + this.convertDecimalToDegree(n2);
I will mark this as answer for now, but I appreciate any solution without having to convert to this form.
You can use the following snippet to solve the problem
public String getCoordinates(String coordinates){
if(Locale.FRANCE == Locale.getDefault()){
Pattern p = Pattern.compile(",.*?(,)");
Matcher m = p.matcher(coordinates);
if (m.find( )) {
int index = m.end(); //gets the second comma position
String str1 = coordinates.substring(0,index-1);
String str2 = coordinates.substring(index,coordinates.length());
NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
try {
str1 = nf.parse(str1).toString();
str2 = nf.parse(str2).toString();
} catch (ParseException e) {
e.printStackTrace();
}
return str1+","+str2;
}
}
return coordinates;
}
Updating the Google Maps app to the latest version (7.2.0) seems to fix the issue.
in Xamarin.Android:
using System.Globalization;
Android.Net.Uri.Parse("http://maps.google.com/maps?daddr=" + myLatitude.ToString(CultureInfo.InvariantCulture) + "," + myLongitude.ToString(CultureInfo.InvariantCulture)));