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?
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);
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);
FAdded 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);
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)
get-input?f