substring
Moderators: chulett, rschirm, roy
substring
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.
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.
-
- Premium Member
- Posts: 209
- Joined: Fri Jan 09, 2004 1:14 pm
- Location: Toronto, Canada
- Contact:
Re: substring
Poorna,
Are you trying to test for the presence of the string? Or perhaps the location of the string?
You could do:
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.
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)
HTH
Rob W.
Rob Wierdsma
Toronto, Canada
bartonbishop.com
Toronto, Canada
bartonbishop.com
-
- Charter Member
- Posts: 130
- Joined: Mon Sep 06, 2004 3:05 am
- Location: Dubai,UAE
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![Smile :)](./images/smilies/icon_smile.gif)
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
![Smile :)](./images/smilies/icon_smile.gif)
Warm Regards,
Amruta Bandekar
<b>If A equals success, then the formula is: A = X + Y + Z, X is work. Y is play. Z is keep your mouth shut. </b>
--Albert Einstein
Amruta Bandekar
<b>If A equals success, then the formula is: A = X + Y + Z, X is work. Y is play. Z is keep your mouth shut. </b>
--Albert Einstein
![Laughing :lol:](./images/smilies/icon_lol.gif)
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?
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
Re: substring
Technically, you will have to know or determine the position of the substring if you truly want to extract it.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.
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 :wink:](./images/smilies/icon_wink.gif)
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
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
-craig
"You can never have too many knives" -- Logan Nine Fingers
"You can never have too many knives" -- Logan Nine Fingers
Sunshine, just an FYI. If you wanted to go crazy you could shorten that all down to one expression:
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 :wink:](./images/smilies/icon_wink.gif)
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.
Code: Select all
Substrings(Input.Sentence,Index(Input.Sentence,ExtractWord,1),Len(ExtractWord))
![Wink :wink:](./images/smilies/icon_wink.gif)
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.
-craig
"You can never have too many knives" -- Logan Nine Fingers
"You can never have too many knives" -- Logan Nine Fingers
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
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.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.