Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a string with n characters? How to create a string with specific length? [duplicate]

Tags:

People also ask

How do you make a string of certain lengths?

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.

How do you make a string with length n?

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

How do you make a string of repeating characters?

StringBuilder sb = new StringBuilder(n); for (int i = 0; i < n; ++i) { sb. append(c); } String result = sb. toString();

How do you create a string of a certain length in Python?

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