Substring look-up

Dan Gudmundsson dangud@REDACTED
Wed Apr 7 09:03:29 CEST 2021


There are currently no replacements for those functions,
the thought was that it was a lot more expensive to traverse the string now
so you should only traverse it once,
or you should at least think about it instead of just replacing the new api.

I believe 'string:split(File, Ext, trailing)' or why not
'string:replace(File, OldExt, NewExt, trailing)'
does what you want in this case, or you could use the 'filename' module for
handling filenames.

But yes the string api could be extended with a function or two.


On Tue, Apr 6, 2021 at 11:29 PM Olivier Boudeville <
olivier.boudeville@REDACTED> wrote:

> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210407/5e5f25d7/attachment.htm>


More information about the erlang-questions mailing list