简体   繁体   English

如何在Java中分割字符串?

[英]How to split a string in Java?

I have a group of radio buttons and a group of checkboxes. 我有一组单选按钮和一组复选框。 I want to print out the selected radio buttons/checked boxes. 我想打印出选定的单选按钮/复选框。 To print out the radio button selected I use 要打印出我使用的单选按钮

Object table = radio.getValue();
System.out.println(table);

I get the radio button selected. 我选择了单选按钮。 To get which checkboxes have been selected I use the same: 要获取已选中的复选框,请使用相同的复选框:

Object columns = check.getValue();
System.out.println(columns);

I get the checkboxes which have been checked but with square brackets surrounding them, eg if I check the boxes 我得到了已选中但周围有方括号的复选框,例如,如果我选中了这些复选框

columnA, columnC, columnF 列A,列C,列F

the printed line would look like this: 打印的行将如下所示:

[columnA, columnC, columnF] [A栏,C栏,F栏]

I want to put the strings of the selected checkboxes into my sql query so I have something like this data.executeQuery("Select " + columns + " From " + table); 我想将所选复选框的字符串放入我的sql查询中,所以我有类似这样的数据data.executeQuery("Select " + columns + " From " + table);

It works with the table but it doesn't work with the columns . 它适用于table但不适用于columns

For this paricular application, you do not need to split a string. 对于此特殊应用程序,您无需拆分字符串。 You may use 您可以使用

String str = columns.toString(); // "[colummA, colummC, colummF]"
System.out.println(str.substring(1, str.length() - 1));

You may also form a comma-separated string using 您还可以使用以下命令形成逗号分隔的字符串

StringBuilder sb = new StringBuilder();
for(Object obj : columns) {
    if (sb.length() != 0) sb.append(",");
    sb.append(obj);
}

However, to answer the question: 但是,要回答这个问题:

String is splitted via split(String) method using a Regular Expression syntax. 使用正则表达式语法通过split(String)方法split(String) As you have a list of items, converting it to a String and splitting it isnt an effective decision. 当您具有项目列表时,将其转换为String并将其拆分不是有效的决定。

columns is an array containing all values of the selected checkboxes. columns是一个包含选定复选框的所有值的数组。 Make sure that the default toString method outputs the items separated by a "," 确保默认的toString方法输出以“,”分隔的项目

this is because default 这是因为默认

  check.getValue().toString()

called and depending on if its a List or a HashSet and default toString() method of List/HashSet is called which by default appends [] while printing the values so for your case you could either use a collection with a overridden toString() method as per your requirement or just write you custom toString method and call it instead of the default toString Method 被调用,并取决于是调用List / HashSet的List还是HashSet以及默认的toString()方法,默认情况下,该方法在打印值时追加[],因此对于您而言,您可以使用带有覆盖的toString()方法的集合根据您的要求,或只编写自定义toString方法并调用它,而不是默认的toString方法

here is a sample implementation if it is HashSet 如果是HashSet,这是一个示例实现

  private String getHashSetString(Set<String> myset){
    StringBuilder sb = new StringBuilder();
    Iterator<String> i = myset.iterator();
    if (! i.hasNext())
        return "";
    for (;;) {
        Long e = i.next();
        sb.append(e);
        if (! i.hasNext())
        return sb.toString();
        sb.append(",");
    }
}

So instead of 所以代替

  data.executeQuery("Select " + columns + " From " + table);

write

  data.executeQuery("Select " + columns.getHashSetString() + " From " + table);

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

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