Searching for parts of a string

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

paues
Posts: 218
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Searching for parts of a string

Postby paues » Wed Apr 18, 2012 1:44 am

Is there any built-in function to search for parts of a string? If we, for example, have the string "fivewordswrittenasone" and want to search to see if the substring "words" can be found therein? That is, I am looking for a function that relates to @wfind as @mid relates to @wmid and thus would return the same as the code below.

Code: Select all

%str="fivewordswrittenasone" %sub="words" !strlen=@len(%str) !sublen=@len(%sub) for !i=1 to !strlen-!sublen+1 %piece=@mid(%str,!i,!sublen) if %piece=%sub then %return=%piece exitloop endif next

trubador
Did you use forum search?
Posts: 1520
Joined: Thu Nov 20, 2008 12:04 pm

Re: Searching for parts of a string

Postby trubador » Wed Apr 18, 2012 2:46 am

I am not sure if this is what you want, but you can try the following:

Code: Select all

!return = @instr(%str,%sub)
which will yield 5. If %sub had not existed in the base string %str, then it would have returned zero.

The following will give you the same result as your code:

Code: Select all

%return = @mid(%str,@instr(%str,%sub),@length(%sub))

paues
Posts: 218
Joined: Fri Apr 15, 2011 7:16 am
Location: Stockholm, Sweden

Re: Searching for parts of a string

Postby paues » Wed Apr 18, 2012 3:22 am

That was exactly what I was looking for :-) Thank you!


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests