在 javascript 中组合两个用连字符连接的字符串
Combine two strings connected with a hyphen in javascript
我想合并两个用连字符连接的字符串,例如;
get-form
到
getForm
如何使用本机 javascript 或 jquery 执行此操作?
尝试组合 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);
试试这个:拆分字符串,然后将第二个单词的第一个字母大写,然后将其与第一部分连接起来。
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);
var x = 'get-form'
var res = x.replace("-f", "F");
console.log(res)
DEMO 使用替换
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)
var input = "get-form";
var sArr = input.split("-");
var result = sArr[0] + sArr[1].charAt(0).toUpperCase() + sArr[1].slice(1);
这是针对多个连字符的解决方案。它将每个部分的第一个字母大写并添加字符串的其余部分。
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 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);
我想合并两个用连字符连接的字符串,例如;
get-form
到
getForm
如何使用本机 javascript 或 jquery 执行此操作?
尝试组合 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);
试试这个:拆分字符串,然后将第二个单词的第一个字母大写,然后将其与第一部分连接起来。
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);
var x = 'get-form'
var res = x.replace("-f", "F");
console.log(res)
DEMO 使用替换
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)
var input = "get-form";
var sArr = input.split("-");
var result = sArr[0] + sArr[1].charAt(0).toUpperCase() + sArr[1].slice(1);
这是针对多个连字符的解决方案。它将每个部分的第一个字母大写并添加字符串的其余部分。
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 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);