Zulip Chat Archive

Stream: new members

Topic: How to use Substring?


Adomas Baliuka (Dec 01 2023 at 16:10):

I'm confused how to use substrings. What I want to do is

def s := "asdf"
#eval s[1:3]?  -- should give `some "sd"`

In the Substring type, the only way it seems to be instantiated (according to loogle) is the constructor, while the start and end positions are wrapped. Surely, this isn't the way to do it?!

-- I assume this is horrible!
#eval {str:="asdf", startPos:=⟨1⟩, stopPos:=⟨3 : Substring}

(this works and just trims the positions to the string instead of giving an Option, which is fine too, I guess).

Kyle Miller (Dec 01 2023 at 16:18):

Do you want a Substring per se, or are you wanting to get a sub-String? There's docs#String.extract for the latter

Adomas Baliuka (Dec 01 2023 at 16:21):

I guess what I really want is notation like "asdf"[1:3]. extract seems to do this but I don't really understand why I need to wrap the positions (presumably, writing "asdf".extract ⟨1⟩ ⟨3⟩).

Kyle Miller (Dec 01 2023 at 17:22):

It's a warning that these are byte positions, so you might not be getting a valid utf8 string after extracting.

Adomas Baliuka (Dec 01 2023 at 17:23):

Ok, I guess that makes sense. That's also the reason then why the slice notation is not supported.


Last updated: Dec 20 2023 at 11:08 UTC