PDA

View Full Version : simple string


septopus
04-09-2008, 11:51 PM
trying to do a simple search and replace function

for instance,

in js

var oString = ""hello"

var stringLength = oString.length

is there a way to get what character is at index 0 or 1 or 2 etc...
for instance the first element in "hello" element 0, would be "h"
how can i log this
seems so simple... -_-

without using the stringSearch) function.

CiaranM
04-10-2008, 03:10 AM
Hi,
have you tried the charAt() method?

var myString = "bozai";
var z = myString.charAt(2);

Try devguru.org for a complete reference to Javascript (mostly applicable to JScript).

:biggrin:

septopus
04-10-2008, 05:58 AM
thanks!.

ive been on there searching up and down actually. :)

StephenBlair
04-10-2008, 08:44 AM
For search and replace why not look into regular expressions?

septopus
04-10-2008, 03:21 PM
For search and replace why not look into regular expressions?
how would i do this without the charAt() function?


thats the way i can think of doing it right now? am i doing it the hard way.

im really making a mirror function for an auto rig
animator select left arm, or left anything ( "L") -click mirrors, and find counterpart right ("R") control object.
then apply transforms.

-- i have naming conventions im adhering by throughout pipelines.--

can you give quick example of search and replace? im not changing the name to something else.
just checking if string exists (under a model).

thanks for help.

StephenBlair
04-10-2008, 04:19 PM
Here you go...

var s = "LArm"
LogMessage( s.replace( /^[L]/i, "R" ) );

s = "lArm"
LogMessage( s.replace( /^[L]/i, "R" ) );

s = "LeftArm"
LogMessage( s.replace( /^Left/i, "Right" ) );


s = "LArm"
var tmp = s.replace( /^Left/i, "Right" )
LogMessage( tmp );
tmp = tmp.replace( /^L/i, "R" )
LogMessage( tmp );

septopus
04-10-2008, 07:51 PM
oh thanks stephen. i dont work much with strings... so this is good stuff for me. saves me some time.
http://www.devguru.com/technologies/javascript/10812.asp
i saw on devguru that i =ignorecase g= global match.

this is very helpful. also if i ommit the "^" it searches the entire word. which is what i want. so
this is leading me in the right direction.
thanks.

kim aldis
04-11-2008, 03:15 AM
Jscript strings have two methods that use regexp; s.replace() and s.match(). Both work much the same way. regular expression syntax can be daunting when you first look at them but they're extremely powerful, especially when working with naming conventions. Here's some links that might be useful:

http://www.webreference.com/js/column5/
http://www.javascriptkit.com/javatutors/redev2.shtml
http://www.regular-expressions.info/javascriptexample.html