0

Title seems to be simple. But I don't get a good Idea. This is the situation

I have String like this in my Java program

String scz="3282E81WHT-22/24";  

I want to split the above string into 3 Strings, such that

first string value should be 3282e81,

Next string should be WHT(ie, the String part of above string and this part is Always of 3 Characters ),

Next String value should be 22/24 (Which will always occur after -)

In short

String first= /* do some expression on scz And value should be "3282e81" */;
String second= /* do some expression on scz And value should be "WHT" */;
String third= /* do some expression on scz And value should be "22/24" */;

Input can also be like

scz="324P25BLK-12"; 

So 324P25 will be first String, BLK will be second (of 3 Characters). 12 will be third ( After - symbol )

How to solve this?

15
  • Is the WHT part is fixed? I mean, do you want to split by WHT or will there be any other characters as well? Commented Aug 20, 2013 at 4:45
  • 4
    You need to take a step backwards. Why is the first element 3282E81 second element WHT and third element 22/24?. Only when you know the rules can you implement them. Commented Aug 20, 2013 at 4:47
  • No. It will change. But that part will be String. It can also be like this scz=324P25BLK-12 So 324P25 will be first String, BLK will be second. 12 will be third Commented Aug 20, 2013 at 4:48
  • @BevynQ I am getting input like that from an XML file. in one tag. I need to split it into 3 values like I said in question Commented Aug 20, 2013 at 4:50
  • @Shiju last 3 digit of 324P25BLK will always of length 3 ? Commented Aug 20, 2013 at 4:53

10 Answers 10

4

You can use a regex like this (\d+[A-Z]\d+)([A-Z]+)-([/\d]+) and using Matcher.group(int) method you can get your string splitted into three groups.

Code snippet

String str = "3282E81WHT-22/24";
//str = "324P25BLK-12";
Pattern pattern = Pattern.compile("(\\d+[A-Z]\\d+)([A-Z]+)-([/\\d]+)");
Matcher match = pattern.matcher(str);
System.out.println(match.matches());
System.out.println(match.group(1));
System.out.println(match.group(2));
System.out.println(match.group(3));

Output

true
3282E81
WHT
22/24
Sign up to request clarification or add additional context in comments.

Comments

2

Use this to split the entire string in to two

String[] parts = issueField.split("-");
String first = parts[0];
String second= parts[1];

Use this to split the first string into two

if (first!=null && first.length()>=3){  
   String lastThree=first.substring(first.length()-3);
}

Comments

1

if your String's Second part (WHT) etc will always be of 3 Characters then following code will surely help you

String scz = "3282E81WHT-22/24";

            String Third[] = scz.split("-");
            String rev = new StringBuilder(Third[0]).reverse().toString();
            String Second=rev.substring(0,3);
            String First=rev.substring(3,rev.length());

            // here Reverse your String Back to Original
            First=new StringBuilder(First).reverse().toString();
            Second=new StringBuilder(Second).reverse().toString();

            System.out.println(First + "  " + Second + "  " + Third[1]);

1 Comment

I'm getting output like this 18E2823 THW 22/24 the first two are reversed
1

You can use subString() method to get this goals. subString has numbers of overloads.

for first string

String first=scz.subString(0,6);
String second=scz.subString(7,9);

1 Comment

But this is based on length. The String value length may change
1

You can use following regex to take out the above type string:

\d+[A-Z]\d{2}|[A-Z]{3}|(?<=-)[\d/]+

In Java, you can use above regex in following way:

  Pattern pattern = Pattern.compile("\\d+[A-Z]\\d{2}|[A-Z]{3}|(?<=-)[\\d/]+");
  Matcher matcher = pattern.matcher("3282E81WHT-22/24");
  while (matcher.find()) {
        System.out.println(matcher.group());
  }

Output:

3282E81
WHT
22/24

Comments

0

You could us a char array instead of a string so you can access specific characters withing the array. Example

char scz[] = "3282E81WHT-22/24";

and access the separate characters just by specifying the place in which the array you want to use.

Comments

0

You can try this

       String scz="3282E81WHT-22/24";
       String[] arr=scz.split("-");
       System.out.println("first: "+arr[0].substring(0,7));
       System.out.println("second: "+arr[0].substring(7,10));
       System.out.println("third: "+arr[1])

Comments

0

Check out my solution -

class Main {
    public static void main(String[] args) {
            String first = "";
            String second = "";
            String third = "";

            String scz="3282E81WHT-22/24";
            String[] portions = scz.split("-");
            if (portions.length > 1) {
                    third = portions[1];
            }

            String[] anotherPortions = portions[0].split("[a-zA-Z]+$");
            if (anotherPortions.length > 0) {
                    first = anotherPortions[0];
            }

            second = portions[0].substring(first.length());

            System.out.println(first);
            System.out.println(second);
            System.out.println(third);
    }
}

Live Demo.

Comments

0
String scz="3282E81WHT-22/24";
String[] array = scz.split("-");
String str1 = (String) array[0].subSequence(0, 7);
String str2 = array[0].substring(7);

Then the split will be in this order :)

str1
str2
array[1]

Comments

0

if the length of string is fixed for scz, first,second and third the you can use

  String first=scz.subString(0,6);
  String second=scz.subString(7,9);
  String third=scz.subString(10,scz.length());

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.