简体   繁体   English

将一系列数字从字符串转换为不同的数字

[英]Converting series of numbers from a string, into different numbers

Pretty much what I need to happen, is I need to take a string 我几乎需要发生的事情是我需要带弦

Example

 String ln = "It's so pretty! �";

The idea, is we're taking this string, a converting the end of the line.. 这个想法是,我们要使用这个字符串,转换行的结尾。

�

into 进入

🀽

I can convert that easy enough, but the problem there can be multiple numbers to change. 我可以很容易地进行转换,但是问题可能是要更改多个数字。 The string could be... 字符串可能是...

 String ln = "It's so pretty! �� But � �";

And I need to convert, every number in that line.. 我需要转换该行中的每个数字。

The purpose of this, is each one of those codes, is unicode for a character, but it's shorthanded, and not correct for the purpose that I need it to be. 这样做的目的是这些代码中的每一个,都是一个字符的unicode,但是它是简写形式,对于我需要的目的而言是不正确的。

  • Every number is in the same format.. and will always be 5 characters long. 每个数字都采用相同的格式..并将始终为5个字符长。
  • Every code, starts with "&#" and ends with a semicolon. 每个代码都以“&#”开头,​​以分号结尾。
  • Every code, can be anywhere in the string 每个代码,可以在字符串中的任何位置

When I tried to create a method to convert the numbers, I pretty much split the string on "&#" and it works, for every case, except for when they aren't connected in series. 当我尝试创建一种转换数字的方法时,我几乎将字符串拆分为“&#”,并且在每种情况下都可以使用,除非它们不是串联连接的。 For example, 例如,

String ln = "Gahh it's so pretty. ������";

It will solve that string, and convert all the numbers, and can recreate the string as it was originally, just with modified numbers. 它将解决该字符串,并转换所有数字,并可以使用修改后的数字重新创建原始字符串。 But it wouldn't recreate a string, with a gap in between number codes. 但是它不会重新创建一个字符串,在数字代码之间留有间隔。


This is the code I have used.. like I said, it converts like its supposed to, just only in one case, and I haven't figured out how to make it work on all cases. 这就是我使用的代码。就像我说的那样,它只能按一种情况进行转换,而我还没有弄清楚如何使它适用于所有情况。

public static void main(String args[]) {
    String ln = "Gahh it's so pretty. ������";
    //71680 + code present
    //Gahh it's so pretty. ������
    String tmp = ln;
    String[] codes = tmp.split("&#");
    System.out.println(ln);
    ArrayList<Integer> ids = new ArrayList<>();

    for (int i = 0; i < codes.length; i++) {
        codes[i] = codes[i].trim();
        if (codes[i].length() != 6)
            continue;
        if (codes[i].endsWith(";")) {
            codes[i] = codes[i].substring(0, codes[i].length()-1);
        }
        try {
            ids.add(Integer.parseInt(codes[i]) + 71680);

        } catch (NumberFormatException e) {
            System.err.println("Error in conversion: " + codes[i]);
        }
    }
    //System.out.println(Arrays.toString(codes));

    codes = tmp.split("&#\\d{5}");
    //System.out.println(Arrays.toString(codes));
    for (int i = 0; i < codes.length; i++) {
        //System.out.println(codes[i]);
        if (codes[i].equals(";")) {
            codes[i] = "&#"+ids.remove(0)+";";

        }
    }
    for (String s : codes)
        System.out.print(s);
    System.out.println();

}

I just need some input on this. 我只需要一些输入。 If you're willing to correct a few things, I'd gladly accept the help, but honestly, I'm more interested in how you would approach this problem. 如果您愿意纠正一些问题,我很乐意接受帮助,但是老实说,我对您如何解决此问题更感兴趣。

You should look into Regular Expressions 您应该研究正则表达式

 Pattern pattern = Pattern.compile("&#(\\d+);");

should work to find all the numbers in your string. 应该可以找到字符串中的所有数字。 The Matcher objects also have methods start() and end() to get the offsets of the matches so you can use those to make substrings to help build up your new String Matcher对象还具有start()end()来获取匹配项的偏移量,因此您可以使用它们来创建子字符串以帮助构建新的String

You can use the look ahead/behind regex: "(?<=&#)(\\\\d+)(?=;)" to match and replace. 您可以使用前向/后向正则表达式: "(?<=&#)(\\\\d+)(?=;)"进行匹配和替换。

    String ln = "Gahh it's so pretty. &#55357;&#56845;&#55357;&#56845;&#55357;&#56874;";

    Pattern patt = Pattern.compile("(?<=&#)(\\d+)(?=;)");
    Matcher mat = patt.matcher(ln);
    StringBuffer buf = new StringBuffer();
    while(mat.find()) {
      mat.appendReplacement(buf, Integer.toString(Integer.parseInt(mat.group(1)) + 71680));
    }
    mat.appendTail(buf);

    System.out.println(buf.toString());

With Regex , using Matcher and Pattern you can construct a pattern as so: 使用Regex ,使用MatcherPattern可以这样构造一个模式:

"&#(\\d{5});\\s?"

This will match your codes that may or may not be followed by a space. 这将与您的代码匹配,该代码可以或可以不带空格。 The numbers of the code are captured into group 1 which you can apply your modifications to. 代码编号被捕获到组1中,您可以对其进行修改。 Then replace each code with the new code like so: 然后将每个代码替换为新代码,如下所示:

public static void main(String[] args) throws Exception {
    List<String> lines = new ArrayList() {{
        add("It's so pretty! &#55357;");
        add("Gahh it's so pretty. &#55357;&#56845;&#55357;&#56845;&#55357;&#56874;");
        add("It's so pretty! &#55357;&#56357; But &#55468; &#55357;");
    }};

    for (String ln : lines) {
        Matcher matcher = Pattern.compile("&#(\\d{5});\\s?").matcher(ln);
        while (matcher.find()) {
            int number = Integer.parseInt(matcher.group(1)) + 71680;
            ln = ln.replace(matcher.group(0), "&#" + number + "; ");
        }
        System.out.println(ln);
    }
}

Results: 结果:

It's so pretty! &#127037; 
Gahh it's so pretty. &#127037; &#128525; &#127037; &#128525; &#127037; &#128554; 
It's so pretty! &#127037; &#128037; But &#127148; &#127037; 

As you can see, spaces are added after each code. 如您所见,在每个代码之后都添加了空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM