Putting Char into a Java String for Each N Characters

Putting char into a java string for each N characters

Something like:

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

Create a string with n characters

The for loop will be optimized by the compiler. In such cases like yours you don't need to care about optimization on your own. Trust the compiler.

BTW, if there is a way to create a string with n space characters, than it's coded the same way like you just did.

String Manipulation insert a character every 4th character

String s = "123456789012";
String s1 = s.substring(0, 4);
String s2 = s.substring(4, 8);
String s3 = s.substring(8, 12);

String dashedString = s1 + "-" + s2 + "-" + s3;
//String.format is extremely slow. Just concatenate them, as above.

substring() Reference

What is the easiest/best/most correct way to iterate through the characters of a string in Java?

I use a for loop to iterate the string and use charAt() to get each character to examine it. Since the String is implemented with an array, the charAt() method is a constant time operation.

String s = "...stuff...";

for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
//Process char
}

That's what I would do. It seems the easiest to me.

As far as correctness goes, I don't believe that exists here. It is all based on your personal style.

Append a single character to a string or char array in java?

1. String otherString = "helen" + character;

2. otherString += character;

I am trying to insert a string character to another string. How can I achieve it in java?

It isn't clear from your question exactly what your "rules" are for processing this. However, your output seems to simply insert a character between each character of your source a string.

Instead of using a substring, create a separate StringBuilder to add individual characters to. The code below produces the output you are looking for:

String string = "000";
StringBuilder output = new StringBuilder();

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

// Get current character in the string
char c = string.charAt(i);

// Add the current character to the output
output.append(c);

// If more characters exist, add the pipe
if (i != string.length() - 1) {
output.append("|");
}
}

System.out.println(output.toString());

How can I insert a character after every n characters in javascript?

function chunk(str, n) {
var ret = [];
var i;
var len;

for(i = 0, len = str.length; i < len; i += n) {
ret.push(str.substr(i, n))
}

return ret
};

chunk("The quick brown fox jumps over the lazy dogs.", 5).join('$');
// "The q$uick $brown$ fox $jumps$ over$ the $lazy $dogs."


Related Topics



Leave a reply



Submit