【问题标题】:Android - How can I replace the number in this type of string?Android - 如何替换此类字符串中的数字?
【发布时间】:2016-04-10 10:41:49
【问题描述】:

有谁知道如何替换此类字符串中的数字并删除第二部分(如果存在)?

我知道这听起来很奇怪,让我解释一下。

每次处理的字符串都不一样:

数字(我需要替换的)可以从1 数字到10 数字,从:0 到:9999999999(也可以是零0000000000)。

数字前总是有一个字符(可以是任何字符,任何大小写),例如:X0000a00h000000G000

有时后面可能会有其他部分(以连字符开头,如果存在,我需要删除这部分),例如:X000-X00000X0000-X00X00-X0

有时可能会有另一个额外的字符(可以是任何字符,任何情况下,以连字符开头,如果存在,我需要保留这部分),例如:X00000-XX0000-X000-Xa000-h、@ 987654338@.

我不知道如何替换第一部分(如果存在),删除第二部分(如果存在)并保留最后一部分(如果存在),这是我需要的示例:

X0000 > X1234
a00 > a12
h000000 > h123456
G000 > G123

X000-X000000 > X123  -  replace the first and delete the last
X0000-X000 > X1234 -  replace the first and delete the last
X00-X00 > X12 -  replace the first and delete the last

X00000-X > X12345-X -  replace only the first and keep the last
a000-h > a123-h -  replace it and keep the last

X0000-X000-X > X1234-X -  replace the first, delete the second and keep the last
g00000-j00-Y > g12345-Y -  replace the first, delete the second and keep the last

在这个例子中,我主要使用 0 和 X,但就像解释的那样,可以是 0 或任何数字,也可以是 X 或任何字符(大写或小写)。

编辑:忘了提,我需要得到那个数字,用它做数学运算然后替换它,不仅仅是替换。

有人知道怎么做吗?非常感谢。

【问题讨论】:

标签: java android string android-studio str-replace


【解决方案1】:

一种方法是使用replace()substring() 来做到这一点,如下所示:

public static void main (String[] args) throws Exception {
    String input = "X000-X";
    String replacement = "123";
    int ls = input.lastIndexOf("-");
    int fs = input.indexOf("-");
    System.out.println("Extracted Number: " + (fs < 1 ? input.substring(1) : 
                                                        input.substring(1,fs)));
    System.out.println("Final Output: " + input.substring(0,1) + replacement + 
                       (ls != fs || fs == input.length() - 2 ? input.substring(ls) : ""));
}

输出:

Extracted Number: 000
Final Output: X123-X

【讨论】:

  • 它是否适用于 X000-X 和 X000 之类的字符串?
  • @Minion 是的,它适用于您上面给出的所有案例。
  • @Pooya 更新了解决方案。它现在提取并打印数字。
  • @user2004685 OP 提到“我需要得到那个数字,用它做数学然后替换它”所以我认为提取的数字应该存储在一个整数中
  • @Pooya 这只是一个示例代码 sn-p,它告诉您如何解决问题。可以轻松地将字符串解析为整数并进行任何必要的计算并使用它来代替replacement 字符串。
【解决方案2】:

我只给你一个例子,你可以根据要求遵循相同的,或者你可以根据你的要求将一些行分成单独的方法,

   String value = "X000-X000000";
    if(value.contains("-")){
        String match="";
        String[] elements = value.split("-");

        Pattern pattern = Pattern.compile("[0-9]+");
        Matcher matcher = pattern.matcher(elements[0]); //For the first element that is X000

        while (matcher.find()) {
            match = matcher.group(); //Numeric is match that is 000
        }

    //Do whatever you want to do with 000 here like I replaced 123 here

     elements[0] = elements[0].replace(match, String.valueOf(123));
        StringBuilder builder = new StringBuilder();
        for(String s : elements) {
            builder.append(s + "-");
        }
        value = builder.toString().substring(0,builder.length()-1); //Your final value that can be returned as well


    }

【讨论】:

  • 它不会删除第二部分。例如,X000-X000000 给出了类似X123-X000000 的内容,而不仅仅是X123,正如预期的那样。也许你可以解决这个问题。
  • @user2004685 是的。他的要求太大了,有时他想删除,有时又不想删除。但是他可以通过在分离的元素数组中将其替换为“”(空)来轻松删除特定元素。或者,如果他只想使用 elements[0] 的第一部分也满足要求。这一切都基于需求。非常感谢你的建议,我很感激。
  • 谢谢,你也是。
猜你喜欢
  • 1970-01-01
  • 2021-09-13
  • 2015-04-12
  • 2017-12-28
  • 1970-01-01
  • 2015-12-25
  • 2012-12-10
  • 2013-09-02
  • 2022-01-03
相关资源
最近更新 更多