簡體   English   中英

將一系列數字從字符串轉換為不同的數字

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

我幾乎需要發生的事情是我需要帶弦

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

這個想法是,我們要使用這個字符串,轉換行的結尾。

�

進入

🀽

我可以很容易地進行轉換,但是問題可能是要更改多個數字。 字符串可能是...

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

我需要轉換該行中的每個數字。

這樣做的目的是這些代碼中的每一個,都是一個字符的unicode,但是它是簡寫形式,對於我需要的目的而言是不正確的。

  • 每個數字都采用相同的格式..並將始終為5個字符長。
  • 每個代碼都以“&#”開頭,​​以分號結尾。
  • 每個代碼,可以在字符串中的任何位置

當我嘗試創建一種轉換數字的方法時,我幾乎將字符串拆分為“&#”,並且在每種情況下都可以使用,除非它們不是串聯連接的。 例如,

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

它將解決該字符串,並轉換所有數字,並可以使用修改后的數字重新創建原始字符串。 但是它不會重新創建一個字符串,在數字代碼之間留有間隔。


這就是我使用的代碼。就像我說的那樣,它只能按一種情況進行轉換,而我還沒有弄清楚如何使它適用於所有情況。

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();

}

我只需要一些輸入。 如果您願意糾正一些問題,我很樂意接受幫助,但是老實說,我對您如何解決此問題更感興趣。

您應該研究正則表達式

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

應該可以找到字符串中的所有數字。 Matcher對象還具有start()end()來獲取匹配項的偏移量,因此您可以使用它們來創建子字符串以幫助構建新的String

您可以使用前向/后向正則表達式: "(?<=&#)(\\\\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());

使用Regex ,使用MatcherPattern可以這樣構造一個模式:

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

這將與您的代碼匹配,該代碼可以或可以不帶空格。 代碼編號被捕獲到組1中,您可以對其進行修改。 然后將每個代碼替換為新代碼,如下所示:

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);
    }
}

結果:

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; 

如您所見,在每個代碼之后都添加了空格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM