ScriptForge.String service

The String service provides a collection of methods for string processing. These methods can be used to:

Definitions

Line breaks

The String service recognizes the following line breaks:

Symbolic name

ASCII number

Line feed
Vertical tab
Carriage return
Carriage return + Line feed
File separator
Group separator
Record separator
Next line
Line separator
Paragraph separator

10
12
13
13 + 10
28
29
30
133
8232
8233


空白として使用される文字

The String service recognizes the following whitespaces:

Symbolic name

ASCII number

Space
Horizontal tab
Line feed
Vertical tab
Form feed
Carriage return
Next line
No-break space
Line separator
Paragraph separator

32
9
10
11
12
13
133
160
8232
8233


Escape sequences

Below is a list of escape sequences that can be used in strings.

Escape Sequence

Symbolic name

ASCII number

\n
\r
\t

Line feed
Carriage return
Horizontal tab

10
13
9


tip

To have the escape sequence "\n" interpreted as an actual string, simply use "\\n" instead of "\" & Chr(10).


Non-printable characters:

Characters defined in the Unicode Character Database as “Other” or “Separator” are considered as non-printable characters.

Control characters (ASCII code <= 0x1F) are also considered as non-printable.

Quotes inside strings:

To add quotes in strings use \' (single quote) or \" (double quote). For example:

Service invocation

Before using the ScriptForge.String service the ScriptForge library needs to be loaded using:

In Basic

      GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
  

Loading the library will create the SF_String object that can be used to call the methods in the String service.

The following code snippets show the three ways to call methods from the String service (the Capitalize method is used as an example):


    Dim s as String : s = "abc def"
    s = SF_String.Capitalize(s) ' Abc Def
  

    Dim s as String : s = "abc def"
    Dim svc : svc = SF_String
    s = svc.Capitalize(s) ' Abc Def
  

    Dim s as String : s = "abc def"
    Dim svc : svc = CreateScriptService("String")
    s = svc.Capitalize(s) ' Abc Def
  
In Python

The code snippet below illustrates how to invoke methods from the String service in Python scripts. The IsIPv4 method is used as an example.


    from scriptforge import CreateScriptService
    svc = CreateScriptService("String")
    ip_address = '192.168.0.14'
    svc.IsIPv4(ip_address) # True
  

プロパティ

The SF_String object provides the following properties for Basic scripts:

名前

ReadOnly

概要

sfCR

はい

Carriage return: Chr(13)

sfCRLF

はい

Carriage return + Linefeed: Chr(13) & Chr(10)

sfLF

はい

Linefeed: Chr(10)

sfNEWLINE

はい

Carriage return + Linefeed, which can be
1) Chr(13) & Chr(10) or
2) Linefeed: Chr(10)
depending on the operating system.

sfTAB

はい

Horizontal tabulation: Chr(9)


tip

You can use the properties above to identify or insert the corresponding characters inside strings. For example, the Linefeed can be replaced by SF_String.sfLF.


List of Methods in the String Service

Capitalize
Count
EndsWith
Escape
ExpandTabs
FilterNotPrintable
FindRegex
HashStr
HtmlEncode
IsADate
IsAlpha
IsAlphaNum
IsAscii
IsDigit
IsEmail

IsFileName
IsHexDigit
IsIBAN
IsIPv4
IsLike
IsLower
IsPrintable
IsRegex
IsSheetName
IsTitle
IsUpper
IsUrl
IsWhitespace
JustifyCenter
JustifyLeft

JustifyRight
Quote
ReplaceChar
ReplaceRegex
ReplaceStr
Represent
Reverse
SplitLines
SplitNotQuoted
StartsWith
TrimExt
Unescape
Unquote
Wrap


note

The first argument of most methods is the string to be considered. It is always passed by reference and left unchanged. Methods such as Capitalize, Escape, etc return a new string after their execution.


warning

Because Python has comprehensive built-in string support, most of the methods in the String service are available for Basic scripts only. The methods available for Basic and Python are: HashStr, IsADate, IsEmail, IsFileName, IsIBAN, IsIPv4, IsLike, IsSheetName, IsUrl, SplitNotQuoted and Wrap.


Capitalize

Capitalizes the first character from each word in the input string.

Syntax:

svc.Capitalize(inputstr: str): str

Parameters:

inputstr: The string to be capitalized.

Example:


    Dim sName as String : sName = "john smith"
    Dim sCapitalizedName as String
    sCapitalizedName = SF_String.Capitalize(sName)
    MsgBox sCapitalizedName 'John Smith
  

Count

Counts the number of occurrences of a substring or a regular expression within a string.

Syntax:

svc.Count(inputstr: str, substring: str, [isregex: bool], [casesensitive: bool]): int

Parameters:

inputstr: The input string to be examined

substring: The substring or the regular expression to be used during search

isregex: Use True if the substring is a regular expression (Default = False)

casesensitive: The search can be case sensitive or not (Default = False).

Example:


    'Counts the occurrences of the substring "or" inside the input string (returns 2)
    MsgBox SF_String.Count("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "or", CaseSensitive := False)
    'Counts the number of words with only lowercase letters (returns 7)
    MsgBox SF_String.Count("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "\b[a-z]+\b", IsRegex := True, CaseSensitive := True)
  
tip

To learn more about regular expressions, refer to the Python's documentation on Regular Expression Operations.


EndsWith

Returns True if a string ends with a specified substring.

The function returns False when either the string or the substring have a length = 0 or when the substring is longer than the string.

Syntax:

svc.EndsWith(inputstr: str, substring: str, [casesensitive: bool]): bool

Parameters:

inputstr: The string to be tested.

substring: The substring to be searched at the end of inputstr.

casesensitive: The search can be case sensitive or not (Default = False).

Example:


    'Returns True because the method was called with the default CaseSensitive = False
    MsgBox SF_String.EndsWith("abcdefg", "EFG")
    'Returns False due to the CaseSensitive parameter
    MsgBox SF_String.EndsWith("abcdefg", "EFG", CaseSensitive := True)
  

Escape

Converts linebreaks and tabs contained in the input string to their equivalent escaped sequence (\\, \n, \r, \t).

Syntax:

svc.Escape(inputstr: str): str

Parameters:

inputstr: The string to be converted.

Example:


    'Returns the string "abc\n\tdef\\n"
    MsgBox SF_String.Escape("abc" & Chr(10) & Chr(9) & "def\n")
  

ExpandTabs

Replaces Tab characters Chr(9) by space characters to replicate the behavior of tab stops.

If a line break is found, a new line is started and the character counter is reset.

Syntax:

svc.ExpandTabs(inputstr: str, [tabsize: int]): str

Parameters:

inputstr: The string to be expanded

tabsize: This parameter is used to determine the Tab stops using the formula: TabSize + 1, 2 * TabSize + 1 , ... N * TabSize + 1 (Default = 8)

Example:


    Dim myText as String
    myText = "100" & SF_String.sfTAB & "200" & SF_String.sfTAB & "300" & SF_String.sfNEWLINE & _
             "X"  & SF_String.sfTAB & "Y" & SF_String.sfTAB & "Z"
    MsgBox SF_String.ExpandTabs(myText)
    '100     200     300
    'X       Y       Z
  

FilterNotPrintable

Replaces all non-printable characters in the input string by a given character.

Syntax:

svc.FilterNotPrintable(inputstr: str, [replacedby: str]): str

Parameters:

inputstr: The string to be searched

replacedby: Zero, one or more characters that will replace all non-printable characters in inputstr (Default = "")

Example:


    Dim LF : LF = Chr(10)
    Dim myText as String
    myText = "àén ΣlPµ" & LF & " Русский" & "\n"
    MsgBox SF_String.FilterNotPrintable(myText)
    ' "àén ΣlPµ Русский\n"
  

FindRegex

Finds in a string a substring matching a given regular expression.

Syntax:

svc.FindRegex(inputstr: str, regex: str, [start: int], [casesensitive: bool], [forward: bool]): str

Parameters:

inputstr: The string to be searched

regex: The regular expression

start: The position in the string where the search will begin. This parameter is passed by reference, so after execution the value of start will point to the first character of the found substring. If no matching substring is found, start will be set to 0.

casesensitive: The search can be case sensitive or not (Default = False).

forward: Determines the direction of the search. If True, search moves forward. If False search moves backwards (Default = True)

At the first iteration, if forward = True, then start should be equal to 1, whereas if forward = False then start should be equal to Len(inputstr)

Example:


    Dim lStart As Long : lStart = 1
    Dim result as String
    result = SF_String.FindRegex("abCcdefghHij", "C.*H", lStart, CaseSensitive := True)
    MsgBox lStart & ": " & result
    '3: CcdefghH
  
tip

In the example above, the new value of lStart can be used to keep searching the same input string by setting the Start parameter to lStart + Len(result) at the next iteration.


HashStr

Hash functions are used inside some cryptographic algorithms, in digital signatures, message authentication codes, manipulation detection, fingerprints, checksums (message integrity check), hash tables, password storage and much more.

The HashStr method returns the result of a hash function applied on a given string and using a specified algorithm, as a string of lowercase hexadecimal digits.

The hash algorithms supported are: MD5, SHA1, SHA224, SHA256, SHA384 and SHA512.

Syntax:

svc.HashStr(inputstr: str, algorithm: str): str

Parameters:

inputstr: The string to hash. It is presumed to be encoded in UTF-8. The hashing algorithm will consider the string as a stream of bytes.

algorithm: One of the supported algorithms listed above, passed as a string.

Example:

In Basic

    MsgBox SF_String.HashStr("œ∑¡™£¢∞§¶•ªº–≠œ∑´®†¥¨ˆøπ‘åß∂ƒ©˙∆˚¬", "MD5")
    ' c740ccc2e201df4b2e2b4aa086f35d8a
  
In Python

    svc = CreateScriptService("String")
    bas = CreateScriptService("Basic")
    a_string = "œ∑¡™£¢∞§¶•ªº–≠œ∑´®†¥¨ˆøπ‘åß∂ƒ©˙∆˚¬"
    hash_value = svc.HashStr(a_string, "MD5")
    bas.MsgBox(hash_value)
    # c740ccc2e201df4b2e2b4aa086f35d8a
  

HtmlEncode

Encodes the input string into the HTML character codes, replacing special characters by their & counterparts.

For example, the character é would be replaced by &eacute; or an equivalent numerical HTML code.

Syntax:

svc.HtmlEncode(inputstr: str): str

Parameters:

inputstr: The string to encode.

Example:


    MsgBox SF_String.HtmlEncode("<a href=""https://a.b.com"">From α to ω</a>")
    ' "&lt;a href=&quot;https://a.b.com&quot;&gt;From &#945; to &#969;&lt;/a&gt;"
  

IsADate

Returns True if the input string is a valid date according to a specified date format.

Syntax:

svc.IsADate(inputstr: str, [dateformat: str]): bool

Parameters:

inputstr: The string to be checked. If empty, the method returns False

dateformat: The date format, as a string. It can be either "YYYY-MM-DD" (default), "DD-MM-YYYY" or "MM-DD-YYYY"

Nd rowspan=