-5

Hey I need help splitting a string. The thing is i only need the words between the " - " . for example:

ABC_DEF-HIJ (KL MNOP_QRS)

I need to store DEF in string1 and HIJ in string2

some other formats are

AB (CDE)_FGH IJK/LMN-OPQ (RST

here too string1 = LMN

string2 = OPQ

I only need the words after and before the " - "

Filburt
  • 17,626
  • 12
  • 64
  • 115
newbieCoder
  • 11
  • 1
  • 7
  • 4
    What you already try to do? Can you show your code? – Slava Vedenin Dec 17 '15 at 15:48
  • 1
    A few hints: use `indexOf("-")` and then check in both directions how many letters there are until a non-alphabetical letter appears. Then you know which characters are relevent for building the two Strings. – LordAnomander Dec 17 '15 at 15:49

4 Answers4

0

Consider this approach using a regex:

public static void main(String[] args) {

    Pattern p = Pattern.compile("(\\w{3})-(\\w{3})");
    Matcher m = p.matcher("AB (CDE)_FGH IJK/LMN-OPQ (RST");
    if(m.find()) {
        System.out.println("1: " + m.group(1));
        System.out.println("2: " + m.group(2));
    }
}

produces

1: LMN
2: OPQ

If your "words" were more then 3 characters long, you might want to change the {3} into + for anything >= 1

Jan
  • 13,738
  • 3
  • 30
  • 55
0

So basically you to first split by - and then each side by a non-word character.

Therefore you can try:

String s = "ABC_DEF-HIJ (KL MNOP_QRS)";
String[] splits = s.split("-");  // {"ABC_DEF", "HIJ (KL MNOP_QRS)"}

String[] lefts = split[0].split("[^a-zA-Z]");  // {"ABC", "DEF"}
String[] rights = split[1].split("[^a-zA-Z]"); // {"HIJ", "", "KL", "MNOP", "QRS"}

String string1 = lefts[lefts.length - 1]; // "DEF""
String string2 = rights[0];               // "HIJ"
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

Try this:

String[] parts = yourString.split("-");
//You can then access each part with: parts[index]
Mayuso
  • 1,291
  • 4
  • 19
  • 41
0
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringExtractTest {

    private static class ExtractResult {
        private ExtractResult(String string1, String string2) {
            this.string1 = string1;
            this.string2 = string2;
        }
        private String string1;
        private String string2;
        public String getString1() {
            return string1;
        }
        public String getString2() {
            return string2;
        }
    }

    public static ExtractResult extract(String input) {
        Pattern p = Pattern.compile("([a-zA-Z]+)-([a-zA-Z]+)");
        Matcher m = p.matcher(input);
        if (m.find()) {
            return new ExtractResult(m.group(1), m.group(2));
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        String inputs[] = {
                "ABC_DEF-HIJ (KL MNOP_QRS)",
                "AB (CDE)_FGH IJK/LMN-OPQ (RST"
        };
        for (String input : inputs) {
            ExtractResult result = extract(input);
            if (result != null) {
                System.out.println(input + " ... string1 = [" + result.getString1() + "] string2 = [" + result.getString2() + "]");
            }
        }
    }

}

The output is:

ABC_DEF-HIJ (KL MNOP_QRS) ... string1 = [DEF] string2 = [HIJ]
AB (CDE)_FGH IJK/LMN-OPQ (RST ... string1 = [LMN] string2 = [OPQ]
MartinCz
  • 528
  • 4
  • 13