在javascript中转换大写和小写

时间:2016-10-22 12:37:13

标签: javascript string uppercase lowercase

我想制作一个在Javascript中转换大写和小写的代码。

例如,“嗨,Stack Overflow”。 ----> '嗨,sTACK oVERFLOW'

我该怎么做?

6 个答案:

答案 0 :(得分:5)

你可以遍历每个角色,如果是大写则将其转换为小写,如果是小写,则将其转换为大写,如果它不是(如果它是逗号,冒号等),则将其转换为原样:

str = 'Hi, Stack Overflow.';
res = '';
for (var i = 0; i < str.length; ++i) {
    c = str[i];
  if (c == c.toUpperCase()) {
    res += c.toLowerCase();
  } else if (c == c.toLowerCase()) {
    res += c.toUpperCase();
  } else {
    res += c;
  }
}

答案 1 :(得分:3)

您可以使用map()

尝试这个简单的解决方案

&#13;
&#13;
var a = 'Hi, Stack Overflow!'

var ans = a.split('').map(function(c){
  return c === c.toUpperCase()
  ? c.toLowerCase()
  : c.toUpperCase()
}).join('')

console.log(ans)
&#13;
&#13;
&#13;

答案 2 :(得分:1)

<!DOCTYPE html>
<html>
<head>
<title>hello</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>

<script>
$(document).ready(function(){
    var x = 'Hi, Stack Overflow.';

    alert(caseAlter(x));

    function caseAlter(txt){
        var output = "";

        for(var i = 0; i < txt.length; i++){
            if(txt[i] == txt[i].toUpperCase()){
                output += txt[i].toLowerCase();
            }else{
                output += txt[i].toUpperCase();
            }           
        }

        return output;
    }

});
</script>
</body>
</html>

答案 3 :(得分:0)

将为您执行此操作的功能:

   // Summary:
//     Specifies that a data field value is required.
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class RequiredAttribute : ValidationAttribute
{
    // Summary:
    //     Initializes a new instance of the System.ComponentModel.DataAnnotations.RequiredAttribute
    //     class.
    public RequiredAttribute();

    // Summary:
    //     Gets or sets a value that indicates whether an empty string is allowed.
    //
    // Returns:
    //     true if an empty string is allowed; otherwise, false. The default value is
    //     false.
    public bool AllowEmptyStrings { get; set; }

    // Summary:
    //     Checks that the value of the required data field is not empty.
    //
    // Parameters:
    //   value:
    //     The data field value to validate.
    //
    // Returns:
    //     true if validation is successful; otherwise, false.
    //
    // Exceptions:
    //   System.ComponentModel.DataAnnotations.ValidationException:
    //     The data field value was null.
    public override bool IsValid(object value);
}

答案 4 :(得分:0)

&#13;
&#13;
var hi = 'Hi, Stack Overflow.';

var hI = hi.split("");
for(var i=0;i<hI.length;i++){
if(hI[i] == hI[i].toLowerCase())hI[i]  = hI[i].toUpperCase();
else if(hI[i] == hI[i].toUpperCase())hI[i] = hI[i].toLowerCase();
} 
hI = hI.join("");
alert(hI);
//'hI, sTACK oVERFLOW'
&#13;
&#13;
&#13;

答案 5 :(得分:0)

var input = 'Hi, Stack Overflow.'
var arr = input.split(' ');

alert(arr);
var output = arr.map(function(elem){

  return (elem[0].toLowerCase() + elem.substring(1,elem.length).toUpperCase());

});

alert(output.join());
相关问题