Page 1 of 1

substring

Posted: Fri Mar 03, 2006 3:17 pm
by poorna_76
Hi All,

I would like to extract specific substring from string, without knowing the position of that substring.
That particular substring position is not fixed, it can be any where in the String.


Example:This Job Extracts New Employment Data for the Processing month Based on Current Month File

I would like to extract "Employment" from the above string.

Any thoughts?

Thanks in Advance.

Re: substring

Posted: Fri Mar 03, 2006 3:29 pm
by rwierdsm
Poorna,

Are you trying to test for the presence of the string? Or perhaps the location of the string?

You could do:

Code: Select all

MyString = "This Job Extracts New Employment Data for the Processing month Based on Current Month File" 

Position = Index(MyString, 'Employment', 1)
This would return the position of the string 'Employment'. If you check for return > 0 then it tells you if your search string is present anywhere in the string.

HTH

Rob W.

Posted: Sat Mar 04, 2006 12:16 am
by Sunshine2323
Hi Poorna,

If I understand your requirement then you want to extract a string from the input in which the location of the string to be extracted is not fixed.

For this you need to know the Length of the string to be extracted and its location in the input string

Declare StageVariables:

StgLen:Len(ExtractWord)
StgPosition:Index(Input.Sentence, ExtractWord, 1)
StgExtract:Substrings(Input.Sentence, StgPosition, StgLen)

ExtractWord: Is the string you want to extract from the input

StgExtract holds the word you want to extract.

Hope this helps :)

Posted: Sat Mar 04, 2006 3:25 am
by kumar_s
:lol: You already got the substring... What else you want from the main string.
I guess you are missing out someting.
Is it you want to know the position of where the substring present or to know the presence of the substring in the main string?

Re: substring

Posted: Sat Mar 04, 2006 8:33 am
by chulett
poorna_76 wrote:I would like to extract specific substring from string, without knowing the position of that substring. That particular substring position is not fixed, it can be any where in the String.
Technically, you will have to know or determine the position of the substring if you truly want to extract it.

However, that being said - if the substring to extract is known ahead of time - aka 'specific' as yu've noted - then (as Kumar pointed out) you don't really need to extract it, do you? :wink:

All you need to do is verify the existance of the sub-string within the string in question, and Rob has already shown you how to do that with the Index function. All I would change were I to do it is the variable name 'Position' to something I would treat as a boolean - like 'SubstringExists'. Then you can use it in an If-Then-Else construct in a manner that is easy to grok:

Code: Select all

MyString = "This Job Extracts New Employment Data for the Processing month Based on Current Month File" 

SubstringExists = Index(MyString, 'Employment', 1)

If SubstringExists Then [do something] End Else [do something else] End
To expand on what Rob posted. This works because 'false' is a zero and 'true' is any non-zero value. The system variables @FALSE and @TRUE are zero and one, respectively, so while you could have directly compared Position to @FALSE, doing the same with @TRUE would only have worked in one very specific scenario.

If you aren't planning on evaluating the result of this more than once, there's no need to store the intermediate result. A little less readable but perfectly fine:

Code: Select all

MyString = "This Job Extracts New Employment Data for the Processing month Based on Current Month File" 

If Index(MyString, 'Employment', 1) Then [do something] End Else [do something else] End
Hope that helps,

Posted: Sat Mar 04, 2006 8:41 am
by chulett
Sunshine, just an FYI. If you wanted to go crazy you could shorten that all down to one expression:

Code: Select all

Substrings(Input.Sentence,Index(Input.Sentence,ExtractWord,1),Len(ExtractWord))
I'm sure you broke it up to illustrate the individual steps, but also wanted to point out it could all be 'inlined' and thus used in a Transform if someone wanted to. :wink:

Of course, you'd need to check the result to see if it extracted anything. I haven't tested it but pretty sure it will return a NULL if the substring does not exist within the string.

Posted: Sat Mar 04, 2006 9:11 pm
by ray.wurlod
It will return the original string. If 0 is used as the the argument in Substrings(), it is replaced with 1. It's also documented thus in the DataStage BASIC manual.

Posted: Sat Mar 04, 2006 10:09 pm
by chulett
Ah. Thanks for the clarification, Mr Ray. :D