To create a string of variable length:Use the Array() constructor to create an array of empty elements with length N + 1. Call the join() method on the array to join the elements with the string you want to repeat. The join method returns a new string, where the array elements are separated by the specified string.
We can fill a fixed size char array with our desired character and convert that to a string: char[] charArray = new char[N]; for (int i = 0; i < N; i++) { charArray[i] = 'a'; } String newString = new String(charArray); assertEquals(EXPECTED_STRING, newString);
StringBuilder sb = new StringBuilder(n); for (int i = 0; i < n; ++i) { sb. append(c); } String result = sb. toString();
In Python, strings have a built-in method named ljust . The method lets you pad a string with characters up to a certain length. The ljust method means "left-justify"; this makes sense because your string will be to the left after adjustment up to the specified length.
I'm writing JavaScript unit tests and I need to create a string of length 65536. What's the best way to do this in JavaScript?
Currently I'm using:
var myString = ''; for (var i = 0; i <= 65535; ++i) { myString += 'x'; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With