? Strings ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password

Strings

Strings variables are variables that hold text. A string variable's name always ends in $. (NAME$, S$, etc.) Strings in SB are 0-indexed, which means that the "first" character is actually the 0th. "ABC"[0] is A String functions: Code% = ASC(String$) -returns the ASCII value of the first character in the string. -gives an error if used on an empty string. ?ASC("ABC") 65 String$ = CHR$(Code%) -the inverse function of ASC, converts an ASCII value to a 1 character-long string. ?CHR$(65) A Number = VAL(String$) -gets the number contained in a string. -if the string is not a valid number, returns 0 ?VAL("12.3") 12.3 String$ = STR$(Number {,Digits%} ) -converts Number into a string. Inverse of VAL. If Digits% is specified, it will make the string that long, with spaces on the left. ?STR$(157,4) 157 Hexadecimal$ = HEX$(Number% {,Numberofdigits%} ) -converts a decimal Number into hexadecimal. Since hexidecimal has more than 10 numerals, it must be stored as a string. If Digits is specified, leading zeroes will be added. ?HEX$(15,3) 00F String$ = FORMAT$(format$, List {,...} ) -similar to printf in other languages -explanation coming soon (this one is pretty complicated!) Length% = LEN(String$) -returns the length of String$ ?LEN("ABC") 3 SubString$ = MID$(String$, Start%, Length%) -returns Length% characters, beginning at Start%, from String$ ?MID$("ABCD",2,2) BC -MID$(String$,Position%,1) can be replaced by String$[Position%] SubString$ = LEFT$(String$, Length%) -returns the Length% leftmost characters from String$ ?LEFT$("ABC",2) AB SubString$ = RIGHT$(string$,Length%) -returns the Length% rightmost characters from String$ ?RIGHT$("ABC",2) BC Position = INSTR({Start%,} String$, Search$) -returns the first position of Search$ in String$, starting from Start% (if specified). Returns -1 if the character is not found. ?INSTR("HELLO","L") 2 ?INSTR(3,"HELLO,"L") 3 ?INSTR("HELLO","D") -1 SubString$ = SUBST$(String$, Start% {,Length%}, Replacement$) -returns String$ with the first Length% characters starting from Start% replaced by Replacement$. If Length% is not specified, it will replace everything after Start% ?SUBST$("A?D",1,1,"BC") ABCD
Author
12Me21
Updated
Rating
3 votes
Categories
Keywords
  • strings
  • string
  • text
  • letters
  • ascii
  • asc
  • chr
  • characters
  • val
  • value
  • str
  • hex
  • hexadecimal
  • format
  • len
  • length
  • mid
  • middle
  • left
  • right
  • instr
  • subst
8 Comment(s) 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself This page is great! There are a few slight improvements you can make though. For example, by removing all the text and deleting it spaceturtles spaceturtles Video Games I like to play video games! Hobbies Avatar Block I didn't change my avatar for 30 days. Website Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength It's still helpful though, could do with a redo Minxrod Minxrod Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Video Games I like to play video games! Hobbies Is this still a WIP or is it finished and the title is just Strings (WIP)? Minxrod Minxrod Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Video Games I like to play video games! Hobbies What does that picture have to do with strings...? 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself It's a long story... The image is just random picture I had on my computer. Minxrod Minxrod Third Year My account is over 3 years old Website Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Video Games I like to play video games! Hobbies K den. Yttria Yttria Head Admin code tags exist 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Tags take a while to type. I'll add some now.