PrevNextUpHome SophiaFramework UNIVERSE 5.3

11.1. String Class

In the BREW environment, there are two kinds of character types: one is the char type that represents the single/multi byte character(or the ANSI character) and the other is the AECHAR type that represents the double byte character of the uint16 type.

In SophiaFramewok UNIVERSE, these two kinds of character types are defined as AChar and WChar using typedef respectively. And two kinds of the following string classes are available:

All the functions of these two string classes are the same except their string types.

The buffer which the SFXAnsiString / SFXWideString class internally contains are managed inside the class, and conversion between the AChar string and the WChar string are automatically performed. Therefore, you don't have to mind the pointer, the heap, type conversion etc., and can handle strings easily.

Table 11.1. String Classes

Kind of the Character BREW Character Type SophiaFramework Character Type SophiaFramework String Class
Single/Multi Byte Character(ANSI Character) char AChar SFXAnsiString
Double Byte Character AECHAR or uint16 WChar SFXWideString

The following three kinds of character processing classes are for converting the character or testing the kind of character and so on.

Table 11.2. Classes for Processing Strings

Class Description
SFXAscii Class for processing an ASCII character.
SFXShiftJIS Class for processing a Shift_JIS character.
SFXTextEncoding Class for converting a string from one encoding into another.

Example 11.1. Defining Strings

SFXAnsiString str1("http://");
SFXAnsiString str2("www.s-cradle.com");
SFXAnsiString str3(str1);
SFXAnsiString str4;

The above code is to define the str1, str2, str3, and str4 variables as the SFXAnsiString objects, and their initial values are as follows:

  1. str1: string of "http://"
  2. str2: string of "www.s-cradle.com".
  3. str3: string same with str1, i.e., "http://"
  4. str4: " (null string)

Example 11.2. Concatenating Strings

SFXAnsiString str1("http://");
SFXAnsiString str2("www.s-cradle.com");
SFXAnsiString str3;

str3 = str1 + str2;  // str3 = "http://www.s-cradle.com"

str3 += "/index.html"; // str3 = "/index.html"

Example 11.3. Inserting String (1)

SFXAnsiString str3("/index.html");
SFXAnsiString str4;

str4 = str3.Insert(23, "/sophiaframework");  // str4 = "/sophiaframework/index.html"

Example 11.4. Inserting String (2)

SFXAnsiString str1("/sophiaframework");
SFXAnsiString str3("/index.html");
SFXAnsiString str4;

// The SFXAnsiString instance can be specified in the argument of Insert().
str4 = str3.Insert(23, str1);
[Caution] Caution

The content of the str3 variable will not be changed after the above code is executed.

Example 11.5. Removing Substring

SFXAnsiString str4("/sophiaframework/index.html");

str4 = str4.Remove(23, 39);  // str4 = "/index.html"

Example 11.6. Clearing String

SFXAnsiString str4("/index.html");

str4.Clear();
// str4 will become "" (null string)

Example 11.7. Getting the Length of String

SFXAnsiString str4("/index.html");

SInt32 length = str4.GetLengthCString();  // length = 34

Example 11.8. Removing a Substring from the End

SFXAnsiString str4("/index.html");  // str4 = "http://www.s-cradle.com"

SInt32 length = str4.GetLengthCString();
str4 = str4.Remove(length - 11, length);

Example 11.9. Assigning String

SFXAnsiString str1;
SFXAnsiString str2("abc");
SFXAnsiString str3;

str1 = "http://www.s-cradle.com";  // str1 becomes "http://www.s-cradle.com"

str3 = str2;  // str3 becomes "abc"

Example 11.10. Accessing Character of String

SFXAnsiString str1("/index1.html");

AChar character = str1[29];  // character becomes '1' 

str1[29] = '2'; // end of str1 becomes "index2.html"

Example 11.11. Cutting Substring from String

SFXAnsiString str1("/index.html");
SFXAnsiString str2;

str2 = str1.Substring(7, 23);  // str2 becomes "www.s-cradle.com"

Example 11.12. Searching for String

SFXAnsiString str1("/index.html");

SInt32 i = str1.FirstIndexOf("//");   // i = 5
SInt32 j = str1.FirstIndexOf("~");    // j = -1 since "~" is not found
SInt32 k = str1.FirstIndexOf("/", 7); // searching start from 7th text k = 23
SInt32 m = str1.LastIndexOf("/");     // m = 23

Example 11.13. Editing URL

SFXAnsiString str1("/index.html");

SInt32 m = str1.LastIndexOf("/"); // m = 23

// str1 = "http://www.s-cradle.com"
str1.Remove(m, str1.GetLength()); // delete end

// str1 = "/file.xml"
str1 += "/file.xml";

Example 11.14. Replacing Substring

SFXAnsiString str3("x < y & y < z");

// replace all "<" with "&lt;"
str3 = str3.Replace("<", "&lt;");

// replace all "&" with "&amp;"
str3 = str3.Replace("&", "&amp;");

// the following code is the same as the above
// str3 = str3.Replace("<", "&lt;").Replace("&", "&amp;");

Example 11.15. Comparing String

if (str1 == str2) {	// if str1 = str2

}
if (str1 == "abc") {	// if str1 = "abc"

}
if (str1.Equals("abc", false)) {  // if str1 = "abc"(case ignored)

}
if (str1.StartsWith("http://")) {  // if str1 starts with "http://"

}

Example 11.16. Get the pointer to the string of C programming language

// function which has ACharPtr as an argument
Void func(ACharPtr arg);

func(str1.GetCString());

Example 11.17. Displaying String on BREW Output Window

TRACE("%s", str1.GetCString());

Example 11.18. Checking the Null String

// function with SFXAnsiString as an argument
Void func(SFXAnsiString str) {
    if (str.IsEmpty()) { // check whether str is the null string or not
        return;          // do nothing if it is the null string
    }
    // main processing of func
    ...
    ...
    ...
}

Example 11.19. Outputting String with Format (Converting Integer into String)

// str4 = "255.255.0.0:8080"
// Format is the same as SPRINTF
str4 = SFXAnsiString::Format("%d.%d.%d.%d:%d", 255, 255, 0, 0, 8080);

Example 11.20. Converting String into Integer

str1 = "10";

// value becomes 10
SInt32 value = str1.AsSInt32();

Example 11.21. Using with the BREW APIs

// get information on the file of which name is str1
SFBFileMgrSmp fileMgr = SFBFileMgr::NewInstance();
fileMgr->GetInfo(str1);

Example 11.22. Drawing String

// get SFXGraphics instance
SFXGraphicsPtr graphics = SFXGraphics::GetInstance();
SFXAnsiString str("Hello World!");

// draw string (drawing area is specified with 2nd argument)
graphics->DrawText(str, SFXRectangle(0, 0, 200, 30));

Figure 11.1. Execution Result

Execution Result

Example 11.23. Terminating

// finalization is not required