Pawan
Pawan

Reputation: 32331

How to Split the String by symbol name and Date in this case

I have got a String in this format

FUTSTKACC28-APR-2016

ACC is a symbol and 28-APR-2016 is a expiry date FUTSTK is predefined word

How to retrieve values symbol and Date in this case

For example how to get ACC

and

28-APR-2016

some sample data

FUTSTKACC26-MAY-2016
FUTSTKACC28-APR-2016
FUTSTKACC30-JUN-2016
FUTSTKADANIENT26-MAY-2016
FUTSTKADANIENT28-APR-2016
FUTSTKADANIENT30-JUN-2016

Upvotes: 3

Views: 140

Answers (4)

totoro
totoro

Reputation: 2456

A regex approach (bits stolen from @ElliottFrisch) assuming you know the predefined word:

String[] sample = { "FUTSTKACC26-MAY-2016", "FUTSTKACC28-APR-2016",
        "FUTSTKACC30-JUN-2016", "FUTSTKADANIENT26-MAY-2016",
        "FUTSTKADANIENT28-APR-2016", "FUTSTKADANIENT30-JUN-2016" };
String predefined = "FUTSTK";

Pattern p = Pattern.compile(Pattern.quote(predefined) + "(\\w+)(\\d\\d-\\w\\w\\w-\\d\\d\\d\\d)");
for (String s: sample) {
    Matcher m = p.matcher(s);
    if (m.matches()) {
        System.out.println(m.group(1) + " " + m.group(2));
    }
}

output:

ACC 26-MAY-2016
ACC 28-APR-2016
ACC 30-JUN-2016
ADANIENT 26-MAY-2016
ADANIENT 28-APR-2016
ADANIENT 30-JUN-2016

Upvotes: 0

muammertuzun
muammertuzun

Reputation: 123

    final String str = "FUTSTKACCCCCCC28-APR-2016";
    final String[] strArr = str.split("-");
    final String month = strArr[0].substring(strArr[0].length() - 2);
    final String word = strArr[0].substring(0, strArr[0].length() - 2);

    System.out.println("word: " + word);
    System.out.println("date: " + month + "-" + strArr[1] + "-" + strArr[2]);

Upvotes: 0

slayful
slayful

Reputation: 31

Something like this should work:

final String PATTERN = "(FUTSTK)(.+)(\d\d-\w\w\w-\d\d\d\d)"

Pattern p = Pattern.compile(PATTERN);
Matcher m = p.matcher("FUTSTKACC28-APR-2016");

String symbol = m.group(1);

DateFormat format = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date date = format.parse(string);

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201447

You have a fixed length prefix word and a fixed length date. You can remove the prefix, and then take the substrings from the right by the 11 characters in your dates. Something like,

String[] sample = { "FUTSTKACC26-MAY-2016", "FUTSTKACC28-APR-2016", 
        "FUTSTKACC30-JUN-2016", "FUTSTKADANIENT26-MAY-2016", 
        "FUTSTKADANIENT28-APR-2016", "FUTSTKADANIENT30-JUN-2016" };
String predefWord = "FUTSTK";
for (String input : sample) {
    if (input.startsWith(predefWord)) {
        input = input.substring(predefWord.length());
        // There are 11 characters in the date format
        String symbol = input.substring(0, input.length() - 11);
        String dateStr = input.substring(input.length() - 11);
        System.out.printf("symbol=%s, date=%s%n", symbol, dateStr);
    }
}

Output is

symbol=ACC, date=26-MAY-2016
symbol=ACC, date=28-APR-2016
symbol=ACC, date=30-JUN-2016
symbol=ADANIENT, date=26-MAY-2016
symbol=ADANIENT, date=28-APR-2016
symbol=ADANIENT, date=30-JUN-2016

Upvotes: 5

Related Questions