XScript Manual · Chapter 30

STR — string functions

STR collects parsing, conversion, and search helpers used constantly for protocol framing, order-number splitting, and recipe parsing.

Basic example

string data = "    abc d   ";
Log("[{0}] => Trim = [{1}]", data, STR.Trim(data));
 
data = "12";
int dataToInt = STR.ParseToInt(data);
 
data = "12.3";
dataToInt = STR.ParseToInt(data, 0);
 
data = "abc:123";
string name;
string value;
STR.ParseNameValue(data, name, value, ":");
 
int pos = STR.IndexOf(data, ":");
string tail = STR.Substring(data, pos + 1);

Real-world — recipe parsing

// recipe = "1:50,3:20.3,7:60.1"
array items = STR.ParseCommaString(recipe);
 
if (items.Count == 0)
{
    return -1;
}
 
int unitNo;
double target;
 
for (i, 0, items.Count - 1)
{
    if (STR.ParsePairIntDouble(items[i], unitNo, target, ":") == false)
    {
        return -1;
    }
    if (unitNo == targetUnit)
    {
        return target;
    }
}

Key methods

Basics

SignatureDescription
int Length(string text)Length
string Trim(string text)Trim whitespace
string ToUpper(string text) / string ToLower(string text)Case
string Replace(string text, string oldValue, string newValue)Replace
bool IsNullOrWhiteSpace(string text)Empty / whitespace
bool Contains(string text, string value)Contains
bool StartsWith(string text, string value) / EndsWithPrefix / suffix

Search / extract

SignatureDescription
int IndexOf(string text, string value)First index
int IndexOf(string text, string value, int startIndex)From index
int NthIndexOf(string text, string value, int n)Nth occurrence
string Substring(string text, int startIndex)To end
string Substring(string text, int startIndex, int length)Fixed length

Parsing

SignatureDescription
array ParseCommaString(string commaString)CSV split
array ParseSplitString(string text, string splitString)Custom delimiter
bool ParseNameValue(string text, ref string name, ref string value, string delim)name/value pair
bool ParsePairIntDouble(string text, ref int first, ref double second, string delim)key:value int + double
bool ParsePairStrings(string text, ref string first, ref string second)String pair
int ParseToInt(string text) / with defaultTo int
double ParseToDouble(string text) / with defaultTo double

File

SignatureDescription
array ReadAllLines(string path, bool addLog = true)File → lines

Tips

  • For external protocols, two-stage parsing (ParseCommaString then ParsePairIntDouble / ParsePairStrings) is robust.
  • ParseToInt with a default argument is the safe form — no exception on bad input.
  • $"..." interpolation is usually clearer than FormatString; use SYS.GetDateTimeStringFormat for complex locale/date formatting.

STR full function reference (XUtilString)

Sourced from C# — every function callable from script.

Parsing (Name=Value · Pair)

FunctionMeaning
STR.GetName(text, delim='=')name part of name=value
STR.GetValue(text, delim='=')value part of name=value
STR.ParseNameValue(text, ref name, ref value, delim="=")both at once
STR.ParsePairStrings(text, ref s1, ref s2, delim="=")two strings
STR.ParsePairIntegers(text, ref i1, ref i2, delim="=")two ints
STR.ParsePairDoubles(text, ref d1, ref d2, delim="=")two doubles
STR.ParsePairIntDouble(text, ref i, ref d, delim="=")int + double
STR.ParsePairStringInteger(text, ref s, ref i, delim="=")string + int
STR.ParsePairStringDouble(text, ref s, ref d, delim="=")string + double

Split / join

FunctionMeaning
STR.ParseCommaString(text)split by ,array
STR.ParseTabSplitString(text)split by tab
STR.ParseSplitString(text, char) / (text, string)split by separator
STR.GetCommaText(list)array → , joined
STR.GetTapSplitText(list)array → tab joined
STR.GetSplitedText(list, char) / (list, string)array → separator joined
STR.StringToDoubleList(list)string array → double array

Inspection / conversion

FunctionMeaning
STR.HaveSpace(text)has whitespace
STR.HaveKoreanChar(str)has Hangul
STR.IsStringNubmer(text)digits only
STR.IsNullOrWhiteSpace(text)empty or whitespace only
STR.GetEnumType(type, value)string → enum
STR.ConvertAvailableName(name)sanitize as filename / identifier
STR.StringToHexString(str)string → hex representation

Manipulation (returns a new string)

FunctionMeaning
STR.Strip(text) / STR.StripBrace(text)trim spaces / trim outer braces
STR.Trim / TrimStart / TrimEnd(text)standard C# equivalents
STR.ToLower(text) / STR.ToUpper(text)case conversion
STR.PadLeft(text, totalWidth) / STR.PadRight(text, totalWidth)pad
STR.Substring(text, start, len) / (text, start)substring
STR.Insert(text, startIndex, value)insert
STR.Remove(text, startIndex) / (text, startIndex, count)partial remove
STR.Replace(text, oldValue, newValue)replace all
STR.ReplaceString(text, start, length, toReplace)position-based replace
STR.GetReverse(data, step)reverse in step-byte chunks

Search / index

FunctionMeaning
STR.Length(text)length
STR.Contains(text, value)contains
STR.StartsWith(text, value) / EndsWith(text, value)prefix / suffix
STR.IndexOf(text, value) / (text, value, start) / (text, value, start, count)first index
STR.NthIndexOf(text, value, n)n-th index
STR.LastIndexOf(text, value) / (text, value, start) / (text, value, start, count)last index

Char codes

FunctionMeaning
STR.GetCharCode(str, index=0)char code at index
STR.SetCharCode(str, index, code)replace char at index

Encoding / escape

FunctionMeaning
STR.EncodeMultiLine(data) / STR.DecodeMultiLine(data)multi-line ↔ single line
STR.EncodeBraceInFormat(format)escape { } for FormatString
STR.Escape(input) / STR.Unescape(input)C-style escape ↔ raw

Number ↔ string

FunctionMeaning
STR.ParseToInt(text) / (text, defaultValue)string → int
STR.ParseToDouble(text) / (text, defaultValue)string → double
STR.ParseToStr(int) / (double) / (bool)number/bool → string
STR.ParseStringNumber(text, ref prefix, ref number, base=10)"M0017" → prefix="M", number=17
STR.FormatNumberWithComma(int) / (double)1,234,567 thousands
STR.ParseCommaNumber(text, int) / (text, double)comma-included string → number

Text file helpers

FunctionMeaning
STR.ReadAllLines(path, addLog=true)file → array of lines
STR.ReadAllIntLines(path, addLog=true)file → array of ints
STR.ReadFirstLine(path)first line only
STR.ReadAllText(path, addLog=false)whole file → one string
STR.SaveAllLines(path, lines)array → file