Closed Thread Icon

Preserved Topic: How do I determine the casing of a letter? Pages that link to <a href="https://ozoneasylum.com/backlink?for=18568" title="Pages that link to Preserved Topic: How do I determine the casing of a letter?" rel="nofollow" >Preserved Topic: How do I determine the casing of a letter?\

 
Author Thread
Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 08-16-2002 20:35

Lets say i wanted to take all of the capital letters of a string and delete them; how would i determine if a letter is a lowercase or an uppercase letter?

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 08-17-2002 05:59

You're best bet is probably a regular expressions
say you have a string called "str"

then you need a regular espression to match uppercase letters, this is pretty easy

re = /[A-Z]/g;


then replace anything that matches the regexp.
str.replace(re,"");



.:[ Never resist a perfect moment ]:.

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-17-2002 07:26

it's for perl, but I think the regexp's work the same..


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 08-17-2002 09:37

usually to do that from the very low level you have to check each character's ascii code range,
for example the upper case 'A' has the ascii code of: 65 (try to press ALT+65 on your keyboard) now add 25 to 65 you end up having the ascii code of the 'Z' .

the lowercase 'a' has ascii code of 97 (97-65 = 32) therefore you can convert from lowercase to uppercase by adding or substracting 32 from the characters ascii code.

hope that helps.

as for js implementation, refer to String.charCodeAt(index) and .fromCharCode()


Elias

Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 08-17-2002 14:53
code:
function reversecaps() {

invcasing=document.f.source.value
var outcase=""
for (var letter=0;letter<invcasing.length;letter++) {
str=invcasing.charCharCodeAt(invcasing[letter])
if (str>96 && str<122) { newoutcase = str.fromCharCode(str-65); } else {newoutcase = str.fromCharCode(str+65);}
outcase=outcase+newoutcase
}
document.f.source.value=outcase }


doesn't work. can't figure it out.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 08-17-2002 17:44

You have a few errors in your code. Here's the fixed version:

function reversecaps()
{
invcasing=document.f.source.value
var outcase=""
for (var letter=0;letter<invcasing.length;letter++) {
&nbsp;&nbsp;&nbsp;&nbsp;str=invcasing.charCodeAt(letter)
&nbsp;&nbsp;&nbsp;&nbsp;if (str>96 && str<123) { newoutcase = String.fromCharCode(str-32); } else { newoutcase = String.fromCharCode(str+32); }
&nbsp;&nbsp;&nbsp;&nbsp;alert(String.fromCharCode(str)+" "+newoutcase);
&nbsp;&nbsp;&nbsp;&nbsp;outcase=outcase+newoutcase
}
document.f.source.value=outcase
}



Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 08-17-2002 17:47

pretty elegant solution, lallous..


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Hugh
Paranoid (IV) Inmate

From: Dublin, Ireland
Insane since: Jul 2000

posted posted 08-17-2002 18:24

I havent read the other posts but:
if(thestring.toUppercase()==thestring) alert('the string is upper case !');

Im not sure on the spelling of toUppercase I think thats it.

to lets say count how many are upper case:
for(i=0;i<thestring.length;i++) { if(thestring[i].toUppercase()==thestring[i]) amountuppercse++; }
that should work, I cant remember if JS saves strings as arrays, you might need theString.charAt(i), I think, its been a while since I'vev used JS to mess with strings.

....

just looking at Lallous' idea, its seems a little nicer and probably less work for the CPU.

« BackwardsOnwards »

Show Forum Drop Down Menu