Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

How get 4 characters from a string of characters

If I have this string of characters in a text field on a form...  "80522526J142-101"    How can I "pull" the "J142" portion of it out for use in another text field on a form?  It will always be the 4 characters prior to the hyphen.
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try

res: Mid("yourString",Instr(1,"yourString", "-")-5,Instr(1,"yourString", "-")-1)

Regards
You could do this with a simple Regex. The following will match the 4 characters immediately preceeding a '-' character.

var testString = '80522526J142-101';

alert(testString.match(/(.{4})(?=-)/));

Open in new window

Corrected Code

res: Mid("yourString",Instr(1,"yourString", "-")-5,4)
Avatar of SteveL13

ASKER

This didn't work:  Mid("yourString",Instr(1,"yourString", "-")-5,Instr(1,"yourString", "-")-1)


and

what is Regex?
Sorry, slight edit to above...
var testString = '80522526J142-101';

alert(testString.match(/(.{4})(?=-)/)[0]);

Open in new window

This is just in case your string contains more than 1 '-' character. If you need to respond to multiple matches then the logic gets a little more complex.
Actually I'm trying to do it in a query.  Like:

User generated image
But when I try to run the query I get "Invalid procedure call".

???
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Had to modify slightly:

Code: Mid([PartN],InStr(1,[PartN],"-")-4,4)