2

I am trying to join a single string Blake Price together, so remove the space " " in between the first and last name (BlakePrice). Are you even able to join a single string in an array?

function openprofile(user) {
  console.log(user)
  var userarray = [user]
  var usernamejoined = userarray.join('')
  console.log(usernamejoined)
}
DSDmark
  • 1,045
  • 5
  • 11
  • 25
  • 1
    Yes, you can use the `join` function to combine multiple items in an array into a single string using your specified delimiter (`''`). But if your variable `userarray` only contains one item, it will not work. You can instead use `replace` to remove the space from a single string: `user.replace(" ", "");`. – George Sun Sep 23 '21 at 01:29
  • Questions related to code should always include a tag for the language you're using. Please [edit] your post to provide that tag - it will help get your question noticed by the people who are familiar with that language, which will get you an answer faster. It will also help future users find the post when they're looking for a solution to a similar problem. Thanks. – Ken White Sep 23 '21 at 01:32
  • Does this answer your question? [How to replace all occurrences of a string in JavaScript](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Heretic Monkey Sep 23 '21 at 11:12

3 Answers3

4

You can split the username with space and you will get an array of each word like this ['Blake', 'Price']. And you join all strings in an array with an empty string and you will get "BlakePrice"

The code is like this

const username = "Blake Price"
const joinedUsername = username.split(' ').join('')
console.log(joinedUsername)
showdev
  • 28,454
  • 37
  • 55
  • 73
Kevin Liao
  • 313
  • 2
  • 7
1

I believe what you are looking for is function 'split' instead of join. Example:

function openfile(user) {
    // also add try-catch block
    console.log(usersplit)
    var usersplit = user.split(" ")
    console.log(usersplit[0] + usersplit[1])
}

Now:

openfile("Blake Price")

Prints out "BlakePrice".

0

You can use .split(" ") to cut each word with a space and put it in an array for example :

var user = 'black price'
console.log(user.split(' '))

result[('black', 'price')]

And use .join('') to concatenate an entire array into a string without any spaces. And this example code for your case:

var openprofile = (username) => {
  var usernamejoined = username.split(' ').join('')
  return usernamejoined
}

console.log(openprofile('black price'))   // output:- blackprice
DSDmark
  • 1,045
  • 5
  • 11
  • 25