-4

Suppose the string is like this:

String msg = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"

I want to add a character supposing 'f' after every 10 character iteration using subString function because I can't call StringBuilder class ( used it for insert or append functionality).

6
  • No it's not duplicate, please do read the problem!!! Commented Sep 22, 2016 at 12:29
  • Well, this is some form of homework. In that case, you need to use material you learned in class. Homework questions are supposed to show your own attempt to solve the problem, and explain where you have a problem in it. Commented Sep 22, 2016 at 12:33
  • why can't you use a StringBuilder, it is what it will be used anyway when your code will be compiled Commented Sep 22, 2016 at 12:33
  • Nicolas, I'm basically writing my program in a bean file for groovy thats why I cant put StringBuilder function. Commented Sep 22, 2016 at 12:34
  • @RonaldoKillergod It is a duplicate of your previous question. Commented Sep 22, 2016 at 12:34

2 Answers 2

0
public class HelloWorld{

 public static void main(String []args){
     int i = 10;

    String msg = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; 
       while(i <= msg.length())
        {
           msg = msg.substring(0, i) + "f" + msg.substring(i, msg.length());
           i = i + 11; 

        }
    System.out.println(msg);
 }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

It may help you

public static String insertPeriodically(String text, String insert, int period)
{
StringBuilder builder = new StringBuilder(text.length() + insert.length() * (text.length()/period)+1);

int index = 0;
String prefix = "";
while (index < text.length())
{
    // Don't put the insert in the very first iteration.
    // This is easier than appending it *after* each substring
    builder.append(prefix);
    prefix = insert;
    builder.append(text.substring(index, 
        Math.min(index + period, text.length())));
    index += period;
}
return builder.toString();
}

1 Comment

This answer is exact copy of @JonSkeet answer in the Putting char into a java string for each N characters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.