1

I want to combine two strings that are connected with hyphen like;

get-form

to

getForm

How can I do this with using native javascript or jquery?

6 Answers 6

4

Try this : split string and then make second word's first letter capitalized and then join it with first part.

    var str = "get-form";
    str = str.split("-");
    
    var str2= str[1].slice(0,1).toUpperCase() + str[1].slice(1);
    
    var newStr = str[0]+str2;
    
    alert(newStr);

Sign up to request clarification or add additional context in comments.

Comments

1

This is a solution for multiple hyphens. It capitalized the first letter of every part and adds the rest of the string.

var string = 'get-form-test-a',

string = string.split('-').map(function (s, i) {
    return i && s.length ? s[0].toUpperCase() + s.substring(1) : s;
}).join('');
document.write(string);

Comments

1
var input = "get-form";
var sArr = input.split("-");
var result = sArr[0] + sArr[1].charAt(0).toUpperCase() + sArr[1].slice(1);

Comments

1

Try a combination of split,join:

    var x = 'get-form';
    var newx = x.split('-');
 
    newx[1] = newx[1].charAt(0).toUpperCase() + newx[1].slice(1);//get the first caracter turn it to uppercase add the rest of the string 
    newx = newx.join('');
    alert(newx);
   

1 Comment

You missed F
0

Added the code which would take care of the capital casing of first letter for each word.

var data = 'get-form-test';
var dataArray = data.split('-');
var result = dataArray[0];
$.each(dataArray,function(index, value){
  if(index > 0){  
   result += value.charAt(0).toUpperCase() + value.slice(1);
}
});
alert(result);

Comments

0
var x = 'get-form'
var res = x.replace("-f", "F");
console.log(res)

DEMO Using replace

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

var x = 'get-form'
var res = x.replace("-f", "F");
console.log(res)

3 Comments

this is to specific,what happens if the text is get-input?
This code only works of the second word starts with f
yes i am only answer the OP. OP didnt mention any other thing it was straight forward question with a straight forward answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.