Main Menu


Sponsored Links

  


  
  
Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

Operators for Strings

As I've said before WCM C++ has some extensions for more convenient work with strings. In pure C++ strings are just arrays of characters. In WCM C++ advanced users can also work with arrays of characters but they are converted to strings immediately after most of the operations. For example you can use operator + to concatenate (or merge) two strings together. Please keep in mind that the example below is impossible in pure C++:

void Scene1 ()

{

string sFirstName = "Bill";

string sLastName = "Gates";


Text Name ( "His name is: " + sFirstName + " " + sLastName );

Name.SetVisible ( true );


// we do not necessary need to have the last statement

// here, because if the cartoon length is zero it is

// automatically extended to one second

SetTime ( 1.0 );

}

It is also legal in WCM C++ to use operator + between strings and integers, doubles, and characters. But please keep in mind that while these operations are also enabled in classic C++, only in WCM C++ you can use them to concatenate strings. In classic C++ they mean different thing. Here is an example of how you can concatenate string with other values and variables:

void Scene1 ()

{

int iHour = 11;

int iMinute = 59;


string sTime = iHour + ":" + iMinute + ':' + 2.5;


Text Time ( sTime );

Time.SetVisible ( true );

SetTime ( 1.0 );

}

Please keep in mind that the order of operations does matter. In our example we first concatenate iHour and ":" . As a result we receive a string and then concatenate it with iMinute and so on. But if we use it like this:

string sTime = iHour + ':' + iMinute + ":" + 2.5;

then the first operation happens between iHour and ':' . And ':' is not the same thing as ":" . This is a character. As a result of this operation we will receive another character as I mentioned in previous section of this book. Operator + can be used in WCM C++ for concatenation only when at least one operand is a strings. For example this code:

string sNumbers = 1 + 2 + "3";

will assign "33 " to sNumbers , because 1 + 2 is an operation between integers and the result of this operation is 3 . But the code below:

string sNumbers = 1 + ( 2 + "3" );

will assign "123" to sNumbers , because there is at least one string operand in every operation.


Contents Previous Next
  
News

New Tales Animator Video by Alan Sturgess

Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer

There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated

Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here!

Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

read more news...


Poll