Jump to content

Some Questions About String Methods/Properties


violagirl
 Share

Recommended Posts

1. There is a global search for the replace() method. Is there anything resembling this for the search() method? What I want to do is have it search through a document and give me the value for EVERY TIME it comes up. Since there could probably be a lot, it would probably be easiest to do through an array, I suppose. But either way, I couldn't figure out how to have it give me anything except the position of the FIRST occurance of the word/whatever I'm searching for. Is there any way to do this?2. Is there any way to use the split() method (or perhaps there is another method existing which would do this task) so it will split at a character but not erase the character itself? For example, let's say I had var txt = "How were you feeling yesterday?" and I wanted to split it up so it divided every time AFTER an e, so txt[0] would equal "How we", txt[1] would equal "re", txt[2] would equal " you fe", txt[3] would equal "e", txt[4] would equal "ling ye", txt[5] would equal "ste", and txt[6] would equal "rday?". Is there any way to do this? Right now I have it so every time e occurs the e dissapears and it splits by it. But I don't want the e to actually dissapear, just split before/after it. Does anyone know how to do this?3. How are slice() and substring() different, save for substring() being considerably older? I noticed the defintions differed slightly, as slice() said: The slice() method extracts a part of a string and returns the extracted part in a new string. and substring() said: The substring() method extracts the characters in a string between two specified indices. Which led me to believe that MAYBE the difference is that you couldn't store obj.substring() in a variable, but the following code worked perfectly well and the same way whether I used slice() or substring()! So what is the difference between the two, if there IS one (and if there isn't, why are they two seperate methods!!!)

<script type="text/javascript">var txt = "Howdy partner!";var txt2 = txt.substring(6,13); //or var txt2 = txt.slice(6,13);document.write(txt2);</script>

4. Speaking of those methods... for slice(), substr(), and substring(), it was mentioned that you can extract characters from the end of a string by using a negative start number. I couldn't figure out a: what they meant by this and b: how to do it. For example, if I extracted from the end of the string "Monkeys!", would it mean spitting out something like "!sy" or the order would still be "ys!"? This is what I attempted (not exactly sure what I wanted, but it didn't work either way :) ):

<script type="text/javascript">var txt = "Howdy partner!";var txt2 = txt.substring(-2,13);document.write(txt2);</script>

As it treated it exactly as if I had typed txt.substring(0,13), I realized I must have done it incorrectly, but I'm not sure the correct way of doing this. :) So could somebody explain it to me?5. In this piece of code from the tutorial, why did they set the new property to null? I couldn't figure out the purpose of it!!!

function employee(name,jobtitle,born){this.name=namethis.jobtitle=jobtitlethis.born=born}var fred=new employee("Fred Flintstone","Caveman",1970)employee.prototype.salary=nullfred.salary=20000document.write(fred.salary)

6. Why does this code work? I would have thought that as I am using perameters that are actually undefined in the function, it would come up as undefined.

<html><head><script type = "text/javascript"><!--function Pets(one,two,three) { this.one=one; this.two=two; this.three=three; };//--></script></head><body><script type = "text/javascript"><!--var One = new Pets("Melvin","Pumpkin","Pineapple");One.four = "Silly!";document.write(One.four);//--></script></body></html>

7. Why won't this work!!! :is frustrated:

<html><head><script type = "test/javascript"><!--function what (x) { if (x.toLowerCase().match("silly") == "silly")   {   return ("That's right!<br />");   } else   {   return ("Wrong!<br />");   } }//--></script></head><body><script type = "text/javascript"><!--document.write(what("Sillymonkeys!"));document.write(what("Shush!"));//--></script></body></html>

Link to comment
Share on other sites

1. Look into using match():

var str = "this is a test is a test is a test is a test is a test";alert(str.match(/test/g));

2. You might try using replace() to replace all instances of "e" with "e|" and then split on the "|" character.3. It doesn't look like there is any difference between those two methods. I tend to use substr() rather than slice() or substring(). I didn't even know slice() existed until I read this post. :)4. I don't use negative numbers much when I use these methods, but this example may help:

var str = "Hello world!";alert(str.substring(5, -5)) // -> "Hello"

It looks like it starts at position 5 and then returns the previous 5 characters (because of the negative number).5. I believe they are setting that to "null" because they are defining a new property using prototype. Simply writing this wouldn't do anything so they had to set it to some value - and they chose null:

employee.prototype.salary; // wouldn't do much of anything

However, since javascript is really lax about how it deals with properties and variables, you could just have easily done this:

//employee.prototype.salary=nullfred.salary=20000

This would have given fred, a single instance of type employee, the property "salary" and assigned the value of 20000 to it. All other instances of employee would not have that property. So if you had an employee object named "mary", and you tried to use alert(mary.salary), you would get an undefined error. However, if you declare, using the prototype, that all instances of employee are to have a property called "salary", alert(mary.salary) would return null.6. See #5.7. match() returns an array. So, in your example:

if (x.toLowerCase().match("silly")[0] == "silly")

But, you could just as easily do:

if(x.toLowerCase().match("silly"))

Link to comment
Share on other sites

7. match() returns an array. So, in your example:
if (x.toLowerCase().match("silly")[0] == "silly")

But, you could just as easily do:

if(x.toLowerCase().match("silly"))

Well, first of all, I didn't know that match() DID return an an array, so thank you for your tidbit. However, I tried putting in both of those and neither of them worked! There must be another problem with the code. :)
Link to comment
Share on other sites

Well, first of all, I didn't know that match() DID return an an array, so thank you for your tidbit. However, I tried putting in both of those and neither of them worked! There must be another problem with the code. :)
No problem, I just learned about match myself.It looks like the problem was that you had this in your first block of code:<script type = "test/javascript">rather than<script type = "text/javascript">
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...