Created
May 17, 2017 08:12
-
-
Save interchen/4bc1f3073b2572a65cbf57b92f3114c6 to your computer and use it in GitHub Desktop.
encrypt some chars of a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String encryptString(String str, String encryptStr) { | |
String resultStr; | |
int startIndex = indexOfNormalText(str, false); | |
int endIndex = indexOfNormalText(str, true); | |
if (startIndex == NOT_FOUND) { | |
resultStr = str + encryptStr; | |
} else { | |
if (str.length() == 1) { | |
resultStr = str + encryptStr; | |
} else { | |
String firstChar, lastChar; | |
// first char | |
if (startIndex == 0) { | |
firstChar = str.substring(0, 1); | |
} else { | |
firstChar = str.substring(0, startIndex); | |
} | |
// last char | |
if (endIndex == str.length() - 1) { | |
lastChar = str.substring(endIndex, str.length()); | |
} else { | |
lastChar = str.substring(endIndex + 1, str.length()); | |
} | |
resultStr = firstChar + encryptStr + lastChar; | |
} | |
} | |
return resultStr; | |
} | |
private static final int NOT_FOUND = Integer.MAX_VALUE; | |
public static int indexOfNormalText(String originalString, boolean reverse) { | |
int index; | |
boolean found = false; | |
if (!reverse) { | |
for (index = 0; index < originalString.length(); index++) { | |
String subString = String.valueOf(originalString.charAt(index)); | |
if (!subString.matches("[\ud000-\ue000]")) { | |
found = true; | |
break; | |
} | |
} | |
if (found) { | |
return index; | |
} else { | |
return NOT_FOUND; | |
} | |
} else { | |
for (index = originalString.length() - 1; index >= 0; index--) { | |
String subString = String.valueOf(originalString.charAt(index)); | |
if (!subString.matches("[\ud000-\ue000]")) { | |
found = true; | |
break; | |
} | |
} | |
if (found) { | |
return index; | |
} else { | |
return NOT_FOUND; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment