javascript 将字符串“hAPPY,i'm a student,NOW”中的大写字母转换成小写字母,小写字母转换成大写字母。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 将字符串“hAPPY,i'm a student,NOW”中的大写字母转换成小写字母,小写字母转换成大写字母。相关的知识,希望对你有一定的参考价值。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function caset(str)
var laststr='';
for(i=0;i<str.length;i++)
thischar=str.charAt(i);
if(thischar.charCodeAt(0)>=97)thischar=thischar.toUpperCase();
elsethischar=thischar.toLowerCase();
laststr+=thischar;

return laststr;

alert( caset("hAPPY,i'm a student,NOW"));
</script>
</head>

<body>
</body>
</html>

一楼的都变成小写的了,二楼的虽然能实现大小写转换,但是标点可能会出现问题。
参考技术A 调用toLowerCase();
下面是测试
<html>
<head>
<title>test</title>
<script type="text/javascript">
function test()
var s="hAPPY,i'm a student,NOW";
s=s.toLowerCase();
alert(s);

</script>
</head>
<body>
<input type="button" value="test" onclick="test()">
</body>
</html>
参考技术B char c[]= s.toCharArray();
for(int i=0;i<c.length;i++)
if(c[i]>65)
c[i]=(char) (c[i]+32);

if(c[i]<97)
c[i]=(char) (c[i]-32);

参考技术C byte b[] = str.getBytes();
for (int i = 0; i < b.length; i++)
if (b[i] <= 123 && b[i] >= 97)
b[i] = (byte) (b[i] - 32);
else if (b[i] <= 91 && b[i] >= 65)
b[i] = (byte) (b[i] + 32);


str = new String(b);
return str;

JavaScript 将字符串调整为宽度

function fitStringToWidth(str,width,className) {
  // str    A string where html-entities are allowed but no tags.
  // width  The maximum allowed width in pixels
  // className  A CSS class name with the desired font-name and font-size. (optional)
  // ----
  // _escTag is a helper to escape 'less than' and 'greater than'
  function _escTag(s){ return s.replace(&quot;&lt;&quot;,&quot;&amp;lt;&quot;).replace(&quot;&gt;&quot;,&quot;&amp;gt;&quot;);}

  //Create a span element that will be used to get the width
  var span = document.createElement(&quot;span&quot;);
  //Allow a classname to be set to get the right font-size.
  if (className) span.className=className;
  span.style.display='inline';
  span.style.visibility = 'hidden';
  span.style.padding = '0px';
  document.body.appendChild(span);

  var result = _escTag(str); // default to the whole string
  span.innerHTML = result;
  // Check if the string will fit in the allowed width. NOTE: if the width
  // can't be determinated (offsetWidth==0) the whole string will be returned.
  if (span.offsetWidth &gt; width) {
    var posStart = 0, posMid, posEnd = str.length, posLength;
    // Calculate (posEnd - posStart) integer division by 2 and
    // assign it to posLength. Repeat until posLength is zero.
    while (posLength = (posEnd - posStart) &gt;&gt; 1) {
      posMid = posStart + posLength;
      //Get the string from the begining up to posMid;
      span.innerHTML = _escTag(str.substring(0,posMid)) + '&amp;hellip;';

      // Check if the current width is too wide (set new end)
      // or too narrow (set new start)
      if ( span.offsetWidth &gt; width ) posEnd = posMid; else posStart=posMid;
    }

    result = '&lt;abbr title=&quot;' +
      str.replace(&quot;\&quot;&quot;,&quot;&amp;quot;&quot;) + '&quot;&gt;' +
      _escTag(str.substring(0,posStart)) +
      '&amp;hellip;&lt;\/abbr&gt;';
  }
  document.body.removeChild(span);
  return result;
}

以上是关于javascript 将字符串“hAPPY,i'm a student,NOW”中的大写字母转换成小写字母,小写字母转换成大写字母。的主要内容,如果未能解决你的问题,请参考以下文章

javascript 将字符串解析为数字javascript ES6

Javascript:将字符串转换为数字?

javascript 怎么将字符串转换为ArrayBuffer

javascript 如何将字符串转换成数字,小数!

将列表字符串从 C# 传递到 JavaScript

javascript将字符串传递给本机objective-c