I am making QR code scan app. When i scan the QR Code for Email, I get the result i.e. given below,
String data = MATMSG:TO:abc#hotmail.com;SUB:email subject;BODY:email body;;
For getting code type i.e. MATMSG in string above i am doing this,
String[] parts = scanData.split(":");
qrCodeType = parts[0];
I want to make 3 strings from the data string like,
String to = abc#hotmail.com;
String subject = email subject;
String body = email body;
How can i use string functions to do this? Notice colons and semicolons in String data
Try the following:
String[] items = data.split(";");
String to = items[0].split(":")[2];
String subject = items[1].split(":")[1];
String body = items[2].split(":")[1];
Hope it helps
Related
In my Android app I have a String[].
If I have 2 of the same Strings, like:
String a = "abc";
String b = "abc";
Can I store both Strings a and b in the same String[]?
Yes, you can add the same string text to two entries in a string array.
String[0] = "abc" and
String[1] = "abc"are perfectly fine
This question already has answers here:
Android getText from EditText field
(6 answers)
Closed 6 years ago.
What went wrong? The data stored in firebase database are all wrong, which are totally different with my inputs in EditTexts.
public void createToUserProfile(){
String firstName = etFirstName.toString().trim();
String lastName = etLastName.toString().trim();
String mobilePhoneNumber = etMobilePhoneNumber.toString().trim();
String iDNumber = etIDNumber.toString().trim();
String year = etYear.toString().trim();
String month = etMonth.toString().trim();
String day = etDay.toString().trim();
String dateOfBirth = year+"/"+month+"/"+day;
String country = etCountry.toString().trim();
String province = etProvince.toString().trim();
String city = etCity.toString().trim();
String postCode = etPostCode.toString().trim();
String address = etAddress.toString().trim();
Firebase ref = new Firebase(Config.USER_URL);
UserP newUser = new UserP();
newUser.setFirstName(firstName);
newUser.setLastName(lastName);
newUser.setMobilePhoneNumber(mobilePhoneNumber);
newUser.setiDNumber(iDNumber);
newUser.setDateOfBirth(dateOfBirth);
newUser.setCountry(country);
newUser.setProvince(province);
newUser.setCity(city);
newUser.setPostCode(postCode);
newUser.setAddress(address);
ref.child("UserP").setValue(newUser);
}
The Firebase Console stored data shows as following:
Data stored wrong in Firebase Database
I recall this same issue from a previous post of yours.
An EditText has a toString() method inherited from View that returns a string dump of the view's properties. You are mistakenly calling that. To get the string the EditText contains, you need to call getText().toString().
I need to extract specfic information of a String. So I have to create two new Strings with the necessary information isolated.
The structure of the String: {line1=necessary information 1, line2=necessary information 2}
As you can see, I need the String values (necessary information 1: after '=' and before ',' and necessary information 2)
This is the String:
String telefonname_nummer = listview.getItemAtPosition((int) position).toString();
Thanks a lot.
You can try something like this:
String yourInfo = "line1=necessary information 1, line2=necessary information 2";
String[] parts = yourInfo.split(",");
String info1 = parts[0].split("=")[1];
String info2 = parts[1].split("=")[1];
try this way may help you
String urString = "line1=necessary information 1, line2=necessary information 2";
// First split your string with ","
String[] splitedString = urString.split(",");
//as u mention in Qestion u want string after "="
//so,
String firstString = splitedString [0].split("=")[1];
String scndString = splitedString [1].split("=")[1];
Log.e("firstString ",firstString );
Log.e("scndString",scndString);
I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);
I have following two string:
String one:
"abcabc/xyzxyz/12345/random_num_09/somthing_random.txt"
String Two:
"abcabc/xyzxyz/12345/"
What i want to do is attach path "random_num_09/somthing_random.txt" from string one two string two. So how can i subtract string two from string one and then attach remaining part to string two.
I have tried to do it by searching for the second last "/" in the string one and then doing sub string and attaching it to string two.
But is there any better way of doing it.
Thanks.
I think the best way is to use substrings, as you said:
String string_one = "abcabc/xyzxyz/12345/random_num_09/somthing_random.txt";
String string_two = "abcabc/xyzxyz/12345/";
String result = string_two + string_one.substring(string_one.indexOf(string_two)+1));
The other possibility is to use regex, but you would still be doing concatenation to get the result.
Pattern p = Pattern.compile(string_two+"(.*)");
Matcher m = p.matcher(string_one);
if (m.matches()) {
String result = string_two+m.group(1);
}
rather that a substring, replace is simpler to use:
String string1 = "abcabc/xyzxyz/12345/random_num_09/somthing_random.txt";
String string2 = "abcabc/xyzxyz/12345/";
String res = string2 + string1.replace(string2, "");