Simple Way to Repeat a String

Simple way to repeat a string

String::repeat

". ".repeat(7)  // Seven period-with-space pairs: . . . . . . . 

New in Java 11 is the method String::repeat that does exactly what you asked for:

String str = "abc";
String repeated = str.repeat(3);
repeated.equals("abcabcabc");

Its Javadoc says:

/**
* Returns a string whose value is the concatenation of this
* string repeated {@code count} times.
* <p>
* If this string is empty or count is zero then the empty
* string is returned.
*
* @param count number of times to repeat
*
* @return A string composed of this string repeated
* {@code count} times or the empty string if this
* string is empty or count is zero
*
* @throws IllegalArgumentException if the {@code count} is
* negative.
*
* @since 11
*/

Java: String - add character n-times

You are able to do this using Java 8 stream APIs. The following code creates the string "cccc" from "c":

String s = "c";
int n = 4;
String sRepeated = IntStream.range(0, n).mapToObj(i -> s).collect(Collectors.joining(""));

Repeating a string using a for loop

Just keep it simple

String string = "Hello";
int num = 3;
for (int i = 0; i < num; i++) {
System.out.println(string);
}

If you want to have your result in a new String you can just do this :

String string = "Hello";
int num = 3;
String res = "";
for (int i = 0; i < num; i++) {
res += string;
}
System.out.println(res);

how to repeat string in javascript loop

a bit clean way Repeat function

const repeatString = function(str, num) {
return (num < 0) ? new Error("Error") : str.repeat(num);
}

module.exports = repeatString

how can i repeat a string multiple times according to its index in a string

In functional style (thanks to @Redu for the comment):

const accum = (s) => Array.from(    s,    (c, i) => `${c.toLocaleUpperCase()}${c.repeat(i)}`  )  .join('-');
console.log(accum(''));console.log(accum('a'));console.log(accum('xyz'));

how repeat a string in language C

In your source code, without much processing, probably the easiest way is with:

#define HI "hello world"
char str[] = HI " " HI " " HI;

This will declare a string of the requested value:

"hello world hello world hello world"

If you want code that will do it, you can use something like:

char *repeatStr (char *str, size_t count) {
if (count == 0) return NULL;
char *ret = malloc (strlen (str) * count + count);
if (ret == NULL) return NULL;
strcpy (ret, str);
while (--count > 0) {
strcat (ret, " ");
strcat (ret, str);
}
return ret;
}

Now keep in mind this can be made more efficient - multiple strcat operations are ripe for optimisation to avoid processing the data over and over (a). But this should be a good enough start.

You're also responsible for freeing the memory returned by this function.


(a) Such as with:

// Like strcat but returns location of the null terminator
// so that the next myStrCat is more efficient.

char *myStrCat (char *s, char *a) {
while (*s != '\0') s++;
while (*a != '\0') *s++ = *a++;
*s = '\0';
return s;
}

char *repeatStr (char *str, size_t count) {
if (count == 0) return NULL;
char *ret = malloc (strlen (str) * count + count);
if (ret == NULL) return NULL;
*ret = '\0';
char *tmp = myStrCat (ret, str);
while (--count > 0) {
tmp = myStrCat (tmp, " ");
tmp = myStrCat (tmp, str);
}
return ret;
}


Related Topics



Leave a reply



Submit