input
input

Reputation: 7519

Split string on spaces and dash but not on the value of the string

I am setting a date_string like this:

gridcell.setTag(theday + "-" + themonth + "-" + theyear + "|" + hijri_day + "-" + hijri_month + " ("+ hijri_monthno +") " + hijri_year);

And I am splitting it like this:

String date_month_year = (String) view.getTag();
String[] dateAr = date_month_year.split("-|\\||\\(|\\)|\\s+");

This is also splitting the spaces and dash in the hijri month names i.e. Rabi al-Thani or Dhul Hijjah:

private String months[] = {"Muharram","Safar","Rabi al-Awwal","Rabi al-Thani","Jumada al-Awwal","Jumada al-Thani","Rajab","Sha\'ban","Ramadhan","Shawwal","Dhul Qa\'dah","Dhul Hijjah"};

How do I split on the date_string only and not the value of the strings in the date_string?

Upvotes: 0

Views: 872

Answers (2)

Mert
Mert

Reputation: 6572

best way is changing the date separator - to / (slash) or .(dot) If you really wanna keep like this, than after split you can check last character on string array if it is a letter join that two string into one back..

gridcell.setTag(theday + "." + themonth + "." + theyear + "|" + hijri_day + " " + hijri_month + " ("+ hijri_monthno +") " + hijri_year); 

make it like this easiest way..

Upvotes: 1

Pshemo
Pshemo

Reputation: 124265

I tried to split your date step by step so check if this works for you

List<String> tokens=new ArrayList<String>();

String data="theday-themonth-theyear|hijri_day-Dhul Qa\'dah (hijri_monthno) hijri_year";
String[] tmp = data.split("\\|");
//System.out.println(Arrays.toString(tmp));

for (String s:tmp[0].split("-"))
    tokens.add(s);

System.out.println(tokens);// -> [theday, themonth, theyear]

String[] tmp2=tmp[1].split("\\s*\\(|\\)\\s*");
//System.out.println(Arrays.toString(tmp2));

for (String s:tmp2[0].split("-",2))
    tokens.add(s);
System.out.println(tokens);// -> [theday, themonth, theyear, hijri_day, Dhul Qa'dah]

tokens.add(tmp2[1]);
tokens.add(tmp2[2]);
System.out.println(tokens);// -> [theday, themonth, theyear, hijri_day, Dhul Qa'dah, hijri_monthno, hijri_year]

Upvotes: 0

Related Questions