Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How/Can you join/combine a single string in an array?

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)
}
like image 401
Blake Price Avatar asked Oct 27 '25 09:10

Blake Price


2 Answers

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)
like image 151
Kevin Liao Avatar answered Oct 29 '25 23:10

Kevin Liao


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".

like image 25
Iman A. Fazel Avatar answered Oct 30 '25 00:10

Iman A. Fazel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!