如何将字符串附加到 JAVA 中的 StringBuffer 变量

How to append String to StringBuffer variable in JAVA

我的代码如下。在将 DD 附加到字符串后,我需要为带有单引号的字符串中的每个单词添加单引号。

public class Main
{
    public static void main(String[] args) {
        String ChkboxIds = "'a1b2c3','321cba','123abc'";
        String checkBoxId = null;
        String checkId = null;
        StringBuilder checkIDS = new StringBuilder("'");
        for(int i=0;i<=ChkboxIds.split(ChkboxIds, ',').length;i++){
            checkBoxId = "DD"+ChkboxIds.split(",")[i].replace("'","")+","+checkBoxId;
            checkId = checkBoxId.substring(0, checkBoxId.length() - 5);
            System.out.println("---PRINT---"+checkId);
            for(int j=0;j<i;j++){
                checkIDS..append('\'').append(checkId.split(",")).append('\'').append(',');
                System.out.println("---PRINT123----"+checkIDS);
            }
        }
    }
}

我也试过使用StringBuffer。请在这里指出你的答案。我得到的输出是一些垃圾数据,而我需要在开头附加 dd 的单词。

预期输出:'DDa1b2c3','DD321cba','DD123abc'

试试这个:

var ids = Arrays.stream( ChkboxIds.split( "," ) ) // Separate the words
  .map( s -> s.replace( '\'', '' ) ) // Strip the single quotes
  .map( s -> "DD%s".formatted( s ) ) // Prepend with "DD"
  .collect( Collectors.joining( "','", "'", "'" ) );
System.out.println( ids );

或者:

var ids = Arrays.stream( ChkboxIds.split( "," ) ) // Separate the words
  .map( s -> s.replaceFirst( "'", "'DD" ) ) // Replace the first single quote with the prefix
  .collect( Collectors.joining( "," ) );
System.out.println( ids );

有关详细信息,请参阅相应的 Javadoc。

问题

  • issue at .append(checkId.split(",")) 你附加一个 String[] 所以它的表示是它的 hashcode

  • 不需要二次循环,每个单词需要一个循环,不需要内循环

  • 你的拆分是错误的,你需要ChkboxIds.split(","),你不需要与分隔符相同的字符串


修复

你可以做的比那简单得多

  • 以逗号分隔
  • 删除引号,附加 DD,添加引号
  • 保存在数组中的相同位置
  • 用逗号连接数组
String chkboxIds = "'a1b2c3','321cba','123abc'";

String[] splitted = chkboxIds.split(",");
String checkBoxId;

for (int i = 0; i < splitted.length; i++) {
    checkBoxId = "DD" + splitted[i].replace("'", "");
    splitted[i] = "'" + checkBoxId + "'";
}

String result = String.join(",", splitted);
System.out.println(result);
// 'DDa1b2c3','DD321cba','DD123abc'

正则表达式能力

String chkboxIds = "'a1b2c3','321cba','123abc'";
String result = chkboxIds.replaceAll("'(\w+)'", "'DD'");

为了更简单的解决方案,您可以尝试下面的代码。

public class Main
{
    public static void main(String[] args) {
        String ChkboxIds = "'a1b2c3','321cba','123abc'";
        //Replace all single quotes from the ChkboxIds, split them using comma delimiter, and store it to variable array
        String[] ChkboxIdsNew = ChkboxIds.replace("'","").split(","); 

        String checkIDS = ""; //String variable to be used to store the check ids

        //Loop through the ChkboxIdsNew array
        for (int i = 0; i < ChkboxIdsNew.length; i++) {
            //Only append comma if i is greater than zero
            if (i > 0) {
                checkIDS = checkIDS + ",";
            }
            //Concatenate the single quotes and prefix DD then append it to checkIDS
            checkIDS = checkIDS + "'DD" + ChkboxIdsNew[i] + "'";
        }


        System.out.println(checkIDS);
    }
}

输出:

使用正则表达式: 如果单个单词不包含额外的单引号:

    String ChkboxIds = "'a1b2c3','321cba','123abc'";
    ChkboxIds = ChkboxIds.replaceAll(",'",",'DD").replaceAll("^'","'DD");

使用迭代:

    String arChkBoxIds[]= ChkboxIds.split(",");
    StringBuilder checkIDS1 = new StringBuilder("");
    for (String chkBoxId:arChkBoxIds){
       
       checkIDS1.append("'DD").append(chkBoxId.substring(1)).append(",");
    }
    checkIDS1.setLength(checkIDS1.length()-1);
    System.out.println(checkIDS1);