如何在Android中每2个字符后将特殊符号连接为冒号 [英] How to concatenate Special symbol as colon after every 2 character in Android
本文介绍了如何在Android中每2个字符后将特殊符号连接为冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在String中每隔2个字符后连接或附加特殊字符,例如冒号:.
I want to concatenate or append special character as colon : after an every 2 character in String.
例如:原始字符串如下:
For Example: Original String are as follow:
String abc =AABBCCDDEEFF;
在连接或附加冒号之后如下:
After concatenate or append colon are as follow:
String abc =AA:BB:CC:DD:EE:FF;
所以我的问题是我们如何在android中实现这一目标.
So my question is how we can achieve this in android.
先谢谢了.
推荐答案
如果要不使用Math类函数,可以尝试下面的代码.
You can try below code, if you want to do without Math class functions.
StringBuilder stringBuilder = new StringBuilder();
for (int a =0; a < abc.length(); a++) {
stringBuilder.append(abc.charAt(a));
if (a % 2 == 1 && a < abc.length() -1)
stringBuilder.append(":");
}
这里
- a%2 == 1 ** ==>此条件语句用于附加**:"
- a<abc.length()-1 ==>此条件语句用于不添加:"
- a % 2 == 1 ** ==> this conditional statement is used to append **":"
- a < abc.length() -1 ==> this conditional statement is used not to add ":"
在最后一个条目中.希望这是有道理的.如果您发现任何问题,请告诉我.
in last entry. Hope this makes sense. If you found any problem please let me know.
这篇关于如何在Android中每2个字符后将特殊符号连接为冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文