This is My String

14-Mar-16,RTRDTG78BGXPBE3H,D-Link DIR-600M Wireless N150 Home Router,10-Apr-16, SECEBH7HZMKZMG3B,Kaspersky Internet Security 2016 1 PC 1 Year

I want to split this string Where ever I find the pattern which I have Highlighted.


Looking at your string like this:

14-Mar-16,RTRDTG78BGXPBE3H,D-Link DIR-600M Wireless N150 Home Router,
10-Apr-16,SECEBH7HZMKZMG3B,Kaspersky Internet Security 2016 1 PC 1 Year

it appears to be a repeating pattern of 3 values separated by commas. If that is the case, personally I would just go for:

String[] parts = '14-Mar-16,RTR...Year'.split(',');

and deal with each piece based on its index in the parts array:

for (Integer i = 0; i < parts.size(); i++) {
    String part = parts[i];
    Integer type = Math.mod(i, 3);
    if (type == 0) use part as a date
    else if (type == 1) use part as a code
    else if (type == 2) use part as a name
| improve this answer | |
  • In "typing" this, you're obviously not creating a class, but I do see the OOP logic to it. Am curious, is this somewhat akin to a factory pattern perhaps, an interface, or what would you say drove your thinking towards this approach? – crmprogdev May 14 '16 at 14:53
  • @crmprogdev Yes I would also probably create a class to represent each item. My core point is that data formats often have simple delimiters (such as commas) and using simple delimiters avoids adding code that could fail such as assuming that the day of month always has 2 numbers when it may sometimes have 1 and vice versa. Also splitting on a comma is KISS compared to using a regular expression. – Keith C May 14 '16 at 15:08
  • Totally agree with you on the KISS and using the commas to do the split. I'd have recommended the same thing. I was simply intrigued by your "typing" the results as a means to find the dates in the list. Nice work! – crmprogdev May 14 '16 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.