View previous topic :: View next topic |
Author |
Message |
dubasir Beginner
Joined: 13 Dec 2004 Posts: 20 Topics: 9
|
Posted: Mon Dec 27, 2004 1:53 am Post subject: How to find the position of a substring within a string. |
|
|
Is there any function available in COBOL which returns the starting position of a substring given within a string. Using Perform I can do it ....but the string lenght is > 26000 bytes. So any better option that would work for my requirement.....so that I can have a better performance.
Eg : STRING = 'INDIA IS GREAT'.
SUBSTRING = 'IS'
When I pass the 2 arg's , it should return the position as 7. |
|
Back to top |
|
 |
Phantom Data Mgmt Moderator

Joined: 07 Jan 2003 Posts: 1056 Topics: 91 Location: The Blue Planet
|
Posted: Mon Dec 27, 2004 5:43 am Post subject: |
|
|
dubasir,
Did u try using the INSPECT verb with TALLYING option ?
Code: |
INSPECT
full_string
TALLYING
count_variable
FOR LEADING
sub_string
|
The position of substring will be stored in the 'count_variable'.
Note: Please pad spaces before and after your sub_string to get the actual location of the substring. And then u will have to add '1' to the counter_variable to account for the leading space. This is necessary since INSPECT performs a character level search and hence if you do not prefix the substring with a space u might end up with a wrong value.
Example:
Code: |
STRING = 'FISHING IS PROHIBITED IN THIS AREA'
SUBSTRING = 'IS'
In this case, tally count will actually return '2' since there is an 'IS' in the word FISHING. If you pad spaces on both sides of substring, this error will not happen.
|
Hope I'm clear
Thanks,
Phantom |
|
Back to top |
|
 |
whizkid79 Beginner

Joined: 29 Sep 2004 Posts: 53 Topics: 14
|
Posted: Tue Jan 10, 2006 8:57 pm Post subject: |
|
|
Hi,
I tried this, but it isn't working. INSPECT is returning the count as 0. Please give me a solution to get the position of a substring in a string.
-Whizkid79 |
|
Back to top |
|
 |
semigeezer Supermod
Joined: 03 Jan 2003 Posts: 1014 Topics: 13 Location: Atlantis
|
Posted: Tue Jan 10, 2006 10:21 pm Post subject: |
|
|
Searching for a needle in a haystack:
Code: | MOVE 0 TO indx
INSPECT haystack
TALLYING indx
FOR CHARACTERS
BEFORE needle |
Same caveats about delimiters apply. You can always use a simple loop too. |
|
Back to top |
|
 |
whizkid79 Beginner

Joined: 29 Sep 2004 Posts: 53 Topics: 14
|
Posted: Tue Jan 10, 2006 11:49 pm Post subject: |
|
|
It worked.
Thanks,
Whizkid79. |
|
Back to top |
|
 |
|
|