【问题标题】:How to append String to StringBuffer variable in JAVA如何在 JAVA 中将字符串附加到 StringBuffer 变量
【发布时间】:2022-01-26 23:36:39
【问题描述】:

我的代码如下。在将 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'

【问题讨论】:

  • checkIDS..append('\'').append(checkId.split(",")).append('\'').append(',');第一个追加前有2个点,删除一个
  • 什么是“垃圾数据”?
  • 你需要显示你得到的输出。
  • 感谢您的评论,但问题不是点,而是有关点的代码中的错误。我所说的垃圾数据是:'[Ljava.lang.String;@d70c109',....
  • 嗨,OP。让我们知道任何答案是否有帮助。如果它回答了您的问题,请接受它。这样其他人就知道你已经(充分地)得到了帮助。另见What should I do when someone answers my question?

标签: java string append output stringbuffer


【解决方案1】:

问题

  • .append(checkId.split(",")) 处附加一个String[] 的问题,所以它的表示是它的哈希码

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

  • 你的分割错误,你需要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$1'");

【讨论】:

  • 感谢您的回答非常有帮助,我得到了预期的输出
【解决方案2】:

试试这个:

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。

【讨论】:

    【解决方案3】:

    对于更简单的解决方案,您可以尝试以下代码。

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

    输出:

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2013-02-08
        • 2012-02-23
        • 1970-01-01
        • 2016-07-14
        • 2017-10-14
        • 1970-01-01
        • 1970-01-01
        • 2012-09-09
        • 2020-10-14
        相关资源
        最近更新 更多