What's the best way to insert a - (dash/minus character) after every 9 characters in a android Edittext, starting from the left?
Examples:
=100001111111111111111111111 -> 100001111-111111111-111111111 My attempt, to show that I have tried doing it myself (a comment below asks: "is this homework?":
final int length = string.length();
StringBuilder stringBuilder = new StringBuilder(newStringCapacity);
您可以执行以下操作:
str = str.replaceAll("(.{9})(?!$)", "$1-");
IF you have a definitive number of characters in your EditText
, I suggest you use MaskedEditText . Sample usage from it's docs:
Usage
Add
xmlns:mask="http://schemas.android.com/apk/res/com.your.app.package"
to your layout xml root. You're now ready to useMaskedEditText
! Just add to your layout xml:
<br.com.sapereaude.maskedEditText.MaskedEditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
mask:mask="###.###.###-##"
/>
mask
element refers to the format you want. For your case, you could use #########-#########-#########
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.