extracting parts of a string

A

Abby Lee

I have a string with 10 charaters 123456789A.
I only want the first 6 charaters followed by the last two...getting
rid of the 7th and 8th charaters.

If I use document.forms[0].MyTextBox.value =
TheString.substring([0-5])...I get the first 6 charaters.
([0-5],8,9) does not work nor does ([0-5],[8-9]) and I get just as bad
results with (0,1,2,3,4,5,8,9)

how do I get a string with the 7th and 8th charaters missing?
 
M

Michael Winter

I have a string with 10 charaters 123456789A.
I only want the first 6 charaters followed by the last two... getting
rid of the 7th and 8th charaters.

If I use document.forms[0].MyTextBox.value =
TheString.substring([0-5])...I get the first 6 charaters.
([0-5],8,9) does not work nor does ([0-5],[8-9]) and I get just as bad
results with (0,1,2,3,4,5,8,9)

I'm not quite sure what your expectations are, but the substring method
only takes two arguments: the zero-based index of the starting character,
and the index of a second character. The returned string doesn't include
the character specified by the second index.
how do I get a string with the 7th and 8th charaters missing?

To do what you want, you'll need to call the method twice:

str.substring(0, 6) + str.substring(8)

Mike
 
M

Mick White

Abby said:
I have a string with 10 charaters 123456789A.
I only want the first 6 charaters followed by the last two...getting
rid of the 7th and 8th charaters.

If I use document.forms[0].MyTextBox.value =
TheString.substring([0-5])...I get the first 6 charaters.
([0-5],8,9) does not work nor does ([0-5],[8-9]) and I get just as bad
results with (0,1,2,3,4,5,8,9)

how do I get a string with the 7th and 8th charaters missing?

theString="123456789A"
alert(theString.substring(0,6)+theString.substring(8))

Mick
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Wed, 1 Sep 2004 08:49:48, seen in Abby
Lee said:
I have a string with 10 charaters 123456789A.
I only want the first 6 charaters followed by the last two...getting
rid of the 7th and 8th charaters.

If I use document.forms[0].MyTextBox.value =
TheString.substring([0-5])...I get the first 6 charaters.
([0-5],8,9) does not work nor does ([0-5],[8-9]) and I get just as bad
results with (0,1,2,3,4,5,8,9)

how do I get a string with the 7th and 8th charaters missing?

Str1 = "123456789A"
Str2 = Str1.replace(/^(......)../, "$1") // 1234569A
or Str2 = Str1.replace(/^(.{6}).{2}/, "$1") // 1234569A

Method is best suited to fixed patterns, but can be adapted for variable
numbers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,433
Messages
2,571,683
Members
48,796
Latest member
Greg L.

Latest Threads

Top