String Methods

String Methods

Method Use
strCat() Catenate a string onto another string
strDeb() Delete leading, trailing, and repeated white space characters from strings
strFind() Search for one or more substrings in a string scalar
charToStr() Convert a character or byte vector to a string scalar
strLen() Lengths of strings in an array
strLookup() Find location of a substring in each string in an array
strLower() Replace uppercase letters with lowercase letters in strings
strLtrim() Delete leading white space characters from strings
strReplace() Replace substrings in a string scalar
strRtrim() Delete trailing white space characters from strings
strToNumber() Convert fields of characters in a string scalar to numbers
strUpper() Replace lowercase letters with uppercase letters in strings
strWith() Remove any characters from strings that are not in another string
strWithout() Remove any characters from strings in another string
subStr() Select substrings at specified locations
strToChar() Convert a string scalar to a character vector
strToByte() Convert a string scalar to a byte vector
strToCharMatrix() Convert a vector of strings to a padded character matrix
strToByteMatrix() Convert a vector of strings to a padded byte matrix

SmArray method Description
a.strCat(b) Catenate a string onto another string

Properties: Scalar

strCat() catenates string scalars together to produce a new string scalar. The subject array and the argument must be string type and of the same shape. Either array can also be a scalar, in which case it is catenated with each item of the other array.

Java /C# Example:

SmArray.scalar( "Smart" ).strCat( "Arrays" ).showDebug();

S[]"SmartArrays"

 

SmArray surnames = SmArray.vector("Berlioz", "Bizet", "Saint-Saens");

SmArray names = SmArray.vector("Hector", "Georges", "Camille" );

// catenate a scalar string onto each string:

surnames = surnames.strCat(", ");

surnames.showDebug();

s[3]"Berlioz, " "Bizet, " "Saint-Saens, " );

// catenate corresponding items of conforming vectors

surnames.strCat( names ).showDebug();

s[3]"Berlioz, Hector" "Bizet, Georges" "Saint-Saens, Camille"

 

C++ Example:

SmArray::scalar( "Smart" ).strCat( "Arrays" ).showDebug();

S[]"SmartArrays"

 

SmArray surnames = SmArray::vector("Berlioz", "Bizet", "Saint-Saens");

SmArray names = SmArray::vector("Hector", "Georges", "Camille" );

// catenate a scalar string onto each string:

surnames = surnames.strCat(", ");

surnames.showDebug();

s[3]"Berlioz, " "Bizet, " "Saint-Saens, " );

// catenate corresponding items of conforming vectors

surnames.strCat( names ).showDebug();

s[3]"Berlioz, Hector" "Bizet, Georges" "Saint-Saens, Camille"

SmArray method Description
a.charToStr() Convert a character or byte vector to a string scalar
a.strToChar() Convert a string scalar to a character vector
a.strToByte() Convert a string scalar to a byte vector

Properties:

charToStr() accepts a character vector as its argument and returns a scalar string as its result. The datatype of the character vector can be either 1-byte (dtByte) or 2-byte (dtChar). strToChar() accepts a string scalar as its argument and returns a character (dtChar) vector. strToByte() accepts a string scalar and returns a vector of bytes, assuming all characters in the string are between 0 and 255; otherwise it throws an SA_CANNOT_CONVERT exception.

Java / C# Example:

SmArray.scalar( "A string" ).strToChar().showDebug();

W[8]A    s t r i n g

SmArray::scalar( "A string" ).strToByte().showDebug();

C[8]A    s t r i n g

SmArray.scalar("abcde").strToChar().reverse().charToStr().showDebug();

S[]"edcba"

C++ Example:

SmArray::scalar( "A string" ).strToChar().showDebug();

W[8]A    s t r i n g

SmArray::scalar( "A string" ).strToByte().showDebug();

C[8]A    s t r i n g

SmArray::scalar("abcde").strToChar().reverse().charToStr().showDebug();

S[]"edcba"

SmArray method Description
a.strToCharMatrix(ncols [,padchar] ) Convert a vector of strings to a padded character matrix
a.strToByteMatrix(ncols [,padchar] ) Convert a vector of strings to a padded byte matrix

Properties:

strToByteMatrix() and strToCharMatrix() convert string vectors into matrices with one row for each string. This is a useful form to hold variable-length character sequences in cases where strings are inefficient. The character or byte matrix arrays can be used for file-mapped arrays, for example, whereas string arrays can not, and they have the added advantage of not filling the SmartArrays string table.

The resulting matrix will have one row for each string and ncols columns. If ncols is -1, the matrix will have as many columns as the length of the longest string. If ncols is less than the length of the longest string, these methods produce an SA_LENGTH exception.

By default the NUL character is used to pad strings to the number of columns in the matrix. You can optionally specify an alternative character to use instead of NUL. The examples below use '.' so the pad characters are visible when displayed.

strToByteMatrix() will throw an SA_CANNOT_CONVERT exception if any 2-byte character in the string cannot be converted to a 1-byte character.

Java / C# Example:

SmArray.vector( "vector","of","strings" ).strToCharMatrix(-1,SmArray.scalar('.')).show();

v e c t o r .

o f . . . . .

s t r i n g s

C++ Example:

SmArray::vector( "vector","of","strings" ).strToCharMatrix(-1,'.').show();

v e c t o r .

o f . . . . .

s t r i n g s

SmArray method Description
a.strLen() length of strings in a

strLen() accepts any array as its argument and returns an integer array of the same shape that contains the length of the string item at each position or -1 if the item is not a string.

Java / C# Example:

SmArray v = SmArray.vector( "One","Twelve" ).catenate(3);

v.showDebug();

M[3]"One" "Two" 3

s.strLen().show();

3 6 -1

C++ Example:

SmArray v = SmArray::vector( "One","Twelve" ).catenate(3);

v.showDebug();

M[3]"One" "Two" 3

s.strLen().show();

3 6 -1

SmArray method Description
a.strLookup(b) Find location of substring b in each string in a
a.strLookup(b,offsets) Find location of substring b starting from an offset in each string in a

strLookup() searches each string in the subject array a, which must be of type dtString, for the string scalar argument b.  The result is an integer array of the same shape as a containing the offset in each string where b is found, or -1 if it is not found.  The optional offsets array is an integer scalar or array with the same shape as a, indicating where the search should begin.  If the offset is negative, the search begins at an offset from the end of each string. In this case, the result still indicates the position of the substring from the start of the string.

Java / C# Example:

SmArray v = SmArray.vector( "This", "is", "a", "test" );

SmArray s = SmArray.scalar( "is" );

v.strLookup(s).show();

2 0 -1 -1

v.strLookup(s,2).show();

2 -1 -1 -1

C++ Example:

SmArray v = SmArray::vector( "This", "is", "a", "test" );

SmArray s = SmArray::scalar( "is" );

v.strLookup(s).show();

2 0 -1 -1

v.strLookup(s,2).show();

2 -1 -1 -1

strLookup() can be used to perform several common string-search algorithms, such as finding all strings that contain a substring:

stringvec.strLookup(substring).ne(-1); // all strings that contain substring

stringvec.strLookup(substring).eq(0);  // all strings that begin with substring

string.strLookup( substring, -substring.strLen() )

    .eq( stringvec.strLen().minus( substring.strLen() ).maximum(0) ); // all strings that end with a substring

 

SmArray method Description
a.strDeb() Delete leading, trailing, and repeated white space characters from strings in a
a.strLtrim() Delete leading white space characters from strings in a
a.strRtrim() Delete trailing white space characters strings in a

Properties: String filter; only string items in the array are affected; others are copied to the result unchanged.

Note: Spaces and tabs are treated as white space characters.

Java / C# Example:

SmArray s = SmArray.scalar( "  a   string  " );

s.showDebug();

S[]"  a   string  "

s.ltrim().showDebug();

S[]"a   string  "

s.rtrim().showDebug();

S[]"  a   string"

s.deb().showDebug();

S[]"a string"

C++ Example:

SmArray s = SmArray::scalar( "  a   string  " );

s.showDebug();

S[]"  a   string  "

s.ltrim().showDebug();

S[]"a   string  "

s.rtrim().showDebug();

S[]"  a   string"

s.deb().showDebug();

S[]"a string"

SmArray method Description
a.strLower() Replace uppercase letters with lowercase letters in strings in a
a.strUpper() Replace lowercase letters with uppercase letters in strings in a

Properties: String filter; only string items in the array are affected; others are copied to the result unchanged.

Note: All Unicode characters for which there are defined upper and lower case forms are handled.

Java / C# Example:

SmArray a = SmArray.vector( "Washington", "Adams" );

s.showDebug();

S[2]"Washington" "Adams"

s.strUpper().showDebug();

S[2]"WASHINGTON" "ADAMS"

s.strLower().showDebug();

S[2]"washington" "adams"

C++ Example:

SmArray a = SmArray::vector( "Washington", "Adams" );

s.showDebug();

S[2]"Washington" "Adams"

s.strUpper().showDebug();

S[2]"WASHINGTON" "ADAMS"

s.strLower().showDebug();

S[2]"washington" "adams"

SmArray method Description
a.strWith( b ) Remove any characters from strings in a that are not in string b.
a.strWithout( b ) Remove any characters from strings in a that are in string b.

Properties: String filter; only string items in the array are affected; others are copied to the result unchanged.

These methods can be applied to any array. The argument must be a string scalar. strWith() filters strings according to their intersection with the set of characters in the argument, while strWithout() filters with the set difference.

Java / C# Example:

SmArray s = SmArray.scalar( "<td> $123,000 </td> <td> $456,000 </td>" );

s.strWithout( " $,").showDebug();

S[]"<td>123000</td><td>456000</td>"

s.strWith( " 0123456789,").showDebug();

S[]"123000 456000"

C++ Example:

SmArray s = SmArray::scalar( "<td> $123,000 </td> <td> $456,000 </td>" );

s.strWithout( " $,").showDebug();

S[]"<td>123000</td><td>456000</td>"

s.strWith( " 0123456789,").showDebug();

S[]"123000 456000"

SmArray method Description
a.strFind( b ) Search for one or more substrings in string scalar a.

The subject array must be a string scalar. The argument can be a string array of any shape. The result of strFind() is a boolean array whose leading dimensions are the shape of b and whose last dimension is the length of the string scalar. A 1 in the result marks the position where the substring is found, including overlapping substrings. An empty string matches at every position.

Java / C# Example:

SmArray s = SmArray.scalar( "This is a smart array!" );

s.strFind( "array").showDebug();

B[22]0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0

s.strFind( SmArray.vector("This","is","smart") ).show();

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0

C++ Example:

SmArray s = SmArray::scalar( "This is a smart array!" );

s.strFind( "array").showDebug();

B[22]0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0

s.strFind( SmArray::vector("This","is","smart") ).show();

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0

SmArray method Description
a.strReplace(b) Replace substrings in string scalar a according to b.

The subject array must be a string scalar. The argument represents pairs of strings to locate and their replacements. It can be a 2-item string vector or a 2-column matrix of strings. The result is a string scalar where the first string in each pair is replaced with the second. For multiple strings replacements, the string matches are applied in the order in which they occur in the matrix. Overlapping matches are not replaced.

Java / C# Example:

SmArray s = SmArray.scalar( "This is a smart array!" );

s.strReplace( SmArray.vector( "smart array", "SmartArray").show();

This is a SmartArray!

SmArray replacments = SmArary.vector("This","That","is","was").reshapeBy(2,2);

s.strReplace( replacements ).show();

That was a smart array!

 

C++ Example:

SmArray s = SmArray::scalar( "This is a smart array!" );

s.strReplace( SmArray::vector( "smart array", "SmartArray").show();

This is a SmartArray!

SmArray replacments = SmArary.vector("This","That","is","was").reshapeBy(2,2);

s.strReplace( replacements ).show();

That was a smart array!

SmArray method Description
a.strToNumber() Convert fields of characters in a string scalar to numbers.

The subject array must be a string scalar. strToNumber() attempts to convert sequences of non-whitespace characters to numbers. The result is a floating-point vector containing the represented numbers. For non-blank fields that are not well-formed numbers, the result contains a NaN.

Java / C# Example:

SmArray s = SmArray.scalar( "1.23 -12E-1 , 123456" );

s.strToNumber().show();

1.23 -1.2 NaN 123456

C++ Example:

SmArray s = SmArray::scalar( "1.23 -12E-1 , 123456" );

s.strToNumber().show();

1.23 -1.2 NaN 123456

SmArray method Description
a.subStr( b ) Select substrings at specified locations.

The subject array can be a string array of any shape. The argument specifies the location and optional maximum length of the substrings to select from the subject array. It can be an integer scalar or 2-item vector. If different offsets or lengths are to be selected from each string, the argument can be an integer array whose rank is one greater than the rank of the subject array and whose leading dimensions match its shape. The last dimension must be either length 1 or length 2. The result of subStr() is a string array whose shape is the same shape as the subject array.

subStr() selects substrings beginning at an offset specified by the first item of the argument. Positive numbers select offsets from the beginning of each string, and negative numbers select offsets backward from the end of each string. The second item of the argument is optional and specifies the maximum length of the substring to select. If the length indicated exceeds the length of the string, the result string will contain only as many characters as are available in the source string. In other words, subStr never pads its result; if you need padding, you can use strToChar() and takeBy() to add fill characters and guarantee the length of the resulting strings.

Java / C# Example:

SmArray s = SmArray.scalar( "Washington" );

s.subStr( 4 ).showDebug();

S[]"ington"

s.subStr( SmArray.vector(4, 3) ).showDebug();

s[]"ing"

 

// select trailing substring

s.subStr( -3 ).showDebug();

s[]"ton"

s.subStr( SmArray.vector(-3,100) ).showDebug();

s[]"ton"

C++ Example:

SmArray s = SmArray.scalar( "Washington" );

s.subStr( 4 ).showDebug();

S[]"ington"

s.subStr( SmArray.vector(4, 3) ).showDebug();

s[]"ing"

 

// select trailing substring

s.subStr( -3 ).showDebug();

s[]"ton"

s.subStr( SmArray::vector(-3,100) ).showDebug();

s[]"ton"