I want to bine two strings that are connected with hyphen like;
get-form
to
getForm
How can I do this with using native javascript or jquery?
I want to bine two strings that are connected with hyphen like;
get-form
to
getForm
How can I do this with using native javascript or jquery?
Share Improve this question edited Jul 2, 2022 at 7:25 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Mar 31, 2016 at 7:15 midstackmidstack 2,1237 gold badges48 silver badges74 bronze badges6 Answers
Reset to default 4Try 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);
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);
var input = "get-form";
var sArr = input.split("-");
var result = sArr[0] + sArr[1].charAt(0).toUpperCase() + sArr[1].slice(1);
Try a bination 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);
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);
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)