String Operations

(1) Replace a substring with functions strReplace and regexReplace. With regexReplace, we can search a regular expression pattern, and we can also specify a starting position to search.

$ strReplace("this computer is faster than that computer", "computer", "desktop");
this desktop is faster than that desktop

$ regexReplace("this computer is faster than that computer", "computer", "desktop", 8);
this computer is faster than that desktop

$ regexReplace("this computer is faster than that computer", "\\b(comp)([^ ]*)", "desktop");
this desktop is faster than that desktop

(2) Generate a substring starting with the left or the right with functions left or right respectively. Use function:doc:/FunctionsandCommands/FunctionReferences/s/substr to get a substring with specified starting position and length.

$ left("Hello World", 5);
Hello

$ right("Hello World", 5);
World

$ substr("This is a test", 5, 2);
is

(3) Functions ltrim or rtrim removes the spaces on the left or right end of the string; function trim removes the spaces on both ends of the string; function strip removes all space, tab, new line, and carriage characters on both ends of a string.

$ ltrim("     Hello World   ");
Hello World

$ rtrim("Hello World   ")+"!";
Hello World!

$ trim("     Hello World   ")+"!";
Hello World!

$ strip("   \t  Hello World   ");
Hello World

(4) Pad specified characters to the left or the right side of a string with functions lpad and rpad respectively.

$ lpad("Hello",7);
Hello

$ lpad("Hello",7,"0");
00Hello

$ rpad("Hello",7,"0");
Hello00

(5) Repeat a string multiple times with function repeat .

$ repeat("ABC",3);
ABCABCABC

$ repeat(`ABC`DE,3);
["ABCABCABC","DEDEDE"]

(6) Convert strings to either lower case or upper case with functions lower or upper respectively.

$ lower `Chloe;
chloe

$ upper 'Christmas';
CHRISTMAS

(7) Use function concat to combine 2 strings into one string.

$ concat (`hello, `world);
helloworld

To combine multiple strings, we can use the operator “+”.

$ var1="Hello"
$ var2='World'
$ var1+", "+var2+"!";
Hello, World!

(8) Use function split to split a string into multiple strings with the specified delimiter.

$ split("xyz 1 ABCD 3241.32"," ");
["xyz","1","ABCD","3241.32"]

$ split("XOM|2018.02.15|76.21", "|");
["XOM","2018.02.15","76.21"]

(9) To calculate the length of a string, use function strlen .

$ strlen("Hello World!");
12

$ strlen(`XOM`MSFT`F`GM);
[3,4,1,2]

To count the number of words in a string, use function wc :

$ wc(`apple);
1

$ wc("This is a 7th generation iphone!");
6

$ wc("This is a 7th generation iphone!" "I wonder what the 8th generation looks like");
[6,8]

Use function regexCount to count how many times a string or a regular expression pattern occurs in a string.

$ regexCount("FB IBM FB IBM AMZN IBM", `IBM);
3

$ regexCount("FB IBM FB IBM AMZN IBM", `IBM, 7);
2

$ regexCount("this subject has a submarine as subsequence", "\\b(sub)([^ ]*)");
3

(10) Use functions startsWith and endsWith to test if a string starts or ends with another string.

$ startsWith('ABCDEF!', "ABC");
1

$ startsWith('ABCDEF!', "ABD");
0

$ endsWith('ABCDEF!', "F!");
1

$ endsWith('ABCDEF!', "E!");
0

(11) Function convertEncode changes the encoding of strings. Function fromUTF8 changes the encoding of strings from UTF-8. Function toUTF8 changes the encoding of strings to UTF-8.

$ convertEncode(["hello","DolphinDB"],"gbk","utf-8");
["hello","DolphinDB"]

$ fromUTF8(["hello","DolphinDB"],"gbk");
["hello","DolphinDB"]

$ toUTF8(["hello","DolphinDB"],"gbk");
["hello","DolphinDB"]
  1. Function charAt returns the characters at specified positions in a string.

$ s=charAt("abc",2);
$ s;
'c'

$ typestr(s);
CHAR

$ charAt(["hello","world"],[3,4]);
['l','d']

(13) Function isAlpha determines whether all characters in a string are alphabets. Function isUpper determines whether all the case-based characters (letters) of a string are uppercase. Function isLower determines whether all the case-based characters (letters) of the string are lowercase. Function isTitle determines whether a string is a titlecased string, which has the first character in each word uppercase and the remaining all characters lowercase alphabets.

$ isAlpha(["hello","hello world","1And1",string()]);
[true,false,false,false]

$ isUpper("123456ABC");
true

$ isLower("123456abc");
true

$ isTitle("Hello World");
true

(14) Function isNumeric, isDigit and isDecimal determines if all characters in a string are numbers. Function isAlNum determines whether all characters in a string are alphanumeric (either alphabets or numbers).

$ isNumeric("123456");
true

$ isDigit("1And1");
false

$ isDecimal("10.05");
false

$ isAlNum("123456abc");
true

(15) Function isSpace determines whether a string consists of only space.

$ isSpace(" \t ");
true