繁体   English   中英

如何将第一个和第三个字母大写?

[英]How to capitalize first and third letter?

我想在我的文本区域中将第一个字母和第三个字母大写。

我只能将第一个字母大写,然后将接下来的每个字母和单词都转换为小写。

如果这个问题有任何解决方案,请告诉我。

我正在使用 AngularJS。

这就是我正在尝试和所做的。

  link: function (scope, iElement, iAttrs, controller) {
        //console.log('init');

         controller.$parsers.push(function (inputValue) {
          var transformedInput = (!!inputValue) ? inputValue.charAt(0).toUpperCase()  + inputValue.substr(1).toLowerCase() : '';
                      if (transformedInput != inputValue) {
            controller.$setViewValue(transformedInput);
            controller.$render();
        }

        return transformedInput;
    });

这仅适用于第一个字母,它转换为大写,然后将另一个字母和单词转换为小写。

我试图将我的代码更改为此但没有。

  var transformedInput = (!!inputValue) ? inputValue.charAt(0).toUpperCase()  + inputValue.substr(1).toLowerCase()  + inputValue.charAt(3).toUpperCase() + inputValue.substr(4).toLowerCase(): '';

看看这个。 与您只使用for循环来标识要修改的字符索引相同。

 var inputValue = "test"; var transformedInput = ''; if(inputValue){ for(var i=0; i<inputValue.length; i++){ if(i===0 || i=== 2){ transformedInput += inputValue.charAt(i).toUpperCase(); } else { transformedInput += inputValue.charAt(i).toLowerCase(); } } } console.log(transformedInput); 

这是在特定位置将字符大写的功能

function capitalizeAtPositions(string, indexes) {
  (indexes || []).forEach(function(index) {
    if (string.length < index) return;

    string = string.slice(0, index) +
      string.charAt(index).toUpperCase() + string.slice(index+1);
  });
  return string;
}

如下运行:

var test = "abcdefg";
var result = capitalizeAtPositions(test, [0, 2]);
//AbCdefg

在您的情况下,我认为它将是类似的(没有jsfiddle不能测试):

var transformedInput = capitalizeAtPositions(inputValue || '', [0, 2]);

看到键入时如何更改输入,您可能需要一个指令。 这是一个用ng-model将任何输入的给定字母大写的字母:

https://plnkr.co/edit/hWhmjQWdrghvsL20l3DE?p=preview

app.directive('myUppercase', function() {
  return {
    scope: {
      positions: '=myUppercase'
    },
    require: 'ngModel',
    link: function(scope, elem, attrs, ngModelCtrl) {

      scope.positions = scope.positions || []

      function makeString(string) {
        if (!string) return;
        angular.forEach(scope.positions, function(pos) {
          string = string.slice(0, pos) + string.slice(pos, pos+1).toUpperCase() + string.slice(pos + 1)
          console.log(string)
        })
        return string;
      }

      ngModelCtrl.$parsers.push(makeString)
      ngModelCtrl.$formatters.push(makeString)
    }
  }
})

HTML:

<input ng-model="value" my-uppercase="[0, 2]">

我的简单解决方案

var inputValue = 'your value';

function toUpper (str) {
    var result = '';
    for (var i = 0; i < str.length; i++) {
        if (i === 0 || i === 2) {
            result += str[i].toUpperCase();
        } else {
            result += str[i].toLowerCase();
        }
    }
    return result;
}
var transformedInput = toUpper(inputValue);

我对“McArturo”这种你想要的文字的解决方案

$("#LastName").keyup(function () {
var last_name = $("#LastName").val();
var op = last_name.substr(0, 2);
if (op == "mc" || op == "Mc" || op == "MC") {
    $("#LastName").val("Mc" + (last_name.charAt(2).toUpperCase()) + last_name.substr(3).toLowerCase());
} else {
    $("#LastName").val(last_name.charAt(0).toUpperCase() + last_name.substr(1).toLowerCase());
}});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM