Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Functions

Status
Not open for further replies.

Bignewbie

Programmer
Feb 22, 2001
351
PH
Hi guys,

old and dave, i hope u guys help me.


how can i change a specific character string into another. for example, in this string: "I AM HERE" i want it to change to: "I AM THERE". i only have to detect the substring: "HERE" and change it to: "THERE". i search the string function in flash help, it didnt gave me anything. Help guys. thanks and Godspeed!



biggie
 
OK, thinking on the fly here (just to make a change!) you have a couple of options, first off is to find the relevant phrase:

1) Use the indexOf command (or lastIndexOf) to find it. This works something like this...

myString = "Hello my name is PetitPal";
myIndex = myString.indexOf("my");

This would return the value 6 in myIndex, which is the position of "my". The only problem with this method is that if the phrase appears twice, it will only return the first instance, or last if you use lastIndexOf. The bit you're searching for could also be part of another phrase (e.g.: Searching for 'here' and the word 'where' is in the string).

2) The second, more techy option, is to treat the string as an array an work through each character until you find the phrase. I was going to try and write out the functions but I don't have time today (work, eh?) so have a go and if you can't do it I'll try and get it done soon!

You can use the command...

x = <myString>.charAt(<pos>);

... (where <mystring> is the string and <pos> is the character position in the string) to read characters out of a string.

Now, assuming that you have found your phrase, all you need to do is change the letter; if it is just a case of changing a letter for a letter you can use the charAt() command to set the character (I think...), e.g.:

myString.charAt(6) = &quot;W&quot;;

If you need to insert a letter I suppose you will have to copy from the string into another one, e.g. ...

myNewString.charAt(i) = myOldString.charAt(i);

...until the point where you need to insert your letter, in which case put it in then continue copyign between the strings.

Anyway, a lot of this is speculation, many many appologies for not being much clearer; just got lots to do at work! Augh!

Good luck and give me a scream (or someone with a little more clarity!) if you need any pointers!

X-)

PetitPal.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top