-1

My code is as below. i need to add single quotes for each word in string with single quotes after appending DD to it.

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

I have tried using StringBuffer too. please point your answers here. The output i get is some junk data while i need the words with dd attached at the start.

Expected output:'DDa1b2c3','DD321cba','DD123abc'

5
  • 2
    checkIDS..append('\'').append(checkId.split(",")).append('\'').append(','); beforethe first append there is 2 dots, delete one Commented Dec 28, 2021 at 9:26
  • 1
    what is "junk data" ? Commented Dec 28, 2021 at 9:41
  • You need to show the output you are getting. Commented Dec 28, 2021 at 10:03
  • Thanks for your comment but the issue is not the dot, it was a mistake in the code regarding the dot. and the junk data i am speaking about is : '[Ljava.lang.String;@d70c109',.... Commented Dec 28, 2021 at 10:21
  • Thanks for you comment.. i have got the answer required and marked a green tick for it Commented Dec 29, 2021 at 6:01

4 Answers 4

1

Problem

  • issue at .append(checkId.split(",")) where you append an String[] so it's representation is it's hashcode

  • don't need a second loop, each word need one loop round, no inner loop needed

  • your split is wrong, you need ChkboxIds.split(","), you don't need with the same string as delimiter


Fix

You can do much more simpler than that

  • split on comma
  • remove quotes, append DD, add quotes
  • save at same place in array
  • join array with comma
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'

Regex power

String chkboxIds = "'a1b2c3','321cba','123abc'";
String result = chkboxIds.replaceAll("'(\\w+)'", "'DD$1'");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you answer its very helpful and i got the expected output
0

Try this:

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

Alternatively:

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

Refer to the respective Javadoc for the details.

Comments

0

For a simpler solution, you can try the code below.

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

Output:

enter image description here

Comments

0

Using regular expressions: If the single words doesnt contain additional single quotes:

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

Using iteration :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.