LoginLogin

Yarn - String Function Library

Root / Submissions / [.]

snail_Created:
Download:Q33J44BD
Version:1.0Size:
Yarn is a library which provides various string processing functions. All functions return new strings and will not taint the inputs.

Instructions:

SPLIT$(string$,separator$) Splits string$ into substrings at each occurrence of separator$ and returns a new string array containing every substring (including empty ones.) If separator$ is the empty string, the output array is the list of every character in the string. Commonly used with JOIN$().
DIM OUT$[0]
OUT$=SPLIT$("HELLO WORLD"," ")
FOR I%=0 TO LEN(OUT$)-1
 PRINT OUT$[I%]
NEXT
This is a function which returns an array, which usually isn't standard practice in SB. Use carefully. JOIN$(array$[],separator$) Returns a new string by joining all of the entries in array$[] together, with separator$ placed between them. Commonly used with SPLIT$().
DIM IN$[3]
COPY IN$,@FRUIT
PRINT JOIN$(IN$,",") 'APPLE,BANANA,ORANGE

@FRUIT
DATA "APPLE","BANANA","ORANGE"
REPLACE$(string$,old$,new$) Returns a new version of string$ where all occurrences of old$ are replaced with new$.
PRINT REPLACE$("HELLO WORLD","L","Q") 'HEQQO WORQD
COUNT(string$,substring$) Returns the number of times substring$ occurs in string$, as an integer.
PRINT COUNT("AABAA","A") '4
UPPER$(string$) Returns a new version of string$ where every lowercase letter is uppercase. Does not touch any other characters. Supports most (if not all) alphabets in SB's character set.
PRINT UPPER$("Hello wORLD") 'HELLO WORLD
LOWER$(string$) Returns a new version of string$ where every uppercase letter is lowercase. Does not touch any other characters. Supports most (if not all) alphabets in SB's character set.
PRINT LOWER$("Hello wORLD") 'hello world
REVERSE$(string$) Returns a backwards version of string$.
PRINT REVERSE$("HELLO WORLD") 'DLROW OLLEH
TRIM$(string$) Returns a version of string$ with leading and trailing whitespace removed. Whitespace characters are tab, space, linefeed, and carriage return.
PRINT TRIM$("  A    ") 'A
REST$(string$,start%) Returns the rest of string$ starting at start%. In other words, returns a substring of string$ which starts at start% and runs to the end of the string.
PRINT REST$("HELLO WORLD",6) 'WORLD

i love it

The Public Key does not work. Also, I found a very elegant approach to some of these, I don't know if you are using them, but these are fast algorithms that avoid excessive string operations, you should consider them, or at least look at the code:
DEF COUNT(S$,C$)
 VAR N=-1,I
 REPEAT
  I=INSTR(I,S$,C$)+1
  N=N+1
 UNTIL !I
 RETURN N
END

DEF TRIM$(S$)
 IF !LEN(S$) THEN RETURN ""
 VAR I,J=LEN(S$)-1
 WHILE S$[I]==" " I=I+1 WEND
 WHILE S$[J]==" " J=J-1 WEND
 RETURN MID$(S$,I,J-I+1)
END

Replying to:Simeon
The Public Key does not work. Also, I found a very elegant approach to some of these, I don't know if you are using them, but these are fast algorithms that avoid excessive string operations, you should consider them, or at least look at the code:
DEF COUNT(S$,C$)
 VAR N=-1,I
 REPEAT
  I=INSTR(I,S$,C$)+1
  N=N+1
 UNTIL !I
 RETURN N
END

DEF TRIM$(S$)
 IF !LEN(S$) THEN RETURN ""
 VAR I,J=LEN(S$)-1
 WHILE S$[I]==" " I=I+1 WEND
 WHILE S$[J]==" " J=J-1 WEND
 RETURN MID$(S$,I,J-I+1)
END
Sorry about that; wound up typoing the key without checking, which means nobody was able to download this the entire timeโ€ฆ :P And yeah, that's more or less what my code looks like. Any suggestions are entirely welcome of course.

feed me ropes

Replying to:Yolkai
feed me ropes
Rope - Yarn Function Library

Replying to:Yolkai
feed me ropes
I meant ropes actually.

COMMON DEF SPLIT$(A$,S$)
 VAR I%,S%,E$
 DIM O$[0]
 
 IF A$=="" THEN RETURN O$
Shouldn't this return an array of length 1?

Replying to:12Me21
COMMON DEF SPLIT$(A$,S$)
 VAR I%,S%,E$
 DIM O$[0]
 
 IF A$=="" THEN RETURN O$
Shouldn't this return an array of length 1?
Hmmmm perhaps That does make the most conventional sense but I remember basing the behavior on JS. Maybe checking the delimiter broke if the string was empty? I'll see about it.

Replying to:12Me21
COMMON DEF SPLIT$(A$,S$)
 VAR I%,S%,E$
 DIM O$[0]
 
 IF A$=="" THEN RETURN O$
Shouldn't this return an array of length 1?
Yeah I checked JS and "".split() returns [""] I think the problem was your special case of SPLIT$(A$,"") (which normally should be invalid, but is usually treated as a special case because it's more useful that way) which should return [] on an empty string,