Java extract integers from a string containing delimiters and range symbols -
is there library can me split string integers delimiters , range marks? instance
values="32,38,42-48,84"
output:
int[] = 32,38,42,43,44,45,46,47,48,84
if want whole range (from 42-55 example), i'm not entirely sure, can use regular expressions find that's not number . expression "[^0-9]
use replace , change character (e.g commas). split commas. feel free read more here: http://www.vogella.com/tutorials/javaregularexpressions/article.html (note: i'm not related in way vogella. liked tuto :)
edit: can see output want (which believe wasn't there). if that's want, find split commas first. after, check if have elements in have symbol (- example), , if so, split , use numbers create range. here's example of working code:
public static void main (string[] args) throws java.lang.exception { // code goes here scanner scan = new scanner(system.in); string s = scan.nextline(); string[] splitted = s.split(","); for(int = 0; < splitted.length;i++){ if(splitted[i].contains("-")){ string[] nums = splitted[i].split("-"); int = integer.parseint(nums[0]); int = integer.parseint(nums[1]); for(int j = from;j <= to;j++) system.out.print(j+","); } else system.out.print(splitted[i] + ","); }
might not efficient solution, works.
Comments
Post a Comment