简体   繁体   中英

Separate specific words from a string in java

Is there any way to split the words from a string in java ?.

String my ="StackOverFlow PF Apple FT Laptop HW."

PF = Platform, FT = Fruit, HW = Hardware.

The expected output should be

StackOverFlow  is a Platform.
Apple  is a Fruit.
Laptop is a hardware.

I am doing in this way:

String[] words = my.split(" ");
  for(int u=0 ; u<words.length ; u++){
      System.out.println( words(u));
  }

If you can guarantee that the values will be in the above order, something like this should work

public static void main(String[] args) {
    String my = "StackOverflow PF Apple FT Laptop HW";
    String[] words = my.split(" ");
    for (i = 0; i < words.length; i++) {
        if (i % 2 == 0) {
            System.out.print(words(i) + " is a ");
        } else {
            System.out.println(getTranslation(words(i)));
        }
    }
}

private String getTranslation(String code) {
    if ("PF".equals(code)) {
        return "Platform";
    }
    //etc...
}

Essentially, what this will do is split the string into all of the words. Since the words are "paired" together, they will come in groups of 2. This means that you can check if the index for the word is even or odd. If it's even, then you know it's the 1st paired word meaning you can append the "is a " string to it. If it's odd, then you want to append the translated value.

Use regex amd split the hole text in groups of 2 words...

then split every element of the array by spaces and you are done!

Example:

public static void main(String[] args) throws ParseException {
String inputTxt = "StackOverFlow PF Apple FT Laptop HW.";
String[] elements = inputTxt.split("(?<!\\G\\w+)\\s");
System.out.println(Arrays.toString(elements));
System.out.println(elements[0].split(" ")[0] + " is a Platform");
System.out.println(elements[1].split(" ")[0] + " is a Fruit");
System.out.println(elements[2].split(" ")[0] + " is a Hardware");
}

Given the limited specification in your question, there's no reason to split. You can just replace your placeholders like this:

String my = "StackOverFlow PF Apple FT Laptop HW.";
my = my.replaceAll("PF[\\s.]?", "  is a Platform.\n");
my = my.replaceAll("FT[\\s.]?", "  is a Fruit.\n");
my = my.replaceAll("HW[\\s.]?", " is a hardware.\n");
System.out.print(my);

Output:

StackOverFlow   is a Platform.
Apple   is a Fruit.
Laptop  is a hardware.
public class StackOverflow {

    public static void main(String[] args) {
        // Fields
        String myString = "StackOverFlow PF Apple FT Laptop HW";

        String[] tokens = myString.split(" ");

        for (int i = 0; i < tokens.length; i++) {
            System.out.print(tokens[i]);
            // Every other token
            if (i % 2 == 0) {
                System.out.print(" is a " + convert(tokens[i + 1]));
                i++;
            }
            System.out.println();
        }

    }

    /**
     * convert method turns the abbreviations into long form
     */
    private static String convert(String s) {
        String str;
        switch (s) {
            case "PF":
                str = "Platform";
                break;
            case "FT":
                str = "Fruit";
                break;
            case "HW":
                str = "Hardware";
                break;
            default:
                str = "Unknown";
                break;
        }
        return str;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM