1.随机位置随机长度的用指定字符替换原来字符串中的字符
2.指定字符串指定长度末尾替换
eg: 1.encryptionStr("123456", 3, true, 2, "*") -->12345*6 or 1234**6 or 12345*
2.encryptionStr("123456", 3, false, 2, "*") -->12345** 演示代码:- /**
- 02 * @param str
- 03 * 要处理的str
- 04 * @param keepStart
- 05 * 要保留的字符串
- 06 * @param flag
- 07 * true-->随机位置随机替换 ,false-->尾部替换len个changeStr
- 08 * @param num
- 09 * changeStr在str结果中的个数
- 10 * @param changeStr
- 11 * 替换使用changeStr,eg:"*","$","%" ....
- 12 * @return
- 13 */
- 14 public static String encryptionStr(String str, int keepStart, boolean flag,
- 15 int num, String changeStr) {
- 16 int lenAll = str.length();
- 17 int strLen = lenAll - keepStart;
- 18 if (keepStart > str.length()
- 19 || str.length() - keepStart < changeStr.length())
- 20 return str;
- 21 String tempStr = str.substring(keepStart, str.length());
- 22 String pz = str.substring(0, keepStart);
- 23 String result = "";
- 24 Random r = new Random();
- 25 if (flag) {
- 26 num = r.nextInt(strLen - 1) + 1;
- 27 int tempLen = r.nextInt(strLen);
- 28 if (strLen - tempLen <= num) {
- 29 pz = str.substring(0, pz.length() + tempLen);
- 30 for (int i = 0; i < num && pz.length() < strLen + keepStart; i++) {
- 31 pz += changeStr;
- 32 }
- 33 return pz;
- 34 }
- 35 result = tempStr.substring(0, tempLen);
- 36 for (int i = 0; i < num && pz.length() < strLen + keepStart; i++) {
- 37 result += changeStr;
- 38 }
- 39
- 40 result += tempStr.substring(tempLen + num, strLen);
- 41 pz += result;
- 42 } else {
- 43 pz += str.substring(pz.length(), lenAll - num);
- 44 for (int i = 0; i < num; i++) {
- 45 pz += changeStr;
- 46 }
- 47 }
- 48 return pz;
- 49 }
复制代码 |