Core Function StrNew
From Sputnik Wiki
(Difference between revisions)
m (1 revision) |
(→Example) |
||
(4 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | StrNew( <char>, <length> | + | StrNew( <char>, <length> ) |
</pre> | </pre> | ||
=== Description === | === Description === | ||
− | Create a new string of a given length filled with a given char | + | Create a new string of a given length filled with a given char. |
=== Parameters === | === Parameters === | ||
Line 11: | Line 11: | ||
==== char ==== | ==== char ==== | ||
+ | What char should populate the new string. | ||
==== length ==== | ==== length ==== | ||
− | + | How big the new string should be. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
=== Return Value === | === Return Value === | ||
− | Success: | + | Success: The new string. |
Failure: null | Failure: null | ||
Line 31: | Line 25: | ||
=== Remarks === | === Remarks === | ||
− | + | None. | |
− | + | ||
− | + | ||
=== Example === | === Example === | ||
Normal string | Normal string | ||
+ | |||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
$Str = StrNew('T', 15); | $Str = StrNew('T', 15); | ||
$Str[0] = 'A'; // Set first char to A | $Str[0] = 'A'; // Set first char to A | ||
− | $Str[1] = ' | + | $Str[1] = 'B'; // Set second char to B |
echo $Str; // Prints: ABTTTTTTTTTTTTT | echo $Str; // Prints: ABTTTTTTTTTTTTT | ||
// Of course you could just make the string like | // Of course you could just make the string like | ||
Line 47: | Line 40: | ||
// OR | // OR | ||
// $Str = "AB" . ('T' x 13); | // $Str = "AB" . ('T' x 13); | ||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | You can create a string full of null terminators since Sputnik strings use a *length* property so a string can contain nulls etc (of course there is an actual null terminator at the end of the length) this means when you use StrNew() to create a string with say 100 length and all null terminators then the string will be 100 chars in size. | |
+ | |||
+ | However you might want to eventually cut the string into the size you want it to be once you finish editing it to do that we can use TrimToNull() which will trim the string to first NULL it finds. | ||
+ | |||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | + | $Str = StrNew(@'\0', 15); | |
− | + | $Str[0] = 'A'; // Set first char to A | |
− | $ | + | $Str[1] = 'B'; // Set second char to B |
− | + | say $Str; // Prints: AB | |
− | $ | + | say StrLen($Str); // 15 |
− | $ | + | |
− | + | // Lets fix the string so its no longer 15 in size | |
− | + | // but fits all way to null term so 2 in size | |
+ | say "Trimming..."; | ||
+ | $Str = TrimToNull($Str); | ||
+ | say $Str; // Prints: AB | ||
+ | say StrLen($Str); // 2 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 10:09, 19 September 2015
StrNew( <char>, <length> )
Contents |
Description
Create a new string of a given length filled with a given char.
Parameters
char
What char should populate the new string.
length
How big the new string should be.
Return Value
Success: The new string.
Failure: null
Remarks
None.
Example
Normal string
$Str = StrNew('T', 15); $Str[0] = 'A'; // Set first char to A $Str[1] = 'B'; // Set second char to B echo $Str; // Prints: ABTTTTTTTTTTTTT // Of course you could just make the string like // $Str = "ABTTTTTTTTTTTTT"; // OR // $Str = "AB" . ('T' x 13);
You can create a string full of null terminators since Sputnik strings use a *length* property so a string can contain nulls etc (of course there is an actual null terminator at the end of the length) this means when you use StrNew() to create a string with say 100 length and all null terminators then the string will be 100 chars in size.
However you might want to eventually cut the string into the size you want it to be once you finish editing it to do that we can use TrimToNull() which will trim the string to first NULL it finds.
$Str = StrNew(@'\0', 15); $Str[0] = 'A'; // Set first char to A $Str[1] = 'B'; // Set second char to B say $Str; // Prints: AB say StrLen($Str); // 15 // Lets fix the string so its no longer 15 in size // but fits all way to null term so 2 in size say "Trimming..."; $Str = TrimToNull($Str); say $Str; // Prints: AB say StrLen($Str); // 2