PrevNextUpHome BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1

11.1. String Class

In the BREW environment, there are two kinds of character types: the char type that represents single byte character or multi-byte character, and the AECHAR type that represents 2 byte character. SophiaFramewok defines these two kinds of types as AChar and WChar respectively.

And SophiaFramework provides two kinds of String classes: the SFXAnsiString class for the string of AChar characters and the SFXWideString class for the string of WChar characters.

Table 11.1. String Classes

Class Description
SFXAnsiString Class that represents a string of AChar type.
SFXWideString Class that represents the string of WChar type.

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. String Processing Classes

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

Example 11.1. Define the string

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

Define variables str1, str2, str3, and str4 as objects of the string class.

str1 indicates the string of "http://", and str2 indicates the string of "www.s-cradle.com".

str3 is the same string as str1 ("http://").

str4 is a null string.

Example 11.2. Concatenate the strings

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

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

// str3 = "http://www.s-cradle.com/index.html" str3 += "/index.html";

Example 11.3. Insert the string (1)

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

// str4 = "http://www.s-cradle.com/sophiaframework/index.html"
str4 = str3.Insert(23, "/sophiaframework");

Example 11.4. Insert the string (2)

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

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

The string of str3 is not converted.

Example 11.5. Delete the string partially

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

// str4 = "http://www.s-cradle.com/index.html"
str4 = str4.Remove(23, 39);

Example 11.6. Delete all the string

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

// str4 = " (null string)
str4.Clear();

Example 11.7. Get the length of string

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

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

  

Example 11.8. Delete the string from the end

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

// str4 = "http://www.s-cradle.com"
SInt32 length = str4.GetLengthCString();
str4 = str4.Remove(length - 11, length);

Example 11.9. Assign or copy the string

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

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

// str3 becomes "abc"
str3 = str2;

Example 11.10. Get and set the Character

SFXAnsiString str1("http://www.s-cradle.com/index1.html");

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

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

Example 11.11. Cut the string partially

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

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

Example 11.12. Search for the string

SFXAnsiString str1("http://www.s-cradle.com/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. Edit the URL

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

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

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

// str1 = "http://www.s-cradle.com/file.xml"
str1 += "/file.xml";

Example 11.14. Replace the string

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. Compare the 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. Display the string on the BREW Output window

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

Example 11.18. Check the Null string

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

Example 11.19. Output the string with format ( convert the integer into the 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. Convert the string into the 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. Draw the 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. Terminate

// termination processing is not required