Page 1 of 2

String Manipulation

Posted: Tue Jan 23, 2007 7:20 pm
by dsdoubt
Hi,

Input Data

ABC_xxx
ABC1_xxx
ABC2_xxx
yyy_ABC3_xxxx

I need to strip out all those characters to the left of _ that were before xxxx.
xxxx is just representation of string. So I need to strip out any char '...ABCn_'
n may or may not presetn. Some characters before ABC may or may not present.
An pointers?

Re: String Manipulation

Posted: Tue Jan 23, 2007 7:28 pm
by ShaneMuir
So you are only requiring the last bit of the field past the _? Ie the _xxxx will always be at the end? If so the easiest way would be to treat the underscore as a delimiter.
Perform a count of how many underscores there are in the field then use that number in the Field function to extract the last "column" as defined by your delimiter (which is '_'). You will probable need to add 1 to the number returned by the count. The syntax would be something like

Code: Select all

InputField['_',Count(InputField,'_')+1,1]
Unfortunately I don't have access to a system at present, so I can't check the syntax, but it would be something like that.
dsdoubt wrote:Hi,

Input Data

ABC_xxx
ABC1_xxx
ABC2_xxx
yyy_ABC3_xxxx

I need to strip out all those characters to the left of _ that were before xxxx.
xxxx is just representation of string. So I need to strip out any char '...ABCn_'
n may or may not presetn. Some characters before ABC may or may not present.
An pointers?

Re: String Manipulation

Posted: Tue Jan 23, 2007 7:42 pm
by I_Server_Whale
ShaneMuir wrote:The syntax would be something like

InputField['_',Count(InputField,'_')+1,1]

Unfortunately I don't have access to a system at present, so I can't check the syntax, but it would be something like that.
Yes. Shane's code will work. Here's the right syntax:

Code: Select all

Field(InLink.Col,'_',Count(InLink.Col,'_')+1,1)
Whale.

Posted: Tue Jan 23, 2007 7:51 pm
by ray.wurlod
Both syntaxes are correct (Shane's is called "delimited substring extraction") and will generate exactly the same object code.

Posted: Tue Jan 23, 2007 8:02 pm
by dsdoubt
Thanks for you inputs guys. I was extracting from Database, so manage to use database functions.

Code: Select all

 SUBSTR(field, INSTR(field, '_',INSTR(article_id, 'ABC'))+1)

Posted: Tue Jan 23, 2007 8:04 pm
by I_Server_Whale
ray.wurlod wrote:Both syntaxes are correct (Shane's is called "delimited substring extraction") and will generate exactly the same object code.
Thanks, Ray. Didn't know about "delimited substring extraction".

Posted: Tue Jan 23, 2007 9:34 pm
by dsdoubt
May I know what is the syntax of this.
Is it InputField[Delimiter,Start, End]?

Posted: Tue Jan 23, 2007 9:43 pm
by DSguru2B
Read about Field(). It works along the same lines.

Posted: Tue Jan 23, 2007 10:21 pm
by ray.wurlod
InputField[delimiter,start,count]

The delimiter asserts that you are dealing with a string (InputField) made up of zero or more substrings separated from each other by delimiter characters. For example "STRAWBERRY ICE CREAM" is a string containing 20 characters, or a string containing three words (substrings separated by " " characters), or a string containing four substrings delimited by "E", and so on.

When delimiter appears, the start and count integers refer not to individual characters but to delimited substrings. Thus, for example, if the variable Product contains "STRAWBERRY ICE CREAM" then the expression
Product["E",2,2] returns "RRY ICE CR" - as you can see, intervening delimiter characters are returned.

Finally, note that the third argument is a count of substrings to return, not the end position. In this respect your understanding was imprecise.

Posted: Tue Jan 23, 2007 10:41 pm
by narasimha
Wonderful explaination. Thanks Ray!

Posted: Wed Jan 24, 2007 1:55 am
by kumar_s
If I would have answered, I would simply given InputField[ delimiter , start , count ]. Thats 'Ray'ish way.

Posted: Wed Jan 24, 2007 8:03 am
by chulett
It's known around here as The Full Wurlod. :wink:

Posted: Wed Jan 24, 2007 8:18 am
by DSguru2B
Thats a good practice. Makes it clear to everyone. Ray, you should consider writing a book. It will be a great seller, and you can call it "DataStage for dummies" :wink:

Posted: Wed Jan 24, 2007 8:19 am
by chulett
Hmmm... I'll let Ray respond to that.

Posted: Wed Jan 24, 2007 8:25 am
by DSguru2B
chulett wrote:Hmmm... I'll let Ray respond to that.
Not possible huh? Legal issues .... :?: