#How can I "extract" a part of a user string input, to put it into a variable?

1 messages · Page 1 of 1 (latest)

stable rune
#

I am trying to make a code using hashmaps such that if a user types the following string: "put(k,v)" and presses enter, I will add the corresponding key-value pair into the hashmap M. (K and V are both strings of variable length).

but the issue is, how can I get that "k" and "v" value to put into a different variable, so that I can use the command M.put("k","v")?

Currently, my main idea is to run a for loop through the entire string, and when I hit "(", start collecting each character into a s1 string until ",", then start collecting each character into another s2 string until ")", but this is very tedious and I'm not even sure if it will work properly.

Are there any easier, more convenient ways to do this?

urban shoalBOT
#

<@&987246399047479336> please have a look, thanks.

covert smelt
#

congrats, you are trying to make a parser

#

you need to make a clear definition of what you want to accept as key and value

#

for simpler things like this, you can use regex to parse the string into groups, then extract those groups

stable rune
#

not sure what that is, but I'm taking a look now

night solstice
stable rune
#

yep, I think I got it working now, thanks!