The text in its current form is incomplete. |
Convert all arguments to strings and concatenate.
Usage examples:
number=2;echo ("This is ",number,3," and that's it.");echo (str("This is ",number,3," and that's it."));Results:
ECHO: "This is ", 2, 3, " and that's it."ECHO: "This is 23 and that's it."
This can be used for simple conversion of numbers to strings
s = str(n);
[Note:Requires version2015.03]
Convert numbers to a string containing character with the corresponding code. OpenSCAD uses Unicode, so the number is interpreted as Unicode code point. Numbers outside the valid code point range produce an empty string.
Parameters
Examples
echo(chr(65),chr(97));// ECHO: "A", "a"echo(chr(65,97));// ECHO: "Aa"echo(chr([66,98]));// ECHO: "Bb"echo(chr([97:2:102]));// ECHO: "ace"echo(chr(-3));// ECHO: ""echo(chr(9786),chr(9788));// ECHO: "☺", "☼"echo(len(chr(9788)));// ECHO: 1
Note: When used with echo() the output to the console for character codes greater than 127 is platform dependent.
[Note:Requires version2019.05]
Convert a character to a number representing theUnicodecode point. If the parameter is not a string, theord() returnsundef.
Parameters
Examples
echo(ord("a"));// ECHO: 97echo(ord("BCD"));// ECHO: 66echo([for(c="Hello! 🙂")ord(c)]);// ECHO: [72, 101, 108, 108, 111, 33, 32, 128578]txt="1";echo(ord(txt)-48,txt);// ECHO: 1,"1" // only converts 1 character
returns the number of characters in a text.
echo(len("Hello world"));// 11
search() for text searching.
The function is_string(value) return true if the value is a string, false else
echo(is_string("alpha"));//trueecho(is_string(22));//false
To complement native functions, you can define your own functions, some suggestions:
//-- Lower case all chars of a string -- does not work with accented charactersfunctionstrtolower(string)=chr([for(s=string)let(c=ord(s))c<91&&c>64?c+32:c]);//-- Replace char(not string) in a stringfunctionchar_replace(s,old=" ",new="_")=chr([for(i=[0:len(s)-1])s[i]==old?ord(new):ord(s[i])]);//-- Replace last chars of a string (can be used for file extension replacement of same length)functionstr_rep_last(s,new=".txt")=str(chr([for(i=[0:len(s)-len(new)-1])ord(s[i])]),new);//-- integer value from string ----------//Parameters ret and i are for function internal use (recursion)functionstrtoint(s,ret=0,i=0)=i>=len(s)?ret:strtoint(s,ret*10+ord(s[i])-ord("0"),i+1);
Note here the use of chr() to recompose a string from unknown number of caracters defined by their ascii code. This avoid using recursive modules as was required before list management came in.