GOLF(2gg)
π GOLF(2gg) β the split-string era π
okay bestie, welcome to the GOLF(2gg) manual, Development girlies edition π this one's all about split-string and it's genuinely so useful, no cap.
β¨ NAME β¨
split-string β (strings) π¦
πΈ PURPOSE πΈ
Split a string into pieces based on a delimiter. that's it, that's the whole vibe π
β SYNTAX β
split-string <string> with <delimiter> to <result> [ count <count> ]
split-string delete <result>
π DESCRIPTION π
so here's the tea π: split-string goes looking for all the instances of string <delimiter> inside <string>, and then it splits that string into pieces, delimited by string <delimiter>. iconique behaviour honestly.
the <result> is your little container afterwards β you can use it with read-split to get the pieces out, and you can also use it with split-string itself to delete it (just use the "delete" clause with <result> to delete it, periodt).
if you use the "count" clause, then the number of pieces ends up in <count>. so you know how many you got π₯
important glow-up detail: all the pieces produced will be trimmed on both left and right. and if a piece is double quoted, the double quotes get removed. so clean, so demure π§
for instance, save this code in "ps.golf" in a separate directory:
%% /parse public
set-string clist = "a , b, \"c , d\" , e"
split-string clist with "," to res count tot
start-loop repeat tot use i
read-split i from res to item status st
if-true st not-equal GG_OKAY
break-loop
end-if
// Output each item
print-format " [%s]", item
end-loop
%%
then create the application, build it, and run it:
sudo mgrg -i -u $(whoami) ps
gg -q
gg -r --req="/parse" --exec --silent-header
and the output would be:
[a] [b] [c , d] [e]
see how the quotes just vanished?? the way that works >>> π
split-string is lowkey perfect for parsing CSV (Comma Separated Values) or literally any other kind of separated values, where the separator can be any string of any length β for example, if you're parsing an encoded URL-string, then "&" may be a separator, as in the example below π
π₯ EXAMPLES π₯
the following will parse a string containing name/value pairs (such as "name=value") separated by string "&":
%% /parse-url public
// Data to parse - data/value pairs delimited by "&" string, and data and value delimited by equal sign:
set-string url ="x=23&y=good&z=hello_world"
// Split string first into pieces based on "amp;"
split-string url with "&" to url_var count tot
// For each of name=value pairs, split it with equal sign
start-loop repeat tot use i
// Get a url pair (each separated with "&")
read-split i from url_var to item
// Then split that with "="
split-string item with "=" to item_var
// Get name and value
read-split 1 from item_var to name
read-split 2 from item_var to val
print-format "Variable %s has value %s\n", name, val
end-loop
%%
and the result is:
Variable x has value 23
Variable y has value good
Variable z has value hello_world
ate. no crumbs. π
π«Ά SEE ALSO π«Ά
Strings π
concatenate-strings copy-string count-substring delete-string lower-string new-string read-split replace-string set-string split-string string-length trim-string upper-string write-string
See all documentation β¨
that's the whole page bestie, go split some strings, you've got this πβ¨ππ¦πΈπ π«Ά
this was $VERSION, dated $DATE, from the GOLF(2gg) manual β mwah, bye girly ππ
π¦ where this page comes from
- ubuntu questing: golf 601.4.41-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms