java - break large String into small Strings -


i have large string contains id's example :

hd47-4585-gg89

here @ above have id of single object may contain id's of multiple objects :

hd47-4585-gg89-ko89-9089-rt45

the above haves ids of 2 objects want convert above string array or in multiple small strings :

id1 = hd47-4585-gg89

id2 = ko89-9089-rt45

every single id haves fixed number of characters in here 14 (counting symbols too) , number of total id's in single string not determined

i dont know how 1 can guide me ?

i think have clip first 14 characters of string assign variable , repeat until string empty

you use regex:

string input = "hd47-4585-gg89-ko89-9089-rt45";  pattern id = pattern.compile("(\\w{4}-\\w{4}-\\w{4})"); matcher matcher = id.matcher(input);  list<string> ids = new arraylist<>();  while(matcher.find()) {     ids.add(matcher.group(1)); }  system.out.println(ids); // [hd47-4585-gg89, ko89-9089-rt45] 

see ideone.

although assumes each group of characters (hd47) 4 long.


Comments