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);
参考技术Cbyte 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("<","&lt;").replace(">","&gt;");}
//Create a span element that will be used to get the width
var span = document.createElement("span");
//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 > 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) >> 1) {
posMid = posStart + posLength;
//Get the string from the begining up to posMid;
span.innerHTML = _escTag(str.substring(0,posMid)) + '&hellip;';
// Check if the current width is too wide (set new end)
// or too narrow (set new start)
if ( span.offsetWidth > width ) posEnd = posMid; else posStart=posMid;
}
result = '<abbr title="' +
str.replace("\"","&quot;") + '">' +
_escTag(str.substring(0,posStart)) +
'&hellip;<\/abbr>';
}
document.body.removeChild(span);
return result;
}
以上是关于javascript 将字符串“hAPPY,i'm a student,NOW”中的大写字母转换成小写字母,小写字母转换成大写字母。的主要内容,如果未能解决你的问题,请参考以下文章