字符串操作每 4 个字符插入一个字符
在 Android 中,如果我有一个编辑文本并且用户输入 123456789012,我怎样才能让程序每 4 个字符插入一个破折号。 即:1234-5678-9012?
我想你需要说一些类似的话:- a=字符1~4,b=字符5~8,c=字符9-12
,结果= a + "-" + b + "-" + c 。但我不确定这在 Android 中会是什么样子。
非常感谢您的帮助。
In Android if I have an edit text and the user entered 123456789012, how could I get the program to insert a dash every 4th character. ie: 1234-5678-9012?
I guess you need to say something along the lines of:-a=Characters 1~4, b=Characters 5~8, c=Characters 9-12
, Result = a + "-" + b + "-" + c. But I am unsure of how that would look in Android.
Many thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
substring()
参考substring()
Reference或者使用 StringBuilder 的另一种替代方法,而不是将字符串拆分为多个部分,然后将它们连接起来:
Or another alternative way using a StringBuilder rather than to split the string in multiple parts and then join them :
替代方法:
如果您需要传递长度不是 dashInterval 倍数的字符串,则必须编写一个额外的位来处理它,以防止索引越界废话。
Alternative way:
If you needed to pass strings with lengths that were not multiples of the dashInterval you'd have to write an extra bit to handle that to prevent index out of bounds nonsense.