我需要写一段java代码来隐藏VBA7425IWC
至ABV2457CIW
。我需要这方面的帮助。如果我对字符串进行排序,它们会按 ASCII 值排序,数字会变成一大块,字母会变成另一 block 。
最佳答案
这个使用正则表达式。您可能需要调整模式以包含小写字母:
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SplitRegex {
public static void main(String[] args) {
// The search string
String str = "VBA7425IWC";
// The Regular expression (Finds {word} tokens)
Pattern pt = Pattern.compile("[A-Z]+|[0-9]+");
// Match the string with the pattern
Matcher m = pt.matcher(str);
String result = "";
// If results are found
while (m.find()) {
char[] chArray = m.group(0).toCharArray();
Arrays.sort(chArray);
result += new String(chArray);
}
System.out.println(result);
}
}
关于java - 字母数字子串排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41704758/