MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to find the position of a substring within a string.

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming
View previous topic :: View next topic  
Author Message
dubasir
Beginner


Joined: 13 Dec 2004
Posts: 20
Topics: 9

PostPosted: Mon Dec 27, 2004 1:53 am    Post subject: How to find the position of a substring within a string. Reply with quote

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
View user's profile Send private message
Phantom
Data Mgmt Moderator
Data Mgmt Moderator


Joined: 07 Jan 2003
Posts: 1056
Topics: 91
Location: The Blue Planet

PostPosted: Mon Dec 27, 2004 5:43 am    Post subject: Reply with quote

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
View user's profile Send private message
whizkid79
Beginner


Joined: 29 Sep 2004
Posts: 53
Topics: 14

PostPosted: Tue Jan 10, 2006 8:57 pm    Post subject: Reply with quote

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
View user's profile Send private message
semigeezer
Supermod


Joined: 03 Jan 2003
Posts: 1014
Topics: 13
Location: Atlantis

PostPosted: Tue Jan 10, 2006 10:21 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
whizkid79
Beginner


Joined: 29 Sep 2004
Posts: 53
Topics: 14

PostPosted: Tue Jan 10, 2006 11:49 pm    Post subject: Reply with quote

It worked.


Thanks,
Whizkid79.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Application Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group