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)
}
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)
}
Share
Improve this question
edited Feb 22, 2023 at 9:58
DSDmark
1,2815 gold badges13 silver badges27 bronze badges
asked Sep 23, 2021 at 1:25
Blake PriceBlake Price
212 silver badges4 bronze badges
3
-
1
Yes, you can use the
join
function to bine multiple items in an array into a single string using your specified delimiter (''
). But if your variableuserarray
only contains one item, it will not work. You can instead usereplace
to remove the space from a single string:user.replace(" ", "");
. – George Sun Commented Sep 23, 2021 at 1: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 Commented Sep 23, 2021 at 1:32
- Does this answer your question? How to replace all occurrences of a string in JavaScript – Heretic Monkey Commented Sep 23, 2021 at 11:12
3 Answers
Reset to default 4You 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)
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".
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