Hello everyone,
I am having a small problem, does someone know how to break a long string into small groups of 3 characters. I've tried it but its not working.

Here's a sample of my code:

int begindex =0, endindex = 3;
    String plaintext = null;
    String block;
    StringBuffer sbplaintext = new StringBuffer();


try{

  
    while ((input = fin.read()) != -1)   {
       sbplaintext.append((char) input);
    }
    plaintext = sbplaintext.toString();

    for(int i=0; i < plaintext.length();i++){


        begindex += 3;
        endindex += 3;
    }

I got a string buffer "sbplaintext", (convert into string) and my objective is to make a group a 3 chars.

Thanks in advance for your help.

I believe the String class has a toCharArray method that would help you. Then every 3 indexes are what you are looking for. Otherwise, use a for loop to add every substring of 3 chars to an array.

Thanks a lot, its working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.