简体   繁体   English

用Java中的字符串将特定单词分开

[英]Separate specific words from a string in java

Is there any way to split the words from a string in java ?. 有没有办法从java的字符串中拆分单词?

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

PF = Platform, FT = Fruit, HW = Hardware. PF =平台,FT =水果,HW =硬件。

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. 由于单词是“成对的”在一起,它们将以2为一组。这意味着您可以检查单词的索引是偶数还是奇数。 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... 使用regex amd将孔文本分成2个单词...

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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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