I have an app which contain mobile number edit text in which user can edit mobile number and I have to send two request to server like:- mobile number and mssdn,mobile number(which is full lenghth ) and mssdn(which contain mobile number last 4 digit).How can I do that
Try this. Check for length greater than 4 before calling subString to avoid IndexOutOfBounds Exception.
EditText mEdtPhoneNumber = (EditText) findViewById(R.id.edtPhoneNumber);
String phoneNumber = mEdtPhoneNumber.getText().toString().trim();
String strLastFourDi = phoneNumber.length() >= 4 ? phoneNumber.substring(phoneNumber.length() - 4): "";
Also what is mssdn?? Is it msisdn??
Use the modulus (%) operator:
To get the last digit: use number % 10
To get the last 2 digits: use number % 100
and so on
For example:
42455%10000 = 2455
You could do something like this:
EditText phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);
String phoneNumber = phoneNumberEditText.getText().toString();
String lastFourDigits = phoneNumber.substring(phoneNumber.length() - 4);
you should use regex because this will only give you result if the last four letters are actually numbers on the other hand the substring function simply give you last four letters no matter they are numbers or characters. e.g 4344sdsdss4 will give you dss4 which is clearly not a part of phone number
String str="4444ntrjntkr555566";
Pattern p = Pattern.compile("(\\d{4})$");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group(m.groupCount()));
}
this will produce 5566
Working
//d mean digits
{4} for fix length as 4
$ mean at the end
List<Integer> f(String str){
ArrayList<Integer> digits = new ArrayList<>();
if (null == str || str.length() < 4){
Log.i(LOG_TAG, "there are less than 4 digits");
return digits;
}
String digitsStr = str.substring(str.length() - 4);
for (char c : digitsStr.toCharArray()){
try {
Integer digit = Integer.parseInt(String.valueOf(c));
digits.add(digit);
} catch (Exception e){
continue;
}
}
return digits;
}
We can also use a new method introduced in kotlin: takeLast(n)
fun getLastDigit(data: String, n:Int): String {
return if(data.length > n){
data.takeLast(n)
}else {
""
}
}
Related
I'm stuck on how to make it so that i can space characters such as, 1 000, 10 000, 100 000, 10 000 000. As long as there is more than three characters, i need it to space the last three digits from the rest. If there are more than 6 characters, i need it to space two times, in the middle of the three last digits and before. I need this to be a mask for edittext so it formats it while being input. I was considering textwatcher, but was thinking if there was a better way to do it with regex. As a bonus, if there was a way to prefix '$' (dollar sign), that would be helpful aswell, but not necessary.
Any help is greatly appreciated :)
EDIT: Got it working with this:
StringBuilder ss;
ss = new StringBuilder(text);
for(int i = ss.length(); i > 0; i -= 3){
ss.insert(i, " ");
}
return ss.toString();
You'll need to use a TextWatcher to achieve your purpose.Here is a simple way you could achieve the spaces:
StringBuilder s;
s = new StringBuilder(yourTxtView.getText().toString());
for(int i = 3; i < s.length(); i += 4){
s.insert(i, " ");
}
yourTxtView.setText(s.toString());
Kotlin:
private fun String.formatter(): String {
val s = StringBuilder(this.reversed())
s.forEachIndexed { index, _ ->
if (index%4==0) s.insert(index," ")
}
return s.reversed().toString()
}
now you can call it this way:
val s = "234567846587678"
s.formatter() // => 234 567 846 587 678
I am working on an Android app that gets a time (duration) value as string.
For example, the app can get a value like: 6 hours 43 mins
or a value like: 15 mins
I am looking for a way to convert this strings to an integer value in minutes.
I have tried using this function, but I canĀ“t extract the needed values:
str = str.replaceAll("[^\\d.]", "");
Edit:
it could be possible a result like 6 hours, the only known condition is that minutes are always rounded to an integer, the minimum value is 1 min
Using a Regex to get each couple numeric/time_unit. You can easily parse that with :
(\\d+) (\\w+)
Group 1 : numeric value
Group 2 : time unit
(note that I used a space between the two (could be optional if you want)
Using Java Pattern class to use that regex
Pattern p = Pattern.compile("(\\d+) (\\w+)");
Then you just have to iterate on each match to get the couple type/value
Matcher m = p.matcher(s);
while(m.find()){
String type = m.group(2);
int value = Integer.parseInt(m.group(1))
...
}
From that, just use a switch to convert the number into minute and add it to the variable, omitting the break to have a nice (but not efficient) converter :
switch(type){
//Add more time unit in the correct order if needed
case "days":
value *= 24;
case "hours":
value *= 60;
case "mins":
mins += value;
break;
}
Each type will convert the value into a correct number of minute, at the end, you will have the result in minutes.
Problem: There are few concerns as you never know without putting extra conditions like:
15 hours and 15 mins both will be stored in same integer value , you eventually need to differentiate them on some conditions to cater all the issues.
Coming to the question, you may achieve all this by using String split cases but you need to manually cater all the cases keeping in mind a user can use any spell words like hours can be hrs and so on
You could split the String at the whitespace and use the values in the array.
String value1 = "6 hours 43 mins";
String value2 = "15 mins";
String[] resultList1 = value1.split(" ");
String[] resultList2 = value2.split(" ");
int minutes1 = 0;
int minutes2 = 0;
if(resultList1.length == 4) {
minutes1 = Integer.parseInt(resultList1[0]) * 60 + Integer.parseInt(resultList1[2]);
} else {
minutes1 = Integer.parseInt(resultList1[0]);
}
if(resultList2.length == 4) {
minutes2 = Integer.parseInt(resultList2[0]) * 60 + Integer.parseInt(resultList2[2]);
} else {
minutes2 = Integer.parseInt(resultList2[0]);
}
System.out.println(minutes1);
System.out.println(minutes2);
The result is:
403
15
Either String split() or Pattern Matcher, as earlier answers suggest, will work. I'm not sure which will be more efficient though, but it's probably irrelevant in this case. My version:
String timeStr = "2 hours 15 mins";
String[] parts = timeStr.split(" ");
int totalMins = 0;
for(int i=1; i< parts.length; i+=2) {
// Add checking for "days", etc., if necessary.
if(parts[i].equals("hours")) {
int h = Integer.parseInt(parts[i-1]);
totalMins += 60 * h;
} else if(parts[i].equals("mins")) {
int m = Integer.parseInt(parts[i-1]);
totalMins += m;
}
}
System.out.println("totalMins = " + totalMins);
>> totalMins = 135
If you can get each minute and hours separately you can use string.replace("mins", "") then use Integer.parseInt().
If you get overral like 6 hours 43 mins you must split the string.
I am not sure whether this can be done in a single regex, but if I were you I would use a different regex to find the number of hours, the number of minutes, the number of seconds, etc.
Given a string in the format you mentioned, you can first extract the number of hours by using this regex:
\d+(?= hours?)
Then extract the number of minutes:
\d+(?= mins?)
If seconds can appear in the input string, you can use this to extract seconds as well:
\d+(?= secs?)
If any of the regexes don't match, that means there isn't that information in the string.
String time = "6 hours 43 mins";//or (43 mins) or (6 hours)
int h, m;
String[] parts = time.split(" ");
if (parts.length == 4) {
h = parts[1];
m = parts[3];
} else if (parts.length == 2) {
if (parts[1].isEqualTo("hours") {
h = parts[0];
} else if (parts[1].isEqualTo("mins") {
m = parts[0];
}
}
return h*60+m;
i am about to create a validation for phone number format..The format is 10 digit including the plus sign eg:+0133999504. Even though I have declare the pattern which is I try to disallow the "-" symbol or any other characters, but the validation is not working. Any other Idea or solution?
1st I declared the string regex:
String PhoneNo;
String PhoneNo_PATTERN ="[\\+]\\d{3}\\d{7}";
2nd I make a if..else statement:
{
Pattern pattern = Pattern.compile(PhoneNo_PATTERN);
Matcher matcher = pattern.matcher(PhoneNo);
if (!matcher.matches())
{
inputemergencyContactNo.setError("Please enter Emergency Contact No");
}
else{
Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
}
Why not remove all non-digits and then count the digits left and put the plus back in later? This allows users the freedom to fill out their phone number anyway they want...
String PhoneNo = "+123-456 7890";
String Regex = "[^\\d]";
String PhoneDigits = PhoneNo.replaceAll(Regex, "");
if (PhoneDigits.length()!=10)
{
// error message
}
else
{
PhoneNo = "+";
PhoneNo = PhoneNo.concat(PhoneDigits); // adding the plus sign
// validation successful
}
If your app is intended for international use replace
if (!PhoneDigits.length()!=10)
with
if(PhoneDigits.length() < 6 || PhoneDigits.length() > 13)
as Fatti Khan suggested.
To apply this in the code you posted at Android EditText Validation and Regex first include this method in your public class or the class containing onClick():
public boolean validateNumber(String S) {
String Regex = "[^\\d]";
String PhoneDigits = S.replaceAll(Regex, "");
return (PhoneDigits.length()!=10);
}
And include this method in the CreateNewRider class:
protected String tidyNumber(String S) {
String Regex = "[^\\d]";
String PhoneDigits = S.replaceAll(Regex, "");
String Plus = "+";
return Plus.concat(PhoneDigits);
}
This is where the validation happens...
#Override
public void onClick(View view) {
Boolean b = false;
if(inputfullname.getText().toString().equals("")) b = true;
else if(... // do this for all fields
else if(inputmobileNo.getText().toString().equals("")) b=true;
else if(inputemergencyContactNo.getText().toString().equals("")) b=true;
else {
if(validateNumber( inputmobileNo.getText().toString() )
Toast.makeText(RiderProfile.this, "Invalid mobile number", Toast.LENGTH_SHORT).show();
else if(validateNumber( inputemergencyContactNo.getText().toString() )
Toast.makeText(RiderProfile.this, "Invalid emergency contact number", Toast.LENGTH_SHORT).show();
else {
// Validation succesful
new CreateNewRider().execute();
}
}
if(b) Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
}
And then use tidyNumber() in the CreateNewRider class:
protected String doInBackground(String... args) {
String fullname= inputfullname.getText().toString();
String IC= inputIC.getText().toString();
String mobileNo= tidyNumber( inputmobileNo.getText().toString() );
String emergencyContactName= inputemergencyContactName.getText().toString() );
String emergencyContactNo= tidyNumber( inputemergencyContactNo.getText().toString() );
...
Given the rules you specified:
upto length 13 and including character + infront.
(and also incorporating the min length of 10 in your code)
You're going to want a regex that looks like this:
^\+[0-9]{10,13}$
With the min and max lengths encoded in the regex, you can drop those conditions from your if() block.
Off topic: I'd suggest that a range of 10 - 13 is too limiting for an international phone number field; you're almost certain to find valid numbers that are both longer and shorter than this. I'd suggest a range of 8 - 20 to be safe.
[EDIT] OP states the above regex doesn't work due to the escape sequence. Not sure why, but an alternative would be:
^[+][0-9]{10,13}$
[EDIT 2] OP now adds that the + sign should be optional. In this case, the regex needs a question mark after the +, so the example above would now look like this:
^[+]?[0-9]{10,13}$
For Valid Mobile You need to consider 7 digit to 13 digit because some country have 7 digit mobile number . Also we can not check like mobile number must starts with 9 or 8 or anything..
For mobile number I used this this Function
private boolean isValidMobile(String phone2)
{
boolean check;
if(phone2.length() < 6 || phone2.length() > 13)
{
check = false;
txtPhone.setError("Not Valid Number");
}
else
{
check = true;
}
return check;
}
^[\\+]\\d{3}\\d{7}$
Use anchors to limit the match.
^ => start of match
$=> end of match
To validate India's mobile number.
Your edit text input
edt_mobile.text.toString().trim()
Number validation method
fun isValidMobile(phone: String): Boolean {
return phone.matches(Constants.REGEX_MOBILE.toRegex()) && phone.trim().length == 10
}
Regression expression
const val REGEX_MOBILE = "^[6-9]{1}[0-9]{9}\$"
I used the following code snippet to sort by phone number:
class Item { String addr; /* phone number */ }
private int compareByAddr(Item objA, Item objB) {
if (objA.addr==null && objB.addr==null) {
return 0;
} else if (objA.addr==null && objB.addr!=null) {
return -1;
} else if (objA.addr!=null && objB.addr==null) {
return 1;
} else {
if (PhoneNumberUtils.compare(objA.addr, objB.addr)) {
return 0;
} // end if
return objA.addr.compareTo(objB.addr);
} // end if
} // end compareByAddr()
However I got an Exception:
E/AndroidRuntime(12157): java.lang.IllegalArgumentException:
Comparison method violates its general contract!
I've searched about it, and found out that it means my sorting algorithm is not transitive...
Does anyone has a better algorithm for sorting by phone number?
Problem is you have 2 String's which represent phone numbers and you want to compare them and sort them from big to small like there were numbers...
but comparing 2 String to each-other (like in your code snippet):
objA.addr.compareTo(objB.addr);
wont work :(
you can do this by manipulating the String to a number (and discarding all non digit from it)
and then compare the 2 like they were regular numbers ...
String phone1Str
String phone2Str
int phone1
int phone2
phone1Str= phone1Str.replaceAll("\\D+",""); //using reg-ex to get only digits
phone1= Integer.valueOf(phone1Str); //convert to int to compare numbers
phone2Str= phone2Str.replaceAll("\\D+","");
phone2= Integer.valueOf(phone2Str);
if (PhoneNumberUtils.compare(phone1Str,phone2Str)
//they are equal
else if (phone1>phone2)
//phone1 is the larger number !
else
//phone2 is the larger number !
hope this helps
I cannot get a string broken into fixed length chunks and added to an ArrayList ... the code below iterates through as expected, but all the messageToSplit[] upto the last one are null. The last one actually has a value in it.
In the example below if the edit text returned, "01234567890" then "", "" and "890".
Pattern p = Pattern.compile(".{4}");
ArrayList<String> myText = new ArrayList<String>();
String[] messageToSplit = TextUtils.split(myStringEditText.getText().toString(), p);
int x = 0;
while(x <= (myStringEditText.getText().toString().length() / 4)) {
Toast.makeText(getBaseContext(), x+": '" + messageToSplit[x] + "'", Toast.LENGTH_SHORT).show();
myText.add(messageToSplit[x]);
x++;
}
In a split operation, the regex pattern is the separator. For example, if the regex pattern were ;, then 12;34;56 would be split into 12, 34, and 56.
So in your case 01234567890 is split into "" (the string before 0123), "" (the string between 0123 and 4567) and 890 (the remainder of the string after 4567).
You probably don't want to use split but rather something like this:
Pattern p = Pattern.compile(".{1,4}");
Matcher regexMatcher = p.matcher(messageToSplit);
while (regexMatcher.find()) {
myText.add(regexMatcher.group());
}
.{1,4} will match 4 characters if it can, but make do with 1-3 if four are no longer available (which might happen at the end of the string if its length is not a multiple of 4).