-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第5天 写一个把字符串大小写切换的方法 #15
Comments
var reversal = function(str){ |
function caseConvert(str) {
return str.split('').map(s => {
const code = s.charCodeAt();
if (code < 65 || code > 122 || code > 90 && code < 97) return s;
if (code <= 90) {
return String.fromCharCode(code + 32)
} else {
return String.fromCharCode(code - 32)
}
}).join('')
}
console.log(caseConvert('AbCdE')) // aBcDe
function caseConvertEasy(str) {
return str.split('').map(s => {
if (s.charCodeAt() <= 90) {
return s.toLowerCase()
}
return s.toUpperCase()
}).join('')
}
console.log(caseConvertEasy('AbCxYz')) // aBcXyZ |
|
|
function toggle(str) {
var result = str.split('');
result.forEach(function(e, i, a) {
a[i] = e === e.toUpperCase() ? a[i] = a[i].toLowerCase() : a[i] = a[i].toUpperCase()
});
return result.join('');
}
var result = toggle('ifYouAreMyWorld');
console.log(result); // IFyOUaREmYwORLD |
function caseTransform (str) {
let transformed = '';
for (let v of str) {
const charCode = v.charCodeAt();
if (charCode >= 97 && charCode <= 122) {
transformed += v.toUpperCase();
}
else if (charCode >= 65 && charCode <= 90) {
transformed += v.toLowerCase();
}
else {
transformed += v;
}
}
return transformed;
} |
const convertCase = (str) =>
str
.split("")
.map((s) => (/[A-Z]/.test(s) ? s.toLowerCase() : s.toUpperCase()))
.join("");
console.log(convertCase('AbCdE'))
console.log(convertCase('aaabbbCCCDDDeeefff'))
console.log(convertCase('aBcDe')) |
const upperOrlower = (str) => str.split('').reduce((acc, x)=> acc+= x == x.toLocaleUpperCase()? x.toLocaleLowerCase(): x.toLocaleUpperCase(), '')
upperOrlower("what's THIS ?") // WHAT'S this ? |
学习到了,借鉴到了; function test(str) { function test2(str) { console.log(test("a6M3cH")); |
const exchangeUpAndLow = str =>
typeof str === 'string'
? str.replace(/[A-Za-z]/g, char =>
char.charCodeAt(0) <= 90
? char.toLocaleLowerCase()
: char.toLocaleUpperCase()
)
: str |
|
function caseConvert(str) {
return str.replace(/([a-z])?([A-Z]?)/g, function(m, $1, $2) {
let s = ''
s += $1 ? $1.toUpperCase() : ''
s += $2 ? $2.toLowerCase() : ''
return s
})
}
caseConvert('asd__BBB_cDe') |
|
|
function caseConvert(sourceString) {
if (typeof sourceString !== "string") {
return ""
}
return sourceString.split("").reduce((previousValue, currentValue) => {
let charCode = currentValue.charCodeAt()
if (
(charCode >= 65 && charCode <= 90) ||
(charCode >= 97 && charCode <= 122)
) {
if (charCode <= 90) {
return previousValue + String.fromCharCode(charCode + 32)
} else {
return previousValue + String.fromCharCode(charCode - 32)
}
} else {
return previousValue + currentValue
}
}, "")
} |
const change = str => {
return [...str].map( letter => {
return letter.toLowerCase() === letter ?
letter.toUpperCase() : letter.toLowerCase();
}).join("");
}
console.log(change("abd ADSas")); // ABD adsAS 刚开始没看清楚题,以为全大写全小写.... |
function stringToUpperLower(str) {
return str.replace(/([a-z]*)([A-Z]*)/g, (all, $1, $2) => {
return $1.toUpperCase() + $2.toLowerCase();
})
}
console.log(stringToUpperLower('a0B1c2D3e4F5g')) |
let str='aAc3_DD=sdLL' |
function sonversion (str) {
return str.replace(/([A-Z]*)([a-z]*)/g,(match,$1,$2)=>{
return `${$1.toLowerCase()}${$2.toUpperCase()}`
})
}
console.log(sonversion('aaBBCCdd')) |
function transUppLow(str) { |
'aAc3_DD=sdLL'.replace(/([a-z]|[A-Z])/g, v => v.toLowerCase() === v ? v.toUpperCase() : v.toLowerCase()) |
/**
* 写一个把字符串大小写切换的方法
* @param {String} str
*/
function toggleCase(str) {
return str
.split("")
.map(item => /[A-Z]/.test(item) ? item.toLocaleLowerCase() : item.toLocaleUpperCase())
.join("");
}
console.log(toggleCase("aa+-12BB")); // "AA+-12bb" |
function convertCase(str){
return str.split('').map(s=>s===s.toUpperCase()? s.toLowerCase():s.toUpperCase()).join('')
}
convertCase('aSd') //AsD |
function convertStr(str) {
return [...str].map(char => char === char.toUpperCase() ? char.toLowerCase() : char.toUpperCase()).join('')
}
console.log(convertStr('aBc'))
// AbC
|
/**
* @param {string} str
* @return {string}
*/
function convert(str) {
var res = "";
for (var chr of str) {
var code = chr.charCodeAt();
if (code >= 65 && code <= 90) {
code += 32;
} else if (code >= 97 && code <= 122) {
code -= 32;
} else {
}
res += String.fromCharCode(code);
}
return res;
} |
|
function caseConvert(str) { |
//第5天 写一个把字符串大小写切换的方法
//for example: aBcDeFg => AbCdEfG
//"a".charCodeAt(0) 97
//"A".charCodeAt(0) 65
function toggleCase(str) {
return str
.split("")
.map(c => {
return c.charCodeAt(0) >= 97 ? c.toUpperCase() : c.toLowerCase();
})
.join("");
}
var str = "aBcDeFg";
console.log(toggleCase(str)); |
如果用正则的话,这个比较好function函数被调用的次数要少。上面有个正则是 |
function stringCaseSwitch(str){ |
function func(str){ |
function translateAa(str) {
|
function toggleCase(str){ |
<script type="text/javascript"> function changStr(str){ if(typeof str !== 'string'){ console.log('确认要更改的对象是字符串') }else{ let newName='' let arr = str.split('') arr.forEach(e=>{ let code = e.charCodeAt() // A~Z 65~90 a~z 97~122 大小写字母的ASCII码 if((code>=65&&code<=90)||(code>=97&&code<=122)){ newName += (code>96 ? e.substr(0,1).toUpperCase(): e.substr(0,1).toLowerCase()) }else{ newName += e } }) return newName } } const str = changStr('ab-FD') console.log(str) </script> |
var str = 'abcdeFGHIJKLmnopqRSTUVwxyz' |
// Unicode编码 A(65) - Z(90) a(97) - b(122)
function toggleCase(str) {
return str
.split("")
.map((char) => {
if (char.charCodeAt() >= 65 && char.charCodeAt() <= 90) {
return char.toLowerCase();
} else if (char.charCodeAt() >= 97 && char.charCodeAt() <= 122) {
return char.toUpperCase();
}
})
.join("");
}
console.log(toggleCase("sdiawhiAAsfFFDD")); |
|
|
let mystr = 'nidHDIEkdIEDN' console.log(toggle(mystr)) |
|
你的邮件我已经收到,我会尽快处理的,谢谢。
|
function UpcaseSwitchLowcase(data){ |
function UpcaseSwitchLowcaseMap(data){ |
const changeCase = (str = '', type = false) => { |
你的邮件我已经收到,我会尽快处理的,谢谢。
|
|
你的邮件我已收到,我会尽快处理
|
var str = 'zyytNhCXzwsb' var arr = [] function tpup(str){ console.log(tpup(str)); // ZYYTnHcxZWSB |
// 切换大小写子母 |
你的邮件我已经收到,我会尽快处理的,谢谢。
|
'asdaASDsda'.replace(/([a-z])([A-Z])/g, (match, $1, $2) => $1.toUpperCase() + $2.toLowerCase() ) |
你的邮件我已经收到,我会尽快处理的,谢谢。
|
function change(str){ |
你的邮件我已经收到,我会尽快处理的,谢谢。
|
1 similar comment
你的邮件我已经收到,我会尽快处理的,谢谢。
|
|
你的邮件我已经收到,我会尽快处理的,谢谢。
|
let arr =[]; |
function transform(str) { |
第5天 写一个把字符串大小写切换的方法
The text was updated successfully, but these errors were encountered: