I want to check if there is a space or not between letters and add a space if there is or not, remove if there are to many. Is there an easy way to achieve this?
What I am currently doing checking for postcode however I want to add spaces if there is no spaces e.g. M11SS make it M1 1SS and delete starting spaces and ending spaces
if (a.isEmpty(txtJobPostcode)) {
Toast.makeText(getContext(), "Please enter a postcode", Toast.LENGTH_LONG).show();
return;
}
if(txtJobPostcode.getText().toString().matches("^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$")){
Log.d("NOTICE", String.valueOf(txtJobPostcode));
}
else {
Toast.makeText(getContext(), "Please enter a valid postcode", Toast.LENGTH_LONG).show();
return;
}
Try to consider: TextWatcher
class in Android sdk.
You can find the documentation by the link:
https://developer.android.com/reference/android/text/TextWatcher
Also you can try some libs(solution from the box) to save your time: