Core Function StrNew

From Sputnik Wiki
Jump to: navigation, search
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
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox