...

View Full Version : Javascript Strings



zk0
02-16-2007, 04:20 PM
Hi,

I need some help with this code. I want to make all the small letters to big and all the big letters to small letters. After that I want to make the letter "A" become an "#".

It should look something like this:

This Is a Text
tHIS iS a tEXT
This is # Text

This is the code I currently work one:


var str = "This Is a Text"

if(str.toLowerCase == str) {
document.write(str.toUpperCase())
document.write("<br />")
}

if(str.toUpperCase == str) {
document.write(str.toLowerCase())
document.write("<br />")
}

I am completely lost and need help or advice. Thanks!

Kor
02-16-2007, 04:59 PM
Use Regular Expressions:
http://lawrence.ecorp.net/inet/samples/regexp-intro.php

zk0
02-16-2007, 05:00 PM
I've updated the code a bit:


var str = "This Is a Text"

if(str.toLowerCase == str) {
str = str.toUpperCase();
str = str.replace('A', '#');
document.write(str);
document.write("<br />");
}

But it still has problems. Nothing is written onto the page! :confused:

Philip M
02-16-2007, 05:46 PM
With respect to Kor, I don't see how regex will help here except for
str = str.replace('A', '#');

Here's what I have done so far, but still needs finishing off.

<SCRIPT type="text/javascript">

var str = "Hello world!"
var flag = 0;
var str1 = ""
var a = new Array();
for (var j=0; j<str.length; j++) {
flag = 0; // reset the flag
a[j] = (str.charCodeAt(j));
if (a[j] >= 65 && a[j] <=90) {
a[j] = a[j] + 32; // change upper case letter A-Z to lower case
flag = 1; // set the flag
}
if (flag==0) { // only if flag not set
if (a[j] >=97 && a[j] <=122) { // change lower case letter a-z to upper case
a[j] = a[j] - 32;
}
}
a[j] = "%" + a[j] // add % sign
str1 = str1 + a[j] //concatenate
alert (str1);

}
</script>

vwphillips
02-16-2007, 07:52 PM
or


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
function zxcES(zxcele,zxcstyle,zxcp,zxctxt){
if (typeof(zxcele)=='string'){ zxcele=document.createElement(zxcele); }
for (key in zxcstyle){ zxcele.style[key]=zxcstyle[key]; }
if (zxcp){ zxcp.appendChild(zxcele); }
if (zxctxt){ zxcele.appendChild(document.createTextNode(zxctxt)); }
return zxcele;
}

function Cng(){
var str = "This Is a Text"
zxcES('DIV',{},document.getElementsByTagName('BODY')[0],str);
str=str.split('');
for (var zxc0=0;zxc0<str.length;zxc0++){
if (str[zxc0]==str[zxc0].toUpperCase()){ str[zxc0]=str[zxc0].toLowerCase(); }
else { str[zxc0]=str[zxc0].toUpperCase(); }
}
str=str.join('');
zxcES('DIV',{},document.getElementsByTagName('BODY')[0],str);
zxcES('DIV',{},document.getElementsByTagName('BODY')[0],str.replace(/A/g,'#'));
}
//-->
</script></head>

<body onload="Cng()">

</body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum