2 summary::array of characters
3 categories:: Collections>Ordered
6 String represents an array of characters.
8 Strings can be written literally using double quotes:
10 "my string".class.postln;
18 Read the entire contents of a link::Classes/File:: and return them as a new String.
22 private::prUnixCmd, prFormat, prCat
25 Strings respond to .at in a manner similar to other indexed collections. Each element is a link::Classes/Char::.
27 "ABCDEFG".at(2).postln;
31 Returns a -1, 0, or 1 depending on whether the receiver should be sorted before the argument, is equal to the argument or should be sorted after the argument. This is a case sensitive compare.
34 Returns a link::Classes/Boolean:: whether the receiver should be sorted before the argument.
37 Returns a link::Classes/Boolean:: whether the two Strings are equal.
40 Prints the string to the current post window.
43 Prints the string and a carriage return to the current post window.
45 method::postc, postcln
46 As link::#-post:: and link::#-postln::, but formatted as a comment.
48 "This is a comment.".postcln;
52 Prints a formatted string with arguments to the current post window. The % character in the format string is replaced by a string representation of an argument. To print a % character use \\% .
54 postf("this % a %. pi = %, list = %\n", "is", "test", pi.round(1e-4), (1..4))
56 this is a test. pi = 3.1416, list = [ 1, 2, 3, 4 ]
60 Returns a formatted string with arguments. The % character in the format string is replaced by a string representation of an argument. To print a % character use \\% .
62 format("this % a %. pi = %, list = %\n", "is", "test", pi.round(1e-4), (1..4))
64 this is a test. pi = 3.1416, list = [ 1, 2, 3, 4 ]
68 POSIX regular expression matching. Returns true if the receiver (a regular expression pattern) matches the string passed to it. The strong::start:: is an offset where to start searching in the string (default: 0), strong::end:: where to stop.
70 "c".matchRegexp("abcdefg", 2, 5); // true
71 "c".matchRegexp("abcdefg", 4, 5); // false
73 "behaviou?r".matchRegexp("behavior"); // true
74 "behaviou?r".matchRegexp("behaviour"); // true
75 "behaviou?r".matchRegexp("behavir"); // false
76 "b.h.v.r".matchRegexp("behavor"); // true
77 "b.h.vi*r".matchRegexp("behaviiiiir"); // true
78 "(a|u)nd".matchRegexp("und"); // true
79 "(a|u)nd".matchRegexp("and"); // true
80 "[a-c]nd".matchRegexp("ind"); // false
81 "[a-c]nd".matchRegexp("bnd"); // true
85 POSIX regular expression search.
87 "foobar".findRegexp("o*bar");
88 "32424 334 /**aaaaaa*/".findRegexp("/\\*\\*a*\\*/");
89 "foobar".findRegexp("(o*)(bar)");
90 "aaaabaaa".findAllRegexp("a+");
94 Prepends an error banner and posts the string.
98 Prepends a warning banner and posts the string.
104 Return a concatenation of the two strings.
108 Return a concatenation of the two strings with a space between them.
111 Compiles a String containing legal SuperCollider code and returns a Function.
115 f = "2 + 1".compile.postln;
120 method::asCompileString
121 Returns a String formatted for compiling.
127 f.asCompileString.postln;
132 As link::#-postln::, but posts the compileString of the reciever.
134 List[1, 2, ["comment", [3, 2]], { 1.0.rand }].postcs;
138 Compile and execute a String containing legal SuperCollider code, returning the result.
140 "2 + 1".interpret.postln;
143 method::interpretPrint
144 Compile, execute and print the result of a String containing legal SuperCollider code.
146 "2 + 1".interpretPrint;
150 Return a link::Classes/Symbol:: derived from the String.
154 z = "myString".asSymbol.postln;
160 Return an link::Classes/Integer:: derived from the String. Strings beginning with non-numeric characters return 0.
162 "4".asInteger.postln;
166 Return a link::Classes/Float:: derived from the String. Strings beginning with non-numeric characters return 0.
168 "4.3".asFloat.postln;
172 Return a link::Classes/Float:: based on converting a time string in format (dd):hh:mm:ss.s. This is the inverse method to link::Classes/SimpleNumber#-asTimeString::.
174 (45296.asTimeString).asSecs;
176 "62.1".asSecs; // warns
179 "-1".asSecs; // neg sign supported
181 "12:-34:56".asSecs; // warns
182 "-23:12.3456".asSecs; //
183 "-1:00:00:00".asSecs; // days too.
187 Concatenate this string with the following args.
189 "These are some args: ".catArgs(\fish, SinOsc.ar, {4 + 3}).postln;
193 Same as link::#-catArgs::, but with spaces in between.
195 "These are some args: ".scatArgs(\fish, SinOsc.ar, {4 + 3}).postln;
199 Same as link::#-catArgs::, but with commas in between.
201 "a String".ccatArgs(\fish, SinOsc.ar, {4 + 3}).postln;
204 method::catList, scatList, ccatList
205 As link::#-catArgs::, link::#-scatArgs:: and link::#-ccatArgs:: above, but takes a Collection (usually a link::Classes/List:: or an link::Classes/Array::) as an argument.
207 "a String".ccatList([\fish, SinOsc.ar, {4 + 3}]).postln;
211 Returns an Array of Strings split at the separator. The separator is a link::Classes/Char::, and is not included in the output array. The default separator is $/, handy for Unix paths.
213 "This/could/be/a/Unix/path".split.postln;
214 "These are several words".split($ ).postln;
218 Returns an Array of asci numbers of the Strings's characters.
224 Returns the index of the string in the receiver, or nil if not found. If strong::ignoreCase:: is true, find makes no difference between uppercase and lowercase letters. The strong::offset:: is the point in the string where the search begins.
226 "These are several words".find("are").postln;
227 "These are several words".find("fish").postln;
230 method::findBackwards
231 Same like link::#-find::, but starts at the end of the string.
234 "These words are several words".find("words"); // 6
235 "These words are several words".findBackwards("words"); // 24
239 Returns the indices of the string in the receiver, or nil if not found.
241 "These are several words which are fish".findAll("are").postln;
242 "These are several words which are fish".findAll("fish").postln;
246 Returns a link::Classes/Boolean:: indicating if the String contains string.
248 "These are several words".contains("are").postln;
249 "These are several words".contains("fish").postln;
253 Same as link::#-contains::, but case insensitive.
255 "These are several words".containsi("ArE").postln;
258 method::containsStringAt
259 Returns a link::Classes/Boolean:: indicating if the String contains string beginning at the specified index.
261 "These are several words".containsStringAt(6, "are").postln;
264 method::icontainsStringAt
265 Same as link::#-containsStringAt::, but case insensitive.
268 Add the escape character (\) at the location of your choice.
270 "This will become a Unix friendly string".escapeChar($ ).postln;
274 Transliteration. Replace all instances of strong::from:: with strong::to::.
276 ":-(:-(:-(".tr($(, $)); //turn the frowns upside down
280 Like link::#-tr::, but with strings as arguments.
282 "Here are several words which are fish".replace("are", "were");
286 Print the String on stream.
288 "Print this on Post".printOn(Post);
291 Post << "Print this on Post";
295 Same as link::#-printOn::, but formatted link::#-asCompileString::.
297 "Store this on Post".storeOn(Post);
300 Post <<< "Store this on Post";
303 method::inspectorClass
304 Returns class link::Classes/StringInspector::.
307 Returns a new String with all RTF formatting removed.
310 // same as File-readAllStringRTF
311 g = File("/code/SuperCollider3/build/Help/UGens/Chaos/HenonC.help.rtf","r");
312 g.readAllString.stripRTF.postln;
317 subsection::Unix Support
319 Where relevant, the current working directory is the same as the location of the SuperCollider app and the shell is the Bourne shell (sh). Note that the cwd, and indeed the shell itself, does not persist:
321 "echo $0".unixCmd; // print the shell (sh)
326 "export FISH=mackerel".unixCmd;
327 "echo $FISH".unixCmd;
329 It is however possible to execute complex commands:
331 "pwd; cd Help/; pwd".unixCmd;
332 "export FISH=mackerel; echo $FISH".unixCmd;
334 Also on os x applescript can be called via osascript:
336 "osascript -e 'tell application \"Safari\" to activate'".unixCmd;
338 Should you need an environment variable to persist you can use link::#-setenv::.
341 Despite the fact that the method is called 'unixCmd', strong::it does work in Windows::. The string must be a DOS command, however: "dir" rather than "ls" for instance.
345 Execute the String on the command line using the Bourne shell (sh). Returns the pid of the newly created process (use Integer.pidRunning to test if a pid is alive). action is a Function that is called when the process has exited. It is passed two arguments: the exit code and pid of the exited process. postOutput is a Boolean that controls whether or not the output of the process is displayed in the post window.
348 "echo one; sleep 1; echo two; sleep 1".unixCmd { |res, pid| [\done, res, pid].postln };
351 method::unixCmdGetStdOut
352 Similar to link::#-unixCmd:: except that the stdout of the process is returned (synchronously) rather than sent to the post window.
354 ~listing = "ls Help".unixCmdGetStdOut; // Grab
355 ~listing.reverse.as(Array).stutter.join.postln; // Mangle
359 Executes a unix command synchronously.
361 returns:: Error code of the unix command
363 method::runInTerminal
364 Execute the String in a new terminal window. (The string is incorporated into a temporary script file and executed using a shell. "/usr/bash" is the default shell used but you can optionally specify which shell to use as an argument.)
366 "echo ---------Hello delightful SuperCollider user----------".runInTerminal;
370 Set the environment variable indicated in the string to equal the String strong::value::. This value will persist until it is changed or SC is quit. Note that if strong::value:: is a path you may need to call link::#-standardizePath:: on it.
372 // all defs in this directory will be loaded when a local server boots
373 "SC_SYNTHDEF_PATH".setenv("~/scwork/".standardizePath);
374 "echo $SC_SYNTHDEF_PATH".unixCmd;
378 Returns the value contained in the environment variable indicated by the String.
384 Returns an link::Classes/Array:: containing all paths matching this String. Wildcards apply, non-recursive.
386 Post << "Help/*".pathMatch;
390 Perform link::#-pathMatch:: on this String, then load and execute all paths in the resultant link::Classes/Array::.
392 //first prepare a file with some code...
394 var f = File("/tmp/loadPaths_example.scd", "w");
396 f.putString("\"This text is the result of a postln command which was loaded and executed by loadPaths\".postln");
398 "test file could not be created - please edit the file's path above".warn;
403 //then load the file...
404 "/tmp/loadPaths_example.scd".loadPaths; //This file posts some text
408 Load and execute the file at the path represented by the receiver.
412 Load and execute the file at the path represented by the receiver, interpreting the path as relative to the current document or text file. Requires that the file has been saved.
414 method::resolveRelative
415 Convert the receiver from a relative path to an absolute path, relative to the current document or text file. Requires that the current text file has been saved. Absolute paths are left untransformed.
417 method::standardizePath
418 Expand ~ to your home directory, and resolve aliases on OSX. See link::Classes/PathName:: for more complex needs.
420 "~/".standardizePath; //This will print your home directory
424 Follow symbolic links (and aliases on OSX) and any parent directory references (like "..") and return the true absolute path.
425 returns:: An absolute path or code::nil:: if path did not exist.
428 Return the filename from a Unix path.
430 "Imaginary/Directory/fish.rtf".basename;
434 Return the directory name from a Unix path.
436 "Imaginary/Directory/fish.rtf".dirname;
440 Split off the extension from a filename or path and return both in an link::Classes/Array:: as [path or filename, extension].
443 "Imaginary/Directory/fish.rtf".splitext;
447 Open file, directory or URL via the operating system. On OSX this is implemented via teletype::open::, on Linux via
448 teletype::xdg-open:: and on Windows via teletype::start::.
450 Platform.userConfigDir.openOS;
451 "http://supercollider.sf.net".openOS;
454 subsection::Document Support
456 method::newTextWindow
457 Create a new link::Classes/Document:: with this.
459 "Here is a new Document".newTextWindow;
463 Create a new link::Classes/Document:: from the path corresponding to this. Returns the Document.
466 d = "Help/Help.html".openDocument;
472 Create a new link::Classes/Document:: from the path corresponding to this. The selection arguments will preselect the indicated range in the new window. Returns this.
475 d = "Help/Help.html".openTextFile(20, 210);
481 Returns the path for the helpfile named this, if it exists, else returns nil.
483 "Document".findHelpFile;
484 "foobar".findHelpFile;
488 Performs link::#-findHelpFile:: on this, and opens the file it if it exists, otherwise opens the main helpfile.
490 "Document".openHelpFile;
491 "foobar".openHelpFile;
495 Sends string to the speech synthesisier of the OS. (OS X only.) see: link::Classes/Speech::
497 "hi i'm talking with the default voice now, i guess".speak;
500 subsection::Drawing Support
502 The following methods must be called within an Window-drawHook or a SCUserView-drawFunc function, and will only be visible once the window or the view is refreshed. Each call to Window-refresh SCUserView-refresh will 'overwrite' all previous drawing by executing the currently defined function.
504 See also: link::Classes/Window::, link::Classes/UserView::, link::Classes/Color::, and link::Classes/Pen::.
507 for cross-platform GUIs, use code::Pen.stringAtPoint, Pen.stringInRect, Pen.stringCenteredIn, Pen.stringLeftJustIn, Pen.stringRightJustIn:: instead.
511 Draws the String at the current 0@0 link::Classes/Point::. If not transformations of the graphics state have taken place this will be the upper left corner of the window. See also link::Classes/Pen::.
514 w = Window.new.front;
515 w.view.background_(Color.white);
517 "abababababa\n\n\n".scramble.draw
524 Draws the String at the given link::Classes/Point:: using the link::Classes/Font:: and link::Classes/Color:: specified.
527 w = Window.new.front;
528 w.view.background_(Color.white);
530 "abababababa\n\n\n".scramble.drawAtPoint(
533 Color.blue(0.3, 0.5))
540 Draws the String into the given link::Classes/Rect:: using the link::Classes/Font:: and link::Classes/Color:: specified.
543 w = Window.new.front;
544 r = Rect(100, 100, 100, 100);
545 w.view.background_(Color.white);
547 "abababababa\n\n\n".scramble.drawInRect(r, Font("Courier", 12), Color.blue(0.3, 0.5));
554 method::drawCenteredIn
555 Draws the String into the given Rect using the Font and Color specified.
558 w = Window.new.front;
559 w.view.background_(Color.white);
560 r = Rect(100, 100, 100, 100);
562 "abababababa\n\n\n".scramble.drawCenteredIn(
573 method::drawLeftJustIn
574 Draws the String into the given Rect using the Font and Color specified.
577 w = Window.new.front;
578 w.view.background_(Color.white);
579 r = Rect(100, 100, 100, 100);
581 "abababababa\n\n\n".scramble.drawLeftJustIn(
592 method::drawRightJustIn
593 Draws the String into the given link::Classes/Rect:: using the link::Classes/Font:: and link::Classes/Color:: specified.
596 w = Window.new.front;
597 w.view.background_(Color.white);
598 r = Rect(100, 100, 100, 100);
600 "abababababa\n\n\n".scramble.drawRightJustIn(