Insert Spaces into string at an index

Related searches

I'm trying to do this Codewars problem.


In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.


  1. The input string will always be lower case but maybe empty.
  2. If the character in the string is whitespace then pass over it as if it was an empty seat. Example wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]

My code so far is hosted on this

My thought process is as follows:

  1. Turn argument into array
  2. manipulate each index of the array at index and then readjust previous index to make a wave pattern
  3. turn array into string
  4. reinsert spaces before logging it to console and restarting the loop

I'm pretty stuck and my mind is stuck on how to use

for(var j = 0; j < indexSpaceNumber.length; j++){ //join and add in the spaces at their former index before returning string strToArray[indexSpaceNumber[j]].slice(0, " "); }

to insert the spaces into the string.

If there's any guidance or tips it would be much appreciated. I feel like I'm close, but so frustratingly far.

The main idea would be:

  1. Iterate the characters
  2. Replace the character in the original string with an uppercase version

You can use Array.from() to convert the string to an array, and map each item to a new string. If the character is a space return something falsy (en empty string in the example). After the creating the array, filter all falsy values:

const wave = str =>
  Array.from(str, (c,i) => // convert the string to an array
    // replace the character with an uppercase version in the original string
    c === ' ' ?
    `${str.substring(0, i)}${c.toUpperCase()}${str.substring(i + 1)}`
  ).filter(c => c)

const result = wave("hello") 


Python insert space every 2 characters, Nov 9, 2019 Write a Python program to insert spaces between words starting with capital letters. String indexing in Python is zero-based: the first character in the� How do you turn ‘20130701’ to ‘2013-07-01’? There are many ways to tackle this, breaking it into pieces (substring) and joining it back with a dash, use some regex fu to manipulate it, parse it as a DateTime and format it and so on. Here’s another way that uses the String.Insert method.

For string with spaces

function wave(str) {
  let res = []
  str.toLowerCase().split('').forEach((v, i) => {
    if(v == ' ') return;
    res.push( str.substr(0, i) + v.toUpperCase() + str.substr(i + 1) )
  return res

console.log(wave("hello hello"))

Pad a String with Whitespace, There are three string functions that are closely related to their base R equivalents, but with x <- "Learning to use the stringr package" # alternative indexing str_sub(x, start = 1, end = 15) To add whitespace, or to pad a string, use str_pad() :. link. brightness_4. code. # Python3 code to demonstrate. # Add substring at specific index. # using list slicing. # initializing string. test_string = 'geeksgeeks'. # initializing add_string.

I'd go recursive ;)

You know that for a string of length n you need an array of the same length. That's your exit condition.

You can use the length of the array at each iteration to work out the shape of the next string:

hello []            [Hello]              0: uppercase 1st char and append
hello [Hello]       [Hello hEllo]        1: uppercase 2nd char and append
hello [Hello hEllo] [Hello hEllo heLlo]  2: uppercase 3rd char and append

const wave =
  (str, arr = []) =>
    str.length === arr.length
      ? arr
      : wave
          ( str
          , [ ...arr
            ,   str.slice(0, arr.length)
              + str[arr.length].toUpperCase()
              + str.slice(arr.length + 1)


Reverse a string preserving space positions, Create a string to store result. Mark the space position of the given string in this string. Insert the character from input string into the result string� Re: Insert character at a specific position in a string Posted 11-15-2017 07:02 PM (14616 views) | In reply to torvyle First step: make sure your variable has a length of $5 or longer.

Go over each char in string and build Slice str from start till current char + current char to upper case + Slice str from current char to end

const wave = str => {
  const res = [];
  for (let i = 0; i < str.length; i++) {
    res.push(`${str.slice(0, i)}${str[i].toUpperCase()}${str.slice(i + 1)}}`);
  return res;

console.log(wave("hi my name is rylan"));

// Alternate way to do with Array.splice
const wave2 = str => {
  const res = [];
  for (let i in str) {
    const temp = Array.from(str);
    temp.splice(i, 1, temp[i].toUpperCase());
  return => x.join(''));

console.log(wave2("hi my name is rylan"));

Print all possible strings that can be made by placing spaces , C++ program to print permutations of a given string with spaces. #include i and j are indices in 'str[]' and 'buff[]' respectively */ Either put the character. Turn REPLACE function into “INSERT” function. Sometimes, we want to add a text string, say “XX”, as a prefix or suffix to another text string. This is quite easy with the function CONCATENATE, or even easier with the operator &.

string::insert - C++ Reference, Another string object. subpos: Position of the first character in str that is inserted into the object as a substring. If this is greater than str's length,� In this ArticleAdd Spaces to a StringVBA Space FunctionVBA Coding Made Easy Add Spaces to a String A common way to add spaces to a string with VBA is to use blank spaces between quotation marks. The following example adds ten spaces between the word “Hello” and “World”: Sub AddSpaces() Dim MyString As String MyString…

IndexOf(String) Reports the zero-based index of the first occurrence of the specified string in this instance. IndexOf(Char) Reports the zero-based index of the first occurrence of the specified Unicode character in this string. IndexOf(String, Int32) Reports the zero-based index of the first occurrence of the specified string in this instance.

  • Can you please add case for multiple words in your input?