String Functions

  Basic C++ / Java Delphi / Pascal Java
(String Class)
Declare Dim str1 As String
Dim str1 As String * 5
char str1[256] str1 : string; string str1
String str1 = "abc";
Assignment str1 = "test" strcpy(str1, "test");
No error checking
str1 := 'test'; str1 = "test";
Compare if str1 = str2
StrComp(string1, string2)
strcmp(str1, str2)
stricmp(str1, str2)
strcmpi(str1, str2)
strcspn(str1, str2)
if str1 = str2
CompareStr(S1, S2)
CompareText(S1, S2)
SameText(S1, S2)
AnsiSameText(S1, S2)
AnsiSameCaption(S1, S2)
TMask.Matches(S2)
if (str1==str2)
str1.equals(str2)
str1.compareTo(str2)
Substring Mid(string, start[, length]) strncpy(str1, "test", 2);
strncpy(str1, str2[3], 2);
Copy(Str1, Index, Count);
str1[i];
str1.substring(1, 2);
Left, Right, Trim Left(string, length)
Right(string, length)
LTrim(string)
RTrim(string)
Trim(string)
N/A Delete(s, Start, Count)
TrimLeft(s)
TrimRight(s)
Trim(s)
trimBlanks(str1)
str1.trim()
Set Case UCase(string)
LCase(string)
strupr(str1);
strlwr(str1);
UpperCase(str1)
LowerCase(str1)
str1.toUpperCase()
str1.toLowerCase()
Concatenate str1 = str1 & str2
(& preferred)
str1 = str1 + str2
(+ is not reliable)
strcat(str1, str2); str3 := str1 + str2;
S := Concat('ABC', 'DEF');
str3 = str1 + str2;
str3 = str1 + 5;
str3 += str2;
str1.concat(str2);
Int to String str$(tempInt)
str(tempInt)
cstr(tempInt)
hex$(tempInt)
itoa(int1, str1, bytes); IntToStr(tempInt)
Str(X, s)
IntToHex(int, 4)
toString()
toString(int)
toBinaryString(int)
toHexString(int)
toOctalString(int)
String to Int val(str1)
cint(str1)
clng(str1)
atoi(char*)
atol(char*)
StrToInt(str1)
StrToIntDef(str1, 100)
Val(S; var V; var Code: Integer);
str1.valueOf(int)
Length len(TempStr) strlen(str1) Length(str1) str1.length()
Search InStr([start, ]str1, str2[, compare])
InstrRev(str1, str2 [,...])
strchr(str1, ch1)
strrchr(str1, ch1)
strstr(str1, str2)
strpbrk(str1, str2)
strtok(str1, str2, ??)
Pos(Substr: string; S: string): Integer; str1.indexOf(...)
str1.lastIndexOf(...)
Replace Replace(str1, str2, str3 [,...]) N/A StringReplace(str1, str2, str3 , flags)
s[i] := 'a'
str1.replace(..)
Characters only
Format Format(...) sprintf(...) Format(...)
FmtStr(...)
Format(...)
Parse N/A N/A Notes StringTokenizer
Misc Asc(string)
Chr(charcode)
Space(number)
String(number, character)
str1[3] Ord(Ch)
Chr(48)
Integer('A')
Insert(s, NewStr, index)
str1.charAt(int)
String(character, number)
str1.toCharArray()


Visual Basic 6.0

In VisualBasic, concatenation using "+" is not reliable. If one of the values is a number and the other is a string that represents a number, normal concatenation converts the number to a string and then concatenates them. However, VisualBasic may convert the string into a number, add the values, and then convert the sum into a string. There are 2 functions to convert numbers to strings. Notice that for positive numbers Many functions may end with an optional dollar sign.

Trim removes spaces, but leaves tabs.

For a list of relevant commands, see "Returning Strings from Functions" in the help.


Delphi 5.0

Delphi provides 2 types of strings - null-terminated (PChar) and Pascal (string). Use StrPas, StrPCopy, and StrPLCopy to convert between the 2 types.
String types can be mixed in assignments and expressions; the compiler automatically performs required conversions. But strings passed by reference to a function or procedure (as var and out parameters) must be of the appropriate type. From the Delphi 5 Help

Integer('A') casts the character A as an integer. (See Typecasts: Overview in the Delphi 5 help)

Access individual characters as an array - TempStr[i]

There are several ways to compare strings

Trim, TrimLeft, and TrimRight remove spaces, tabs, carriage returns, and every ASCII character with a value less than space. (The Delphi 5 help says that it removes "control characters".)

Format (a function which returns a string) calls FmtStr (a procedure which modifies a string).

There are many additional string functions not listed in the table above. Some work on null terminated strings, some on multi-byte character sets, and perhaps some other stuff. For instance, most functions in the table above (such as UpperCase) work on 7-bit ASCII characters. To convert 8-bit international characters, use functions which start with Ansi (such as AnsiUpperCase).

Note - I used Delphi 5 regularly for 7 years before learning about SameText ... simply because it was left out of the help files. I finally found it in the Delphi 6 help. Previously, I used


C++ Builder

The Java char type is 16 bits, C++ is 8 bits.

C++ Builder provides several additional string types and functions not defined in standard C++ (and thus, not in the table above). AnsiString is a class which is designed to function like the Delphi long string type.

The TMask VCL object performs case insensitive, wildcard string compares.


Java

The Java char type is 16 bits, C++ is 8 bits.

Java 1.1 supports 3 string compare functions

Use StringTokenizer to parse a string on one or more delimiters. The parse and ParseObject methods are part of the Format commands and don't perform the same function.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / Strings.htm