Skip to content

[js] 第5天 写一个把字符串大小写切换的方法 #15

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第5天 写一个把字符串大小写切换的方法

Activity

undefinedYu

undefinedYu commented on Apr 27, 2019

@undefinedYu
Contributor

var reversal = function(str){
var newStr = '';
if(Object.prototype.toString.call(str).slice(8,-1) !== 'String'){
alert("请填写字符串")
}else{
for(var i=0;i<str.length;i++){
newStr += ((str.charCodeAt(i)>96 && str.substr(i,1).toUpperCase()) || str.substr(i,1).toLowerCase());
}
}
return newStr;
}

linghucq1

linghucq1 commented on May 8, 2019

@linghucq1
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 
lantian1024

lantian1024 commented on May 15, 2019

@lantian1024
function caseConvert(str){
    return str.replace(/([a-z]*)([A-Z]*)/g, (m, s1, s2)=>{
	return `${s1.toUpperCase()}${s2.toLowerCase()}`
    })
}
caseConvert('AsA33322A2aa') //aSa33322a2AA
fuiiiiiii

fuiiiiiii commented on May 22, 2019

@fuiiiiiii
let str = 'aBcDeFgH'
let arr = []
for(let item of str) {
  if (item === item.toUpperCase()) {
    item = item.toLowerCase()
  } else {
    item = item.toUpperCase()
  }
  arr.push(item)
}
let newStr = arr.join('')
console.log(newStr)
// AbCdEfGh
likeke1997

likeke1997 commented on Jun 5, 2019

@likeke1997
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
MartinsYong

MartinsYong commented on Jun 21, 2019

@MartinsYong
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;
}
Konata9

Konata9 commented on Jul 2, 2019

@Konata9
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'))
Azathoth-H

Azathoth-H commented on Jul 12, 2019

@Azathoth-H
const upperOrlower = (str) => str.split('').reduce((acc, x)=> acc+= x == x.toLocaleUpperCase()? x.toLocaleLowerCase(): x.toLocaleUpperCase(), '')
    upperOrlower("what's THIS ?") // WHAT'S this ?
seho-dev

seho-dev commented on Jul 14, 2019

@seho-dev

学习到了,借鉴到了;

function test(str) {
return str.replace(/([a-z])([A-Z])/g, (m, s1, s2) => {
return ${s1.toUpperCase()}${s2.toLowerCase()};
});
}

function test2(str) {
let arr = [];
for (let x of str) {
if (x === x.toUpperCase()) {
x = x.toLowerCase();
} else {
x = x.toUpperCase();
}
arr.push(x);
}
return arr.join("");
}

console.log(test("a6M3cH"));

shufangyi

shufangyi commented on Jul 18, 2019

@shufangyi
const exchangeUpAndLow = str =>
  typeof str === 'string'
    ? str.replace(/[A-Za-z]/g, char =>
        char.charCodeAt(0) <= 90
          ? char.toLocaleLowerCase()
          : char.toLocaleUpperCase()
      )
    : str
wyx2014

wyx2014 commented on Jul 24, 2019

@wyx2014
function change2Char(str) {
  if(typeof str !== 'string'){
    return '请输入字符串';
   }
   return str.split('').map(function (item) {
       return /^[a-z]$/.test(item)?item.toUpperCase():item.toLowerCase();
   }).join('');
gu-xionghong

gu-xionghong commented on Jul 25, 2019

@gu-xionghong
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')
IanSun

IanSun commented on Jul 26, 2019

@IanSun
function reverseCharCase ( string = `` ) {
  return Array.prototype.reduce.call(
    string,
    ( string, char ) =>
      string += `\u0040` < char && `\u005b` > char ?
        char.toLowerCase() :
        `\u0060` < char && `\u007b` > char ?
          char.toUpperCase() :
          char,
    ``,
  );
}
wsb260

wsb260 commented on Jul 31, 2019

@wsb260
// 1.test() 方法用于检测一个字符串是否匹配某个模式,返回Boolean值 
// 2.toUpperCase() 转换成大写,toLowerCase()转换成小写

function changeStr(str){
  let ary = [];
  let newStr = '';
  if(!str||str === ''||typeof str !== 'string'){
    return false;
  }
  ary = str.split("");
  ary.map(item => {
    newStr += /[A-Z]/.test(item) ? item.toLowerCase() : item.toUpperCase();
  })
  console.log(newStr)
}

changeStr('aAbBcC') // 输出:AaBbCb
//$1、$2、...、$99与 regexp 中的第 1 到第 99 个子表达式相匹配的文本
//function(a,b,c)一共可以传入3个参数,第一个为匹配到的字符串,第二个为匹配字符串的起始位置,第三个为调用replace方法的字符串本身,(非全局匹配的情况下/g),下例为多组匹配,s1,s2分别对应$1,$2
function caseConvert(str){
  return str.replace(/([a-z]*)([A-Z]*)/g, function(m,s1,s2){
    return s1.toUpperCase() + s2.toLowerCase()
  })
}
console.log(caseConvert('aSa')) //AsA

69 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @smile-2008@haizhilin2013@JJL-SH@Konata9@wyx2014

        Issue actions

          [js] 第5天 写一个把字符串大小写切换的方法 · Issue #15 · haizlin/fe-interview