简体   繁体   English

分割字符串

[英]Splitting a line of String

I have a String line which can be 我有一个字符串行,可以是

AND A B C -> D
OR A B -> D
NAND A B C D E -> F
............

How can I split the String line into just ABCD without the first String token (AND, OR, NAND) and without -> and should trim the whitespaces? 如何在没有第一个String令牌(AND,OR,NAND)且没有->情况下将String行仅分割为ABCD并应修剪空白? I have done here but it gives me an output 我在这里做了,但是给了我一个输出

or A B C
D

public static void main(String [] args)throws IOException
    {
        String str = "or A B C -> D";
        String[] array = str.split("->");
        String[] leftSide = array[0].trim().split(array[0]);
        String[] rightSide = array[1].trim().split(" ");

        for(int i = 0; i < leftSide.length; i++)
        {
            System.out.println(leftSide[i]);
        }

        for(int i = 0; i < rightSide.length; i++)
        {
            System.out.println(rightSide[i]);
        }
    }
        String str = "AND A B C -> D";
        // \\s+ for multiple white spaces  -> is for your request
        StringTokenizer st = new StringTokenizer(str, "\\s+ ->");

        boolean isFirstToken = true;
        while (st.hasMoreTokens()) {
            if (isFirstToken) {
                isFirstToken = false;
                st.nextToken();//For first iteration
                continue;
            }
            System.out.print(st.nextToken() + " ");
        }

Split the string by a space " " and then iterate through the resulting array, removing any sequence that is more than 1 symbol. 用空格“”分隔字符串,然后遍历结果数组,删除任何超过1个符号的序列。

    String str = "or A B C -> D";
    String[] array = str.split(" ");
    List<String> list = Arrays.asList(array);
    String currentString;

    for(int i = 0; i < list.size(); i++)
    {
        currentString = list.get(i);

        if(currentString.length() >1) {
             list.remove(currentString);
        }
    }

     System.out.println(list);

What's happening is you're splitting on "->", giving "or ABC " and " D". 发生的事情是您在“->”上拆分,给了“或ABC”和“ D”。 Then you're trimming them, then splitting them on nothing in particular (you split the left side by itself, and the right side by " " but "D" has no spaces to split on. That's giving you the results you see. 然后修剪它们,然后对其进行特别分割(将左侧本身分开,将右侧分开为“”,但是“ D”没有可分割的空间。这将为您提供看到的结果。)

You can just define all the stuff that's valid in some array and then split everything, then only keep the stuff that's valid. 您可以只在某个数组中定义所有有效内容,然后拆分所有内容,然后仅保留有效内容。

public static final String[] VALUES = new String[] {"A","B","C","D"};
public static void main(String [] args)
{
    String str = "or A B C -> D";
    String[] array = str.split(" ");
    for (int i = 0; i < array.length; i++) {
        if (Arrays.asList(VALUES).contains(array[i])) {
            System.out.print(array[i] + " ");
        }
    }
}

Alternatively, you can do this the other way around if that makes more sense for your application - define a list of terms that should NOT be included instead. 或者,如果对您的应用程序更有意义,则可以采用其他方法进行操作-定义不应包含的术语列表。

public static final String[] EXCLUDE = new String[] {"AND", "OR", "->"};
public static void main(String [] args)
{
    String str = "or A B C -> D";
    String[] array = str.split(" ");
    for (int i = 0; i < array.length; i++) {
        if (!(Arrays.asList(EXCLUDE).contains(array[i]))) {
            System.out.print(array[i] + " ");
        }
    }
}

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

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