iCode
iCode

Reputation: 9202

Split Java String

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?

Upvotes: 0

Views: 721

Answers (10)

Shakeeb Ayaz
Shakeeb Ayaz

Reputation: 6096

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

Upvotes: 0

JWqvist
JWqvist

Reputation: 717

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]

Upvotes: 0

Akash KC
Akash KC

Reputation: 16310

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

Upvotes: 1

Tarsem Singh
Tarsem Singh

Reputation: 14199

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

Upvotes: 1

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29166

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.

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35547

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])

Upvotes: 0

prasanth
prasanth

Reputation: 3602

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

Upvotes: 4

freemann098
freemann098

Reputation: 284

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.

Upvotes: 0

Hybrid Developer
Hybrid Developer

Reputation: 2340

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

Upvotes: 2

Sohail Anjum
Sohail Anjum

Reputation: 94

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

Upvotes: 1

Related Questions