PDA

View Full Version : have a variable extract first letter from another variable


Erica_qt
03-25-2003, 09:40 PM
I need a javascript script where one variable can extract the first letter of another variable. The following is a simplified version of what I have so far, but it has only what is required for you to know what I'm talking about, and how it would be used; I just don't know what to put in the var letter="" line. By the way, in this example, I'm the variable "letter" should be "b".<P>

<script language="javascript">
<!-- Begin
var word="blah blah"
var letter=""

document.write(" The first letter of the word \""+word+"\" is the letter \""+letter+"\".");

// End -->
</script>

Thanks,
Erica

cheesebagpipe
03-25-2003, 09:51 PM
http://www.devguru.com/Technologies/ecmascript/quickref/string_charat.html

Erica_qt
03-25-2003, 11:30 PM
Cool, thanks! That's even more usefull than what I had in mind!

edit:
Wait, I spoke too soon. I need a variable to equal the first letter of the word, so that I can have it be like

if the first letter is "a" then do this, but if it equals "b", then do this. Of course, that's english and not javascript. I have a basic idea of how to do it in code, like

if (letter=a) {
WHAT IT'LL DO
}
else if (letter=b){
SOMETHING ELSE FOR IT TO DO
}
etc.

Erica_qt
03-25-2003, 11:56 PM
Oh, I sorta figured it out, I used this to get it:

myString = new String("blah blah")
fletter = myString.charAt(0)

But what am I doing wrong? The following script always incorrectly tells me that character 0 (the first letter) is a

myString = new String("blah blah")
fletter = myString.charAt(0)
if (fletter="a")
document.write("character 0 is a")
else if (fletter="b")
document.write("character 0 is b")
else if (fletter="c")
document.write("character 0 is c")
else if (fletter="d")
document.write("character 0 is d")

cheesebagpipe
03-26-2003, 12:06 AM
Erica...

Not necessary to wrap a primitive string ("blah blah") in a String object; the browser will do this for you, as needed. Don't forget:

= ....assignment operator (this equals that)
==....comparison operator (does this equal that?)

Keep in mind that JS strings are immutable - you can't move characters in-and-out, only create new strings from the characters in other ones.

http://www.xs4all.nl/~ppk/js/strings.html

Jason
03-26-2003, 12:12 AM
Note: double quotes are used for strings while single quotes are used for characters.


Jason

Erica_qt
03-26-2003, 12:20 AM
Oh, I see. So you're saying that I didn't need to put "blah blah" inside of "new String ( )". I was just using the script on the URL you gave me as a template, and that's how they had it, but I see what you mean.

I didn't really know that because I am a decent HTML, JavaScript, and XML programmer, but I never read tutorials or used classes to learn what I know... I just stared at other's scripts untill it made sense enough to be able to do stuff on my own, but sometimes I use portions of existing scripts if it is similar to what I want to do, and tweak it to my pleasing. Every time I see a cool script on a site, I just save the source to a folder I have in case I ever want to do something like that in the future.

I need to learn C++ for a program me and some friends are trying to make, unfortunately it's way too complex for me to learn the same way I learned HTML, JavaScript and XML though. So I'm taking C++ programming courses right now.

cheesebagpipe
03-26-2003, 12:33 AM
I hope you won't misunderstand when I say that 'staring' at JS is not a suitable way to learn it. Take the above: you skipped the chapter on "Operators in JS", and so didn't know the difference between assignment (=) and comparison(==). Perfectly OK - we all started somewhere - until you actually start writing scripts using these core concepts, and can't figure out why they're not working. If you were taking a JS course (or any other programming language) the instructor simply wouldn't permit you to proceed to step 2 until you'd mastered step 1, and so on. The application of a little self-discipline here (a book!) will save you loads of time later; don't be minute-wise and hour-foolish, so to speak.

The above comments on double-vs-single quotes are similar: completely invented.....

var myString = "blah blah";
var fletter = myString.charAt(0);
if (fletter == "a") alert("character 0 is a");
else if (fletter == "b") alert("character 0 is b");
else if (fletter == "c") alert("character 0 is c");
else if (fletter == "d") alert("character 0 is d");

Jason
03-26-2003, 12:34 AM
thats what the computer programing forum is for ;-)

Jason