Substring look-up

Olivier Boudeville <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <[email protected]>
Hi,

It must be a silly question, but, since the Latin1 -> Unicode switch in 
OTP 20.0, is there a (non-obsolete) way in the string module to look-up 
the index of a string into another one, i.e. to find the location of a 
given substring?

rstr/2 is supposed to be replaced with find/3, yet the former returns an 
index whereas the latter returns a part of the original string. I could 
not find a way to obtain a relevant index with any of the newer string 
functions - whereas I would guess it is a fairly common need?

To give a bit more context, the goal was to prevent the implementation 
of [1] from becoming obsolete; string:substr/3 and string:sub_string/3 
are flagged as obsolete and may be replaced by slice/3 (see [2]); yet 
what can be done for rstr/2?

(even if a smart use of some function was found to address the 
particular need of this replace_extension/3 function, obtaining indexes 
of substrings would still be useful in many cases, isn't it?)

Thanks in advance for any hint!

Best regards,

Olivier.


[1] Soon obsolete apparently:

% Returns a new filename whose extension has been updated.
%
% Ex: replace_extension("/home/jack/rosie.ttf", ".ttf", ".wav") should 
return
% "/home/jack/rosie.wav".
%
-spec replace_extension( file_path(), extension(), extension() ) -> 
file_path().
replace_extension( FilePath, SourceExtension, TargetExtension ) ->

     case string:rstr( FilePath, SourceExtension ) of

         0 ->
             throw( { extension_not_found, SourceExtension, FilePath } );

         Index ->
             string:substr( FilePath, 1, Index-1 ) ++ TargetExtension

     end.


[2] BTW there is a change in the indexing convention that could be 
better advertised in the doc:

 > string:substr("abc",1).
"abc"

 > string:sub_string("abc",1).
"abc"

 > string:slice("abc",1).
"bc"

 > string:slice("abc",0).
"abc"

-- 
Olivier Boudeville
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.