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
"5" + 5 => "55" or "10" "5" & 5 => "55"There are 2 functions to convert numbers to strings. Notice that for positive numbers
Str$(5) => " 5" ' Contains a leading space (for the sign) Format$(5) => "5" ' No leading spaceMany functions may end with an optional dollar sign.
Str produces a variant Str$ produces a string
Trim removes spaces, but leaves tabs.
For a list of relevant commands, see "Returning Strings from Functions" in the help.
Delphi 5.0
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
S1 = S2 - case sensitive, boolean CompareStr (S1, S2) - case sensitive, integer CompareText(S1, S2) - case insensitive, integer SameText(S1, S2) - case insensitive, boolean (in Delphi 5, but not in the help files, in D6 help) AnsiSameStr (S1, S2) - case sensitive, boolean AnsiSameText (S1, S2) - case insensitive, boolean AnsiSameCaption(S1, S2) - case insensitive, boolean, ignores accelerator characters AnsiCompareStr (S1, S2) - case sensitive, integer These work with null terminated strings AnsiStrComp (S1, S2) - case sensitive, integer AnsiStrIComp (S1, S2) - case insensitive, integer Add Masks to your Uses clause Var xx: TMask; - case insensitive, allows wildcards Begin xx := Tmask.Create('abc*xyz[3-5]'); // Supports * ? [ranges] if xx.Matches(S2) then xx.free
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
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
if CompareText(S1, 'target') = 0 then ...
C++ Builder
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
Java 1.1 supports 3 string compare functions
Returns | ||
---|---|---|
if (str1==str2) | ||
str1.equals(str2) | true or false | |
str1.compareTo(str2) | 0 or not 0 |
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.