Υπηρεσία ScriptForge.String

Η υπηρεσία String παρέχει μια συλλογή μεθόδων για την επεξεργασία συμβολοσειρών. Αυτές οι μέθοδοι μπορούν να χρησιμοποιηθούν για:

Ορισμοί

Αλλαγές γραμμής

Η υπηρεσία String αναγνωρίζει τις ακόλουθες αλλαγές γραμμής:

Συμβολικό όνομα

Αριθμός ASCII

Αλλαγή γραμμής
Κατακόρυφη καρτέλα
Αλλαγή παραγράφου
Αλλαγή γραμμής + Αλλαγή παραγράφου
Διαχωριστικό αρχείων
Διαχωριστικό ομάδων
Διαχωριστικό εγγραφών
Επόμενη γραμμή
Διαχωριστικό γραμμών
Διαχωριστικό παραγράφου

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


Κενά

Η υπηρεσία String αναγνωρίζει τα ακόλουθα κενά:

Συμβολικό όνομα

Αριθμός ASCII

Κενό
Οριζόντια καρτέλα
Αλλαγή γραμμής
Κατακόρυφη καρτέλα
Αλλαγή σελίδας
Αλλαγή παραγράφου
Επόμενη γραμμή
Διάστημα χωρίς κενό
Διαχωριστικό γραμμών
Διαχωριστικό παραγράφων

32
9
10
11
12
13
133
160
8232
8233


Ακολουθίες διαφυγής

Παρακάτω είναι ένας κατάλογος με ακολουθίες διαφυγής που μπορούν να χρησιμοποιηθούν σε συμβολοσειρές.

Ακολουθία διαφυγής

Συμβολικό όνομα

Αριθμός ASCII

\n
\r
\t

Αλλαγή γραμμής
Αλλαγή παραγράφου
Οριζόντια καρτέλα

10
13
9


tip

Για να ερμηνευτεί η ακολουθία διαφυγής "\n" ως πραγματική συμβολοσειρά, χρησιμοποιήστε απλώς "\\n" αντί για "\" & Chr(10).


Μη εκτυπώσιμοι χαρακτήρες:

Οι χαρακτήρες που ορίζονται στη βάση δεδομένων χαρακτήρων Unicode ως "Άλλο" ή "Διαχωριστικό" θεωρούνται ως μη εκτυπώσιμοι χαρακτήρες.

Οι χαρακτήρες ελέγχου (κωδικός ascii <= 0x1F) θεωρούνται επίσης ως μη εκτυπώσιμοι.

Εισαγωγικά μέσα σε συμβολοσειρές:

Για να προσθέσετε εισαγωγικά σε συμβολοσειρές χρησιμοποιήστε \' (μονό εισαγωγικό) ή \" (διπλό εισαγωγικό). Για παράδειγμα:

Κλήση υπηρεσίας

Πριν χρησιμοποιήσετε την υπηρεσία ScriptForge.String, η βιβλιοθήκη ScriptForge πρέπει να φορτωθεί χρησιμοποιώντας:

Σε Basic

      GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
  

Η φόρτωση της βιβλιοθήκης θα δημιουργήσει το αντικείμενο SF_String που μπορεί να χρησιμοποιηθεί για την κλήση των μεθόδων στην υπηρεσία String.

Τα παρακάτω αποσπάσματα κώδικα δείχνουν τους τρεις τρόπους κλήσης μεθόδων από την υπηρεσία String (η μέθοδος Capitalize χρησιμοποιείται ως παράδειγμα):


    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
  
Σε Python

Το παρακάτω απόσπασμα κώδικα δείχνει πώς να καλέσετε μεθόδους από την υπηρεσία String σε σενάρια Python. Ως παράδειγμα χρησιμοποιείται η μέθοδος IsIPv4.


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

Ιδιότητες

Το αντικείμενο SF_String παρέχει τις ακόλουθες ιδιότητες για τα σενάρια Basic:

Όνομα

Μόνο για ανάγνωση

Περιγραφή

sfCR

Ναι

Αλλαγή παραγράφου: Chr(13)

sfCRLF

Ναι

Αλλαγή παραγράφου + αλλαγή γραμμής: Chr(13) & Chr(10)

sfLF

Ναι

Αλλαγή γραμμής: Chr(10)

sfNEWLINE

Ναι

Αλλαγή παραγράφου + αλλαγή γραμμής, που μπορεί να είναι
1) Chr(13) & Chr(10) ή
2) Αλλαγή γραμμής: Chr(10)
ανάλογα με το λειτουργικό σύστημα.

sfTAB

Ναι

Οριζόντια στηλοθέτηση: Chr(9)


tip

Μπορείτε να χρησιμοποιήσετε τις παραπάνω ιδιότητες για να αναγνωρίσετε ή να εισαγάγετε τους αντίστοιχους χαρακτήρες μέσα σε συμβολοσειρές. Για παράδειγμα, η αλλαγή γραμμής μπορεί να αντικατασταθεί από 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

Το πρώτο όρισμα των περισσότερων μεθόδων είναι η συμβολοσειρά που πρέπει να ληφθεί υπόψη. Μεταβιβάζεται πάντα με αναφορά και μένει αμετάβλητο. Μέθοδοι όπως Capitalize (Κεφαλαιοποίηση), Escape κ.λπ. επιστρέφουν μια νέα συμβολοσειρά μετά την εκτέλεσή τους.


warning

Επειδή η Python διαθέτει ολοκληρωμένη ενσωματωμένη υποστήριξη συμβολοσειρών, οι περισσότερες από τις μεθόδους στην υπηρεσία String είναι διαθέσιμες μόνο για σενάρια Basic. Οι διαθέσιμες μέθοδοι για Basic και Python είναι: HashStr, IsADate, IsEmail, IsFileName, IsIBAN, IsIPv4, IsLike, IsSheetName, IsUrl, SplitNotQuoted και Wrap.


Capitalize

Κεφαλοποιεί τον πρώτο χαρακτήρα από κάθε λέξη στη συμβολοσειρά εισόδου.

Σύνταξη:

svc.Capitalize(inputstr: str): str

Παράμετροι:

inputstr: Η συμβολοσειρά που θα κεφαλαιοποιηθεί.

Παράδειγμα:


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

Count

Μετρά τον αριθμό των εμφανίσεων μιας υποσυμβολοσειράς ή μιας κανονικής έκφρασης μέσα σε μια συμβολοσειρά.

Σύνταξη:

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

Παράμετροι:

inputstr: Η συμβολοσειρά εισόδου που θα εξεταστεί

substring: Η υποσυμβολοσειρά ή η κανονική έκφραση που θα χρησιμοποιηθεί κατά την αναζήτηση

isregex: Χρησιμοποιήστε το True, εάν η υποσυμβολοσειρά είναι κανονική έκφραση (Προεπιλογή = False)

casesensitive (διάκριση πεζών-κεφαλαίων): Η αναζήτηση μπορεί να κάνει διάκριση πεζών-κεφαλαίων ή όχι (Προεπιλογή = False).

Παράδειγμα:


    'Μετράει τις εμφανίσεις της υποσυμβολοσειράς "ή" μέσα στη συμβολοσειρά εισόδου (επιστρέφει 2)
    MsgBox SF_String.Count("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "or", CaseSensitive := False)
    'Μετράει τον αριθμό των λέξεων μόνο με πεζά γράμματα (επιστρέφει 7)
    MsgBox SF_String.Count("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "\b[a-z]+\b", IsRegex := True, CaseSensitive := True)
  
tip

Για να μάθετε περισσότερα σχετικά με τις κανονικές εκφράσεις, ανατρέξτε στην τεκμηρίωση της Python στις Λειτουργίες κανονικών εκφράσεων .


EndsWith

Επιστρέφει True, εάν μια συμβολοσειρά τελειώνει με μια καθορισμένη υποσυμβολοσειρά.

Η συνάρτηση επιστρέφει False, όταν είτε η συμβολοσειρά, είτε η υποσυμβολοσειρά έχουν μήκος = 0, ή όταν η υποσυμβολοσειρά είναι μεγαλύτερη από τη συμβολοσειρά.

Σύνταξη:

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

Παράμετροι:

inputstr: Η συμβολοσειρά που θα δοκιμαστεί.

substring: Η υποσυμβολοσειρά προς αναζήτηση στο τέλος του inputstr.

casesensitive (διάκριση πεζών-κεφαλαίων): Η αναζήτηση μπορεί να κάνει διάκριση πεζών-κεφαλαίων ή όχι (Προεπιλογή = False).

Παράδειγμα:


    'Επιστρέφει True, επειδή η μέθοδος κλήθηκε με το προεπιλεγμένο CaseSensitive = False
    MsgBox SF_String.EndsWith("abcdefg", "EFG")
    'Επιστρέφει False λόγω της παραμέτρου CaseSensitive
    MsgBox SF_String.EndsWith("abcdefg", "EFG", CaseSensitive := True)
  

Escape

Μετατρέπει τις αλλαγές γραμμής και τις καρτέλες που περιέχονται στη συμβολοσειρά εισόδου στην ισοδύναμη ακολουθία διαφυγής (\\, \n, \r, \t).

Σύνταξη:

svc.Escape(inputstr: str): str

Παράμετροι:

inputstr: Η συμβολοσειρά που θα μετατραπεί.

Παράδειγμα:


    'Επιστρέφει τη συμβολοσειρά "abc\n\tdef\\n"
    MsgBox SF_String.Escape("abc" & Chr(10) & Chr(9) & "def\n")
  

ExpandTabs

Αντικαθιστά τους χαρακτήρες στηλοθέτη Chr(9) με χαρακτήρες διαστήματος για να αναπαράγει τη συμπεριφορά των ορίων στηλοθέτη.

Εάν βρεθεί μια αλλαγή γραμμής, ξεκινά μια νέα γραμμή και επαναφέρεται ο μετρητής χαρακτήρων.

Σύνταξη:

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

Παράμετροι:

inputstr: Η συμβολοσειρά που πρόκειται να επεκταθεί

tabsize: Αυτή η παράμετρος χρησιμοποιείται για τον προσδιορισμό των ορίων στηλοθετών χρησιμοποιώντας τον τύπο: TabSize + 1, 2 * TabSize + 1 , ... N * TabSize + 1 (Προεπιλογή = 8)

Παράδειγμα:


    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

Αντικαθιστά όλους τους μη εκτυπώσιμους χαρακτήρες στη συμβολοσειρά εισόδου με έναν δεδομένο χαρακτήρα.

Σύνταξη:

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

Παράμετροι:

inputstr: Η συμβολοσειρά προς αναζήτηση

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

Παράδειγμα:


    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.

Σύνταξη:

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

Παράμετροι:

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)

Παράδειγμα:


    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.

Σύνταξη:

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

Παράμετροι:

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.

Παράδειγμα:

Σε Basic

    MsgBox SF_String.HashStr("œ∑¡™£¢∞§¶•ªº–≠œ∑´®†¥¨ˆøπ‘åß∂ƒ©˙∆˚¬", "MD5")
    ' c740ccc2e201df4b2e2b4aa086f35d8a
  
Σε 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.

Σύνταξη:

svc.HtmlEncode(inputstr: str): str

Παράμετροι:

inputstr: The string to encode.

Παράδειγμα:


    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.

Σύνταξη:

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

Παράμετροι:

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"

The dash (-) may be replaced by a dot (.), a slash (/) or a space.

If the format is invalid, the method returns False.

Παράδειγμα:

Σε Basic

    MsgBox SF_String.IsADate("2020-12-31", "YYYY-MM-DD") ' True
  
note

This method checks the format of the input string without performing any calendar-specific checks. Hence it does not test the input string for leap years or months with 30 or 31 days. For that, refer to the IsDate built-in function.


The example below shows the difference between the methods IsADate (ScriptForge) and the IsDate (built-in) function.


    Dim myDate as String : myDate = "2020-02-30"
    MsgBox SF_String.IsADate(myDate, "YYYY-MM-DD") 'True
    MsgBox IsDate(myDate) ' False
  
Σε Python

    svc = CreateScriptService("String")
    s_date = "2020-12-31"
    result = svc.IsADate(s_date) # True
  

IsAlpha

Returns True if all characters in the string are alphabetic.

Alphabetic characters are those characters defined in the Unicode Character Database as Letter.

Σύνταξη:

svc.IsAlpha(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:


    MsgBox SF_String.IsAlpha("àénΣlPµ") ' True
    MsgBox SF_String.IsAlpha("myVar3") ' False
  

IsAlphaNum

Returns True if all characters in the string are alphabetic, digits or "_" (underscore). The first character must not be a digit.

Σύνταξη:

svc.IsAlphaNum(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:


    MsgBox SF_String.IsAlphaNum("_ABC_123456_abcàénΣlPµ") ' True
    MsgBox SF_String.IsAlphaNum("123ABC") ' False
  

IsAscii

Returns True if all characters in the string are Ascii characters.

Σύνταξη:

svc.IsAscii(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:


    MsgBox SF_String.IsAscii("a%?,25") ' True
    MsgBox SF_String.IsAscii("abcàénΣlPµ") ' False
  

IsDigit

Returns True if all characters in the string are digits.

Σύνταξη:

svc.IsDigit(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:


    MsgBox SF_String.IsDigit("123456") ' True
    MsgBox SF_String.IsDigit("_12a") ' False
  

IsEmail

Returns True if the string is a valid email address.

Σύνταξη:

svc.IsEmail(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:

Σε Basic

    MsgBox SF_String.IsEmail("first.last@something.org") ' True
    MsgBox SF_String.IsEmail("first.last@something.com.br") ' True
    MsgBox SF_String.IsEmail("first.last@something.123") ' False
  
Σε Python

    svc = CreateScriptService("String")
    bas = CreateScriptService("Basic")
    bas.MsgBox(svc.IsEmail("first.last@something.org")) # True
    bas.MsgBox(svc.IsEmail("first.last@something.com.br")) # True
    bas.MsgBox(svc.IsEmail("first.last@something.123")) # False
  

IsFileName

Returns True if the string is a valid filename in a given operating system.

Σύνταξη:

svc.IsFileName(inputstr: str, [osname: str]): bool

Παράμετροι:

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

osname: The operating system name, as a string. It can be "WINDOWS", "LINUX", "MACOSX" or "SOLARIS".

The default value is the current operating system on which the script is running.

Παράδειγμα:

Σε Basic

    MsgBox SF_String.IsFileName("/home/user/Documents/a file name.odt", "LINUX") ' True
    MsgBox SF_String.IsFileName("C:\home\a file name.odt", "LINUX") ' False
    MsgBox SF_String.IsFileName("C:\home\a file name.odt", "WINDOWS") ' True
  
Σε Python

    svc = CreateScriptService("String")
    bas = CreateScriptService("Basic")
    bas.MsgBox(svc.IsFileName("/home/user/Documents/a file name.odt", "LINUX")) # True
    bas.MsgBox(svc.IsFileName(r"C:\home\a file name.odt", "LINUX")) # False
    bas.MsgBox(svc.IsFileName(r"C:\home\a file name.odt", "WINDOWS")) # True
  

IsHexDigit

Returns True if all characters in the string are hexadecimal digits.

Σύνταξη:

svc.IsHexDigit(inputstr: str): bool

Παράμετροι:

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

The hexadecimal digits may be prefixed with "0x" or "&H".

Παράδειγμα:


    MsgBox SF_String.IsHexDigit("&H00FF") ' True
    MsgBox SF_String.IsHexDigit("08AAFF10") ' True
    MsgBox SF_String.IsHexDigit("0x18LA22") ' False
  

IsIBAN

Returns True if the string is a valid International Bank Account Number (IBAN). The comparison is not case-sensitive.

Σύνταξη:

svc.IsIBAN(inputstr: str): bool

Παράμετροι:

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

Επιστρεφόμενη τιμή:

True if the string contains a valid IBAN number.

Παράδειγμα:


    ' Basic
    MsgBox SF_String.IsIBAN("BR15 0000 0000 0000 1093 2840 814 P2") ' True
  

    # Python
    result = svc.IsIBAN("BR15 0000 0000 0000 1093 2840 814 P2") # True
  

IsIPv4

Returns True if the string is a valid IP(v4) address.

Σύνταξη:

svc.IsIPv4(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:

Σε Basic

    MsgBox SF_String.IsIPv4("192.168.1.50") ' True
    MsgBox SF_String.IsIPv4("192.168.50") ' False
    MsgBox SF_String.IsIPv4("255.255.255.256") ' False
  
Σε Python

    svc = CreateScriptService("String")
    bas = CreateScriptService("Basic")
    bas.MsgBox(svc.IsIPv4("192.168.1.50")) # True
    bas.MsgBox(svc.IsIPv4("192.168.50")) # False
    bas.MsgBox(svc.IsIPv4("255.255.255.256")) # False
  

IsLike

Returns True if the whole input string matches a given pattern containing wildcards.

Σύνταξη:

svc.IsLike(inputstr: str, pattern: str, [casesensitive: bool]): bool

Παράμετροι:

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

pattern: The pattern as a string. Wildcards are:

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

Παράδειγμα:

Σε Basic

    MsgBox SF_String.IsLike("aAbB", "?A*") ' True
    MsgBox SF_String.IsLike("C:\a\b\c\f.odb", "?:*.*") ' True
    MsgBox SF_String.IsLike("name:host", "?*@?*") ' False
    MsgBox SF_String.IsLike("@host", "?*@?*") ' False
  
Σε Python

    svc = CreateScriptService("String")
    bas = CreateScriptService("Basic")
    bas.MsgBox(svc.IsLike("aAbB", "?A*")) # True
    bas.MsgBox(svc.IsLike(r"C:\a\b\c\f.odb", "?:*.*")) # True
    bas.MsgBox(svc.IsLike("name:host", "?*@?*")) # False
    bas.MsgBox(svc.IsLike("@host", "?*@?*")) # False
  

IsLower

Returns True if all characters in the string are in lowercase. Non-alphabetic characters are ignored.

Σύνταξη:

svc.IsLower(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:


    MsgBox SF_String.IsLower("abc'(-xy4z") ' True
    MsgBox SF_String.IsLower("1234") ' True
    MsgBox SF_String.IsLower("abcDefg") ' False
  

IsPrintable

Returns True if all characters in the string are printable.

Σύνταξη:

svc.IsPrintable(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:


    MsgBox SF_String.IsPrintable("àén ΣlPµ Русский") ' True
    MsgBox SF_String.IsPrintable("First line." & Chr(10) & "Second Line.") ' False
  

IsRegex

Returns True if the whole input string matches a given regular expression.

Σύνταξη:

svc.IsRegex(inputstr: str, regex: str, [casesensitive: bool]): bool

Παράμετροι:

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

regex: The regular expression. If empty, the method returns False.

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

Παράδειγμα:


        MsgBox SF_String.IsRegex("aAbB", "[A-Za-z]+") ' True
        MsgBox SF_String.IsRegex("John;100", "[A-Za-z]+;[0-9]+") ' True
        MsgBox SF_String.IsRegex("John;100;150", "[A-Za-z]+;[0-9]+") ' False
      

IsSheetName

Returns True if the input string is a valid Calc sheet name.

Σύνταξη:

svc.IsSheetName(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:

Σε Basic

    MsgBox SF_String.IsSheetName("1àbc + ""déf""") ' True
    MsgBox SF_String.IsSheetName("[MySheet]") ' False
  
Σε Python

    svc = CreateScriptService("String")
    bas = CreateScriptService("Basic")
    bas.MsgBox(svc.IsSheetName("1àbc + ""déf""")) # True
    bas.MsgBox(svc.IsSheetName("[MySheet]")) # False
  
note

A sheet name must not contain the characters [ ] * ? : / \ or the character ' (apostrophe) as first or last character.


IsTitle

Returns True if the first character of every word is in uppercase and the other characters are in lowercase.

Σύνταξη:

svc.IsTitle(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:


    MsgBox SF_String.IsTitle("This Is The Title Of My Book") ' True
    MsgBox SF_String.IsTitle("This is the Title of my Book") ' False
    MsgBox SF_String.IsTitle("Result Number 100") ' True
  

IsUpper

Returns True if all characters in the string are in uppercase. Non alphabetic characters are ignored.

Σύνταξη:

svc.IsUpper(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:


    MsgBox SF_String.IsUpper("ABC'(-XYZ") ' True
    MsgBox SF_String.IsUpper("A Title") ' False
  

IsUrl

Returns True if the string is a valid absolute URL (Uniform Resource Locator) address. Only the http, https and ftp protocols are supported.

Σύνταξη:

svc.IsUrl(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:

Σε Basic

    MsgBox SF_String.IsUrl("http://foo.bar/?q=Test%20URL-encoded%20stuff") ' True
    MsgBox SF_String.IsUrl("www.somesite.org") ' False
  
Σε Python

    svc = CreateScriptService("String")
    bas = CreateScriptService("Basic")
    bas.MsgBox(svc.IsUrl("http://foo.bar/?q=Test%20URL-encoded%20stuff")) # True
    bas.MsgBox(svc.IsUrl("www.somesite.org")) # False
  

IsWhitespace

Returns True if all characters in the string are whitespaces

Σύνταξη:

svc.IsWhitespace(inputstr: str): bool

Παράμετροι:

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

Παράδειγμα:


    MsgBox SF_String.IsWhitespace("    ") ' True
    MsgBox SF_String.IsWhitespace(" " & Chr(9) & Chr(10)) ' True
    MsgBox SF_String.IsWhitespace("") ' False
  

JustifyCenter

Returns the input string center-justified.

The leading and trailing white spaces are stripped and the remaining characters are completed left and right up to a specified total length with the character padding.

Σύνταξη:

svc.JustifyCenter(inputstr: str, [length: int], [padding: str]): str

Παράμετροι:

inputstr: The string to be center-justified. If empty, the method returns an empty string.

length: The length of the resulting string (default = the length of the input string).

If the specified length is shorter than the center-justified input string, then the returned string is truncated.

padding: The single character to be used as padding (default = the Ascii space " ").

Παράδειγμα:


    MsgBox SF_String.JustifyCenter("Title", Length := 11) ' "   Title   "
    MsgBox SF_String.JustifyCenter("    ABCDEF", Padding := "_") ' "__ABCDEF__"
    MsgBox SF_String.JustifyCenter("A Long Title", Length := 5) ' "ong T"
  

JustifyLeft

Returns the input string left-justified.

The leading white spaces are stripped and the remaining characters are completed to the right up to a specified total length with the character padding.

Σύνταξη:

svc.JustifyLeft(inputstr: str, [length: int], [padding: str]): str

Παράμετροι:

inputstr: The string to be left-justified. If empty, the method returns an empty string.

length: The length of the resulting string (default = the length of the input string).

If the specified length is shorter than the left-justified input string, then the returned string is truncated.

padding: The single character to be used as padding (default = the Ascii space " ").

Παράδειγμα:


    MsgBox SF_String.JustifyLeft("Title", Length := 10) ' "Title     "
    MsgBox SF_String.JustifyLeft("    ABCDEF", Padding := "_") ' "ABCDEF____"
    MsgBox SF_String.JustifyLeft("A Long Title", Length := 5) ' "A Lon"
  

JustifyRight

Returns the input string right-justified.

The leading white spaces are stripped and the remaining characters are completed to the left up to a specified total length with the character padding.

Σύνταξη:

svc.JustifyRight(inputstr: str, [length: int], [padding: str]): str

Παράμετροι:

inputstr: The string to be right-justified. If empty, the method returns an empty string.

length: The length of the resulting string (default = the length of the input string).

If the specified length is shorter than the right-justified input string, then the returned string is truncated.

padding: The single character to be used as padding (default = the Ascii space " ").

Παράδειγμα:


    MsgBox SF_String.JustifyRight("Title", Length := 10) ' "     Title"
    MsgBox SF_String.JustifyRight("  ABCDEF  ", Padding := "_") ' "____ABCDEF"
    MsgBox SF_String.JustifyRight("A Long Title", Length := 5) ' "Title"
  

Quote

Returns the input string enclosed in single or double quotes. Existing quotes are left unchanged, including leading and/or trailing quotes.

Σύνταξη:

svc.Quote(inputstr: str, [quotechar: str]): str

Παράμετροι:

inputstr: The string to quote.

quotechar: Either the single (') or double (") quote (default).

Παράδειγμα:


    MsgBox SF_String.Quote("Text Value")
    ' "Text Value"
    MsgBox SF_String.Quote("Book Title: ""The Arabian Nights""", "'")
    ' 'Book Title: "The Arabian Nights"'
  
tip

This method can be useful while preparing a string field to be stored in a csv-like file, which requires that text values be enclosed with single or double quotes.


ReplaceChar

Replaces all occurrences of the characters specified in the Before parameter by the corresponding characters specified in After.

If the length of Before is greater than the length of After, the residual characters in Before are replaced by the last character in After.

Σύνταξη:

svc.ReplaceChar(inputstr: str, before: str, after: str): str

Παράμετροι:

inputstr: The input string on which replacements will occur.

before: A string with the characters that will be searched in the input string for replacement.

after: A string with the new characters that will replace those defined in before.

Παράδειγμα:


    ' Replaces accented characters
    MsgBox SF_String.ReplaceChar("Protégez votre vie privée", "àâãçèéêëîïôöûüýÿ", "aaaceeeeiioouuyy")
    ' "Protegez votre vie privee"
    MsgBox SF_String.ReplaceChar("Protégez votre vie privée", "àâãçèéêëîïôöûüýÿ", "")
    ' "Protgez votre vie prive"
    MsgBox SF_String.ReplaceChar("àâãçèéêëîïôöûüýÿ", "àâãçèéêëîïôöûüýÿ", "aaaceeeeiioouuyy")
    ' "aaaceeeeiioouuyy"
  

The SF_String service provides useful public constants for the Latin character sets, as shown in the example below:


    MsgBox SF_String.ReplaceChar("Protégez votre vie privée", SF_String.CHARSWITHACCENT, SF_String.CHARSWITHOUTACCENT)
    ' "Protegez votre vie privee"
  

ReplaceRegex

Replaces all occurrences of a given regular expression by a new string.

Σύνταξη:

svc.ReplaceRegex(inputstr: str, regex: str, newstr: str, [casesensitive: bool]): str

Παράμετροι:

inputstr: The input string on which replacements will occur.

regex: The regular expression.

newstr: The replacing string.

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

Παράδειγμα:


    MsgBox SF_String.ReplaceRegex("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "[a-z]", "x", CaseSensitive := True)
    ' "Lxxxx xxxxx xxxxx xxx xxxx, xxxxxxxxxxx xxxxxxxxxx xxxx." (each lowercase letter is replaced by "x")
    MsgBox SF_String.ReplaceRegex("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "\b[a-z]+\b", "x", CaseSensitive := False)
    ' "x x x x x, x x x." (each word is replaced by "x")
  

ReplaceStr

Replaces in a string some or all occurrences of an array of strings by an array of new strings.

Σύνταξη:

svc.ReplaceStr(inputstr: str, oldstr: str, newstr: str, [occurrences: int], [casesensitive: bool]): str

Παράμετροι:

inputstr: The input string on which replacements will occur.

oldstr: A single string or an array of strings. Zero-length strings are ignored.

newstr: The replacing string or the array of replacing strings.

If oldstr is an array, each occurrence of any of the items in oldstr is replaced by newstr.

If oldstr and newstr are arrays, replacements occur one by one up to the UBound(newstr).

If oldstr has more entries than newstr, then the residual elements in oldstr are replaced by the last element in newstr.

occurrences: The maximum number of replacements. The default value is 0, meaning that all occurrences will be replaced.

When oldstr is an array, the occurrence parameter is computed separately for each item in the array.

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

Παράδειγμα:


    MsgBox SF_String.ReplaceStr("100 xxx 200 yyy", Array("xxx", "yyy"), Array("(1)", "(2)"), CaseSensitive := False)
    ' "100 (1) 200 (2)"
    MsgBox SF_String.ReplaceStr("abCcdefghHij", Array("c", "h"), Array("Y", "Z"), CaseSensitive := False)
    ' "abYYdefgZZij"
  

Represent

Returns a string with a readable representation of the argument, truncated at a given length. This is useful mainly for debugging or logging purposes.

If the anyvalue parameter is an object, it will be enclosed with square brackets "[" and "]".

In strings, tabs and line breaks are replaced by \t, \n or \r.

If the final length exceeds the maxlength parameter, the latter part of the string is replaced by " ... (N)" where N is the total length of the original string before truncation.

Σύνταξη:

svc.Represent(anyvalue: any, [maxlength: int]): str

Παράμετροι:

anyvalue: The input value to be represented. It can be any value, such as a string, an array, a Basic object, a UNO object, etc.

maxlength: The maximum length of the resulting string. The default value is 0, meaning there is no limit to the length of the resulting representation.

Παράδειγμα:


    MsgBox SF_String.Represent("this is a usual string") ' "this is a usual string"
    MsgBox SF_String.Represent("this is a usual string", 15) ' "this i ... (22)"
    MsgBox SF_String.Represent("this is a" & Chr(10) & " 2-lines string") ' "this is a\n 2-lines string"
    MsgBox SF_String.Represent(Empty) ' "[EMPTY]"
    MsgBox SF_String.Represent(Null) ' "[NULL]"
    MsgBox SF_String.Represent(Pi) ' "3.142"
    MsgBox SF_String.Represent(CreateUnoService("com.sun.star.util.PathSettings")) ' "[com.sun.star.comp.framework.PathSettings]"
  

Note that the representation of data types such as Arrays and ScriptForge.Dictionary object instances include both the data type and their values:


    ' An example with a Basic built-in Array
    MsgBox SF_String.Represent(Array(1, 2, "Text" & Chr(9) & "here"))
    ' "[ARRAY] (0:2) (1, 2, Text\there)"
    ' An example with a ScriptForge Array
    Dim aValues as Variant
    aValues = SF_Array.RangeInit(1, 5)
    MsgBox SF_String.Represent(aValues)
    ' "[ARRAY] (0:4) (1.0, 2.0, 3.0, 4.0, 5.0)"
    ' An example with a ScriptForge Dictionary
    Dim myDict As Variant : myDict = CreateScriptService("Dictionary")
    myDict.Add("A", 1) : myDict.Add("B", 2)
    MsgBox SF_String.Represent(myDict)
    ' "[Dictionary] ("A":1, "B":2)"
  

Reverse

Returns the input string in reversed order.

This method is equivalent to the built-in StrReverse Basic function.

note

To use the StrReverse function, the statement Option VBASupport 1 must be present in the module.


Σύνταξη:

svc.Reverse(inputstr: str): str

Παράμετροι:

inputstr: The string to be reversed.

Παράδειγμα:


    MsgBox SF_String.Reverse("abcdefghij") ' "jihgfedcba"
  

SplitLines

Returns a zero-based array of strings with the lines in the input string. Each item in the array is obtained by splitting the input string at newline characters.

Σύνταξη:

svc.SplitLines(inputstr: str, [keepbreaks: int]): str[0..*]

Παράμετροι:

inputstr: The string to be split.

keepbreaks: When True, line breaks are preserved in the output array (default = False).

Παράδειγμα:


    Dim a as Variant
    a = SF_String.SplitLines("Line1" & Chr(10) & "Line2" & Chr(13) & "Line3")
    ' a = Array("Line1", "Line2", "Line3")
    a = SF_String.SplitLines("Line1" & Chr(10) & "Line2" & Chr(13) & "Line3" & Chr(10))
    ' a = Array("Line1", "Line2", "Line3", "")
    a = SF_String.SplitLines("Line1" & Chr(10) & "Line2" & Chr(13) & "Line3" & Chr(10), KeepBreaks := True)
    ' a = Array("Line1\n", "Line2\r", "Line3\n", "")
  

SplitNotQuoted

Splits a string into an array of elements using a specified delimiter.

If a quoted substring contains a delimiter, it is ignored. This is useful when parsing CSV-like records that contain quoted strings.

Σύνταξη:

svc.SplitNotQuoted(inputstr: str, [delimiter: str], [occurrences: int], [quotechar: str]): str[0..*]

Παράμετροι:

inputstr: The string to be split.

delimiter: A string of one or more characters that will be used as delimiter. The default delimiter is the Ascii space " " character.

occurrences: The maximum number of substrings to return. The default value is 0, meaning that there is no limit to the number of returned strings.

quotechar: Either the single (') or double (") quote.

Παράδειγμα:

Σε Basic

    arr1 = SF_String.SplitNotQuoted("abc def ghi")
    ' arr1 = Array("abc", "def", "ghi")
    arr2 = SF_String.SplitNotQuoted("abc,""def,ghi""", ",")
    ' arr2 = Array("abc", """def,ghi""")
    arr3 = SF_String.SplitNotQuoted("abc,""def\"",ghi""", ",")
     ' arr3 = Array("abc", """def\"",ghi""")
    arr4 = SF_String.SplitNotQuoted("abc,""def\"",ghi"""",", ",")
    ' arr4 = Array("abc", """def\"",ghi""", "")
  
Σε Python

    svc = CreateScriptService("String")
    arr1 = svc.SplitNotQuoted('abc def ghi')
    # arr1 = ('abc', 'def', 'ghi')
    arr2 = svc.SplitNotQuoted('abc,"def,ghi"', ",")
    # arr2 = ('abc', '"def,ghi"')
    arr3 = svc.SplitNotQuoted(r'abc,"def\",ghi"', ",")
    # arr3 = ('abc', '"def\\",ghi"')
    arr4 = svc.SplitNotQuoted(r'abc,"def\",ghi"",', ",")
    # arr4 = ('abc', '"def\\",ghi""', '')
  
note

Beware of the differences between Basic and Python when representing strings. For example, in Basic two "" characters inside a string are interpreted as a single " character. In Python, strings enclosed with single quotes can contain " characters without having to double them.


StartsWith

Returns True if the first characters of a string are identical to a given substring.

This method returns False if either the input string or the substring have a length = 0 or when the substring is longer than the input string.

Σύνταξη:

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

Παράμετροι:

inputstr: The string to be tested.

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

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

Παράδειγμα:


    MsgBox SF_String.StartsWith("abcdefg", "ABC") 'True
    MsgBox SF_String.StartsWith("abcdefg", "ABC", CaseSensitive := True) 'False
  

TrimExt

Returns the input string without its leading and trailing whitespaces.

Σύνταξη:

svc.TrimExt(inputstr: str): str

Παράμετροι:

inputstr: The string to trim.

Παράδειγμα:


    MsgBox SF_String.TrimExt(" Some text.  ") ' "Some text."
    MsgBox SF_String.TrimExt("   ABCDEF" & Chr(9) & Chr(10) & Chr(13) & " ") ' "ABCDEF"
  

Unescape

Converts any escaped sequence (\\, \n, \r, \t) in the input string to their corresponding Ascii character.

Σύνταξη:

svc.Unescape(inputstr: str): str

Παράμετροι:

inputstr: The string to be converted.

Παράδειγμα:


    MsgBox SF_String.Unescape("abc\n\tdef\\n")
    ' "abc" & Chr(10) & Chr(9) & "def\n"
  

Unquote

Removes the single or double quotes enclosing the input string.

This is useful when parsing CSV-like records that contain quoted strings.

Σύνταξη:

svc.Unquote(inputstr: str, [quotechar: str]): str

Παράμετροι:

inputstr: The string to unquote.

quotechar: Either the single (') or double (") quote (default).

Παράδειγμα:


    Dim s as String
    ' s = "Some text" (without enclosing quotes)
    s = SF_String.Unquote("""Some text""")
    ' The string below does not have enclosing quotes, so it remains unchanged
    ' s = "Some text" (unchanged)
    s = SF_String.Unquote("Some text")
    ' Quotes inside the string are not removed
    ' s = "The ""true"" meaning" (unchanged)
    s = SF_String.Unquote("The ""true"" meaning")
  

Wrap

Converts the input string into an array of substrings so that each item in the array has at most a given number of characters.

In practice, this method returns a zero-based array of output lines, without newlines at the end, except for the pre-existing line-breaks.

Tabs are expanded using the same procedure performed by the ExpandTabs method.

Symbolic line breaks are replaced by their equivalent Ascii characters.

If the wrapped output has no content, the returned array is empty.

Σύνταξη:

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

Παράμετροι:

inputstr: The string to wrap.

width: The maximum number of characters in each line (Default = 70).

tabsize: Before wrapping the text, the existing TAB Chr(9) characters are replaced with spaces. The argument tabsize defines the TAB stops at TabSize + 1, 2 * TabSize + 1 , ... N * TabSize + 1 (Default = 8).

Παράδειγμα:

Σε Basic

    a = "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."
    b = SF_String.Wrap(a, 20)
    ' Array("Neque porro ", "quisquam est qui ", "dolorem ipsum quia ", "dolor sit amet, ", "consectetur, ", "adipisci velit...")
  
Σε Python

    a = "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."
    b = svc.Wrap(a, 20)
    # ('Neque porro ', 'quisquam est qui ', 'dolorem ipsum quia ', 'dolor sit amet, ', 'consectetur, ', 'adipisci velit...')
  
warning

Όλες οι ρουτίνες ή αναγνωριστικά του ScriptForge στη Basic που έχουν το πρόθεμα χαρακτήρα υπογράμμισης "_" δεσμεύονται για εσωτερική χρήση. Δεν προορίζονται να χρησιμοποιηθούν σε μακροεντολές Basic ή σενάρια Python.