java - 字母数字子串排序

标签 java sorting alphanumeric substring

我需要写一段java代码来隐藏VBA7425IWCABV2457CIW 。我需要这方面的帮助。如果我对字符串进行排序,它们会按 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/

相关文章:

java - 将捕获的视频存储在特定文件夹中并在视频 View 中播放视频

Java - 为什么这是一个无限循环以及如何修复它?

Java 安卓 android.view.WindowManager$BadTokenException : Unable to add window

WPF:对列表框进行排序

XSLT 按字母顺序和数字顺序排序问题

python正则表达式在数字与非数字相邻时添加空格

java - 在 Jersey 中为 REST API 处理完整和部分 View

arrays - 给定一个有序数组,其中有几个数字是颠倒的。如何排序?

java - 带有比较器 ClassCast 异常的 SortedMap