String Objects Making Strings Easy to Use Creative

String Objects Making Strings Easy to Use Creative Commons License – Curt Hill

Introduction • C++ is an object-oriented language, which distinguishes it from its predecessor C and many other languages such as Pascal, COBOL, etc. • One of the advantages of this is the ability to extend the basic language with new objects • It turns out that ifstreams and ofstreams are both objects Creative Commons License – Curt Hill

Existing Strings • Depending on the system you may have several kinds of strings • C has an array of character is often called a C style string • The standard C++ libraries include a string type • CBuilder has a String object • Visual Studio also has a String object • C++Dev has a wx. String Creative Commons License – Curt Hill

C Style Strings • Anything in quotes is a C style string • There are many more limits on them than on String • We will see the declaration later • They cannot be directly assigned, compared or concatenated – Unlike Strings • There are good reasons (in C) for this, but it makes object strings easier to use Creative Commons License – Curt Hill

Windows and IDEs • CBuilder, Dev and Visual Studio are the same in this regard: – They both prefer a local string object for Windows components • They do not use either the C style string or the standard C++ library string (notice lower case s) • Instead they use their own String, which are objects Creative Commons License – Curt Hill

String Objects • What we now want to consider is a string object that will make our life easier • One of the other great things about objects is that the underlying implementation can be concealed from the user • In this case we do not know and do not care about the underlying implementation • In fact it will use arrays and classes, thus the use of it is quite simple so we do not care about internals Creative Commons License – Curt Hill

Common Features • There are several common features of these Strings – The include is provided in Windows programs – Allow assignment and subscripting • There are more differences – The declarations are slightly different – Most of the methods have different names • We will now focus on C++Builder String objects Creative Commons License – Curt Hill

Declaration • CBuilder: String s = “A string”; • In Windows program, no extra includes are needed • Most of the text properties of VCL components are of this type Creative Commons License – Curt Hill

Aside on Constructors • Objects have special methods called constructors • The goal of a constructor is to create (construct) an object – Initialize all the properties of the object • The name of these is always the same as the object name • The constructor may take parameters – The default constructor takes no parameters Creative Commons License – Curt Hill

Constructors • Since both String types are objects the constructor form may be used • CBuilder String s 0; String s 1(“A String”); String s 2(s 1); • If no assignment or constructor is used a zero-length string is created Creative Commons License – Curt Hill

Operators • The three operators that really make sense are allowed • Assignment • Comparison • Concatenation Creative Commons License – Curt Hill

Assignment • Once declared they may be assigned to with other items of String type or constants in double quotes • Works in expected ways: s 1 = “Another String”; s 2 = s 1; Creative Commons License – Curt Hill

Comparison operators • The standard ones are all present < > == … • The two strings are compared in an unusual way • Character by character until different • First unequal determines string comparison • Case is honored • Comparison is based on collating sequence of machine - in our case ASCII • If two are equal to length of shorter, then shorter is lesser Creative Commons License – Curt Hill

Concatenation • The + operator is concatenation – The thing to the left becomes the first so many characters the thing to the right becomes the last so many • The += has the obvious interpretation • Strings are inherently variable length, unlike most other things that we deal with – This turns out to be an implementation headache, but do we do not care • Example: • a 3 = a 2 + “ middle piece”; • a 3 += a 1; Creative Commons License – Curt Hill

Lengths • The length of each may be obtained • CBuilder it is a method: int len = s 1. Length(); – Notice the dot, not -> • The length includes any characters – Spaces, new lines, tabs Creative Commons License – Curt Hill

Subscripting • The process of subscripting or indexing is obtaining one item from an array • The symbols used to do this are the square brackets [ ] • We can obtain a single character from either String • Thus: char ch = s 1[4]; Creative Commons License – Curt Hill

Caveats • CBuilder: – The subscript starts at 1 and proceeds to Length() • The subscript may be a constant or variable • Providing a subscript that is out of range is an error Creative Commons License – Curt Hill

Counting Blanks • CBuilder: int count = 0; for(int i=1; i<=s 1. Length(); i++) if(s 1[i] == ‘ ‘) count++; Creative Commons License – Curt Hill

Insignificant Blanks • Leading and trailing blanks usually are of no value – Blanks between words are important • The Trim method removes leading and trailing blanks • C++Builder: s 2 = s 1. Trim(); Creative Commons License – Curt Hill

Case • The letters ‘A’ and ‘a’ are not equal, but we might want them to be • There is a method to convert all letters to a single case • C++Builder – Upper. Case and Lower. Case methods • These do not change the string, but return a changed string • The transformed string has only upper or lower case letters • Other characters are unchanged • No error if there are no upper/lower or letters Creative Commons License – Curt Hill at all

Sizes • There actually two distinct String objects: • Ansi. String uses an 8 bit code – The underlying character is a char • Unicode. String uses a 16 bit code – The underlying character is a wchar_t • All versions of C++Builder have a #define that makes the String one of these two – Earlier versions Ansi. String, current is Unicode String Creative Commons License – Curt Hill

Size Again • The underlying character of an Ansi. String is just a char – Constant character is in apostrophes • char ch = ‘C’; – Strings in double quotes • Ansi. String s = “Hello”; • Unicode strings are long – An L is prefixed on quotes and apostrophes – Wchar_t ch = L‘C’; – Unicode. String s = L“Hello”; Creative Commons License – Curt Hill

Other methods • String Sub. String(int start, int length) – Extracts a new String from the interior of the old • int Pos(String s) – Finds the position of the first s within the String – -1 if not found • int Last. Delimiter(String s) – Finds the position of the last one of these – -1 if not found Creative Commons License – Curt Hill

Last Thoughts • There are very many more methods on either object • Most of which we will not need in this class • There is a presentation on these in CSCI 161 Creative Commons License – Curt Hill
- Slides: 24