2

I am getting comma sepeated string in below format:

String codeList1 = "abc,pqr,100101,P101001,R108972";

or

String codeList2 = "mno, 100101,108972";

Expected Result : Check if code is numeric after removing first alphabet. If yes, remove prefix and return. If no, still return the code.

codeList1 = "abc,pqr,100101,101001,108972"; 

or

codeList2 = "mno, 100101,108972";

As you can see, I can get codes (P101001 or 101001) and (R108972 ,108972) format. There is will be only one prefix only.

If I am getting(P101001), I want to remove 'P' prefix and return number 101001.

If I am getting 101001, do nothing.

Below is the working code. But is there any easier or more efficient way of achieving this. Please help

for (String code : codeList.split(",")) {
   if(StringUtils.isNumeric(code)) {
        codes.add(code);
   } else if(StringUtils.isNumeric(code.substring(1))) {
        codes.add(Integer.toString(Integer.parseInt(code.substring(1))));
   } else {
        codes.add(code);
     }
   }
7
  • 1
    What about R in R108972? Commented Jul 19, 2018 at 11:24
  • There can be any alphabet prefix but only one. Commented Jul 19, 2018 at 11:33
  • According to your question,do you want to also add data like abc? what is else used for? Commented Jul 19, 2018 at 11:36
  • Please what is the expected result? Commented Jul 19, 2018 at 11:38
  • I want to add add the codes. If single alphabet prefix found , remove it and add remaining Commented Jul 19, 2018 at 11:40

3 Answers 3

2

If you want to remove prefixes from the numbers you can easilly use :

String[] codes = {"abc,pqr,100101,P101001,R108972", "mno, 100101,108972"};
for (String code : codes){
    System.out.println(
            code.replaceAll("\\b[A-Z](\\d+)\\b", "$1")
    );
}

Outputs

abc,pqr,100101,101001,108972
mno, 100101,108972

If you are using Java 8+, and want to extract only the numbers, you can just use :

String codeList1 = "abc,pqr,100101,P101001,R108972";
List<Integer> results = Arrays.stream(codeList1.split("\\D")) //split with non degits
        .filter(c -> !c.isEmpty())                            //get only non empty results
        .map(Integer::valueOf)                                //convert string to Integer
        .collect(Collectors.toList());                        //collect to results to list

Outputs

100101
101001
108972
Sign up to request clarification or add additional context in comments.

7 Comments

Do you know what is codes.add(code); used for in OP's question? I think he is also want to add pure string like abc
@lucumt It is unclear what OP result should be, I will wait the OP to answer my comment in top!
Thanks for your response. it seems to be more cleaner approach. Thanks...
one thing, I was testing the scenarios and for this code (abc15), it is not working. it is removing the c and giving result as ab15. Can we handle this?? I only want to remove prefix if one alphabet is coming
@MananKapoor are you sure? check the demo link it return abc15 for abc15 !!
|
2

You can use regex to do it

  String str = "abc,pqr,100101,P101001,R108972";
  String regex = ",?[a-zA-Z]{0,}(\\d+)";
  Pattern pattern = Pattern.compile(regex);
  Matcher matcher = pattern.matcher(str);
  while(matcher.find()){
      System.out.println(matcher.group(1));
  }

Output

100101
101001
108972

Updated:
For your comment(I want to add add the codes. If single alphabet prefix found , remove it and add remaining ),you can use below code:

String str = "abc,pqr,100101,P101001,R108972";
String regex = "(?=,?)[a-zA-Z]{0,}(?=\\d+)|\\s";// \\s is used to remove space
String[] strs = str.replaceAll(regex,"").split(",");

Output:

abc
pqr
100101
101001
108972

Comments

1

How about this:

String codeList1 = "abc,pqr,100101,P101001,R108972";
String[] codes = codeList1.split(",");

for (String code : codes) {
    if (code.matches("[A-Z]?\\d{6}")) {
        String codeF = code.replaceAll("[A-Z]+", "");
        System.out.println(codeF);
    }
}

100101
101001
108972

Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.