-2

I have a String looking like this: 450ZVXX_OF_INV_CC 3, which basically consists of three parts: Column 1 has 3 digits (in this case the numbers 450), Column 2 has 30 digits (in this case the digits and whitespaces until 3) and Column 3 has again 3 digits.

Now I need to split the String into the three parts using the digits 3, 30, 3 (stored in ints), because I have to build a table with above mentioned three columns.

So I basically want to take int1 = 3, get the first 3 digits of the String. Then I take int2 = 30, get the next 30 digits of the String. And then I take int3 = 3 to get the next 3 digits of the String.

FYI: The length digits 3, 30, 3 are the preset column length by the database system.

Regex is not useful, because I do not have any digits or characters, which I use to split the String. I will just use the preset numbers as explained above.

Programming Language: Java

Thanks in advance!

Jan Konrad
  • 135
  • 1
  • 1
  • 5

2 Answers2

0

You can code it using String.substring method.

public class SubStrings {
  public static void main(String[] args) {
    String input = "450ZVXX_OF_INV_CC                 3";
    int first = 3;
    int second = 30;
    String part1 = input.substring(0, first);
    String part2 = input.substring(first, first+second);
    String part3 = input.substring(first+second);
    System.out.println(part1);
    System.out.println(part2);
    System.out.println(part3);
  }
}
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
0

This will work for you.

public class SubStrings {
  public static void main(String[] args) {
    String abc="123456789101112131415151617181999123";
    Integer first = Integer.parseInt(abc.substring(0, 3));
    BigInteger second = new BigInteger(abc.substring(3, 30));
    Integer third = Integer.parseInt(abc.substring(33, 36));
    System.out.println(first);
    System.out.println(second);
    System.out.println(third);
  }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Droy
  • 173
  • 2
  • 17
  • The third `substring` can throw `StringIndexOutOfBoundsException` if the length of the string is less then 36. Even same is happening, for the sample string provided in the question. – YoungHobbit Sep 04 '15 at 11:42
  • @abhishekbafna The string he provided seems to be just an example as he mentioned "**Column 1** has **3 digits**", "**Column 2** has **30 digits**","**Column 3** has **3 digits**" highlighted. – Droy Sep 04 '15 at 11:55
  • @Jan Konrad Please clarify the question. Is it possible to have less digits than 36? – Droy Sep 04 '15 at 11:57
  • Why would you create `BigInteger` objects for all the three parts. The `first` and `third` are of 3 character only, so max value is `999` and `second` part of the input is not necessary a `number`. Even sample data contains chars and whitespaces `(ZVXX_OF_INV_CC )`. – YoungHobbit Sep 04 '15 at 12:04
  • @abhishekbafna Definitely, you got the point about that BigInteger thing. But still not so sure about what could be the possible content of the string. Let the asker answer what he wants – Droy Sep 04 '15 at 12:08