C++ Windows Forms L06 - Utlitity and Strings

119
Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker C++.NET Windows Forms Course L06-Utility

description

C++ Windows Forms L06 - Utlitity and Strings of C++ Windows Forms Light Course

Transcript of C++ Windows Forms L06 - Utlitity and Strings

Page 1: C++ Windows Forms L06 - Utlitity and Strings

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

L06-Utility

Page 2: C++ Windows Forms L06 - Utlitity and Strings

How to work with OOP in .NET

Page 3: C++ Windows Forms L06 - Utlitity and Strings

How to work with OOP in .NET

• Structure of Form class

• Adding variables – Public– Private

• Adding member functions– Procedural

• Adding classes by “ref”

• Passing parameters – % , ^

Page 4: C++ Windows Forms L06 - Utlitity and Strings

.NET

Page 5: C++ Windows Forms L06 - Utlitity and Strings

.NET

Page 6: C++ Windows Forms L06 - Utlitity and Strings

Debugging

Page 7: C++ Windows Forms L06 - Utlitity and Strings

Debugging – Break Point

Page 8: C++ Windows Forms L06 - Utlitity and Strings

Debugging

Page 9: C++ Windows Forms L06 - Utlitity and Strings

Debugging

Page 10: C++ Windows Forms L06 - Utlitity and Strings

Debugging

Page 11: C++ Windows Forms L06 - Utlitity and Strings

Debugging

Page 12: C++ Windows Forms L06 - Utlitity and Strings

references \ declarations \ definitions

Page 13: C++ Windows Forms L06 - Utlitity and Strings
Page 14: C++ Windows Forms L06 - Utlitity and Strings

Searching in msdn

Page 15: C++ Windows Forms L06 - Utlitity and Strings
Page 16: C++ Windows Forms L06 - Utlitity and Strings

DateTime class

Page 17: C++ Windows Forms L06 - Utlitity and Strings

DateTime

Page 18: C++ Windows Forms L06 - Utlitity and Strings

DateTime

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime D;

D.Now;

textBox1->Text= D.Day.ToString();

};

1

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime D;

textBox1->Text= D.Day.ToString();

};

1

Page 19: C++ Windows Forms L06 - Utlitity and Strings

DateTime

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime D;

D.Now;

textBox1->Text= D.Now.Day.ToString();

};

9

Page 20: C++ Windows Forms L06 - Utlitity and Strings

DateTime Constructor

Page 21: C++ Windows Forms L06 - Utlitity and Strings

DateTime Methods

Page 22: C++ Windows Forms L06 - Utlitity and Strings

DateTime

Page 23: C++ Windows Forms L06 - Utlitity and Strings

DateTime

Page 24: C++ Windows Forms L06 - Utlitity and Strings

DateTime

Page 25: C++ Windows Forms L06 - Utlitity and Strings

DateTime

Page 26: C++ Windows Forms L06 - Utlitity and Strings

DateTime

• Considering today is 22/10/2010, the output will be

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)

{

// Calculate what day of the week is 36 days from this instant

DateTime ^today= DateTime::Now;

TimeSpan duration(36, 0, 0, 0);

DateTime ^answer= today->Add(duration);

textBox1->Text= answer->ToString();

}

11/27/2010 1:49:12 AM

Page 27: C++ Windows Forms L06 - Utlitity and Strings

DateTime

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime ^Day= gcnew DateTime(2010, 10, 6);

textBox1->Text= Day->ToString();

}

10/6/2010 12:00:00 AM

DateTime date1= gcnew DateTime::Now;

DateTime date2= gcnew DateTime::UtcNow;

DateTime date3= gcnew DateTime::Today;

Utc: Coordinated Universal Time

Page 28: C++ Windows Forms L06 - Utlitity and Strings

DateTime

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime ^Day= gcnew DateTime(2010, 10, 6);

textBox1->Text= Day->ToLongTimeString();

}

12:00:00 AM

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime ^Day= gcnew DateTime(2010, 10, 6);

textBox1->Text= Day->ToLongDateString();

}

Wednesday, October 06, 2010

Page 29: C++ Windows Forms L06 - Utlitity and Strings

DateTimeprivate: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime ^Day= gcnew DateTime(2010, 10, 6);

textBox1->Text= Day->ToShortDateString();

}

10/6/2010

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime ^Day= gcnew DateTime(2010, 10, 6);

textBox1->Text= Day->ToShortTimeString();

}

12:00 AM

Page 30: C++ Windows Forms L06 - Utlitity and Strings

DateTimeprivate: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime ^Day= gcnew DateTime(2010, 10, 6);

textBox1->Text= Day >ToString();

}

10/6/2010 12:00:00 AM

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

DateTime ^Day= gcnew DateTime(2010, 10, 6);

textBox1->Text= Day->ToLocalTime().ToString();

}

10/6/2010 3:00:00 AM

Page 31: C++ Windows Forms L06 - Utlitity and Strings

int window= 10;int freq= 60 * 60 * 2; // 2 hours;

DateTime d1= DateTime.Now;

DateTime d2= d1->AddSeconds(2 * window);DateTime d3= d1->AddSeconds(-2 * window);DateTime d4= d1->AddSeconds(window / 2);DateTime d5= d1->AddSeconds(-window / 2);

DateTime d6= (d1.AddHours(2))->AddSeconds(2 * window);DateTime d7= (d1.AddHours(2))->AddSeconds(-2 * window);DateTime d8= (d1.AddHours(2))->AddSeconds(window / 2);DateTime d9= (d1.AddHours(2))->AddSeconds(-window / 2);

Console.WriteLine("d1 ({0}) ~= d1 ({1}): {2}“, d1, d1, RoughlyEquals(d1, d1, window, freq));Console.WriteLine("d1 ({0}) ~= d2 ({1}): {2}", d1, d2, RoughlyEquals(d1, d2, window, freq));Console.WriteLine("d1 ({0}) ~= d3 ({1}): {2}", d1, d3, RoughlyEquals(d1, d3, window, freq));Console.WriteLine("d1 ({0}) ~= d4 ({1}): {2}", d1, d4, RoughlyEquals(d1, d4, window, freq));Console.WriteLine("d1 ({0}) ~= d5 ({1}): {2}", d1, d5, RoughlyEquals(d1, d5, window, freq));

Console.WriteLine("d1 ({0}) ~= d6 ({1}): {2}", d1, d6, RoughlyEquals(d1, d6, window, freq));Console.WriteLine("d1 ({0}) ~= d7 ({1}): {2}", d1, d7, RoughlyEquals(d1, d7, window, freq));Console.WriteLine("d1 ({0}) ~= d8 ({1}): {2}", d1, d8, RoughlyEquals(d1, d8, window, freq));Console.WriteLine("d1 ({0}) ~= d9 ({1}): {2}", d1, d9, RoughlyEquals(d1, d9, window, freq));

DateTime

Page 32: C++ Windows Forms L06 - Utlitity and Strings

Properties

Page 33: C++ Windows Forms L06 - Utlitity and Strings

Properties

• A compromise between a function and a variable! • See, the following, how awesome!

private:

int MyInt;

public:

// property block

property int intProperty

{

int get()

{

return MyInt;

}

void set(int value)

{

MyInt= value;

}

}

Page 34: C++ Windows Forms L06 - Utlitity and Strings

Properties

The property keyword introduces the declaration of a property and can appear in a class, interface, or value type. A property can have a getter function (read only), a setter function (write only), or both (read-write).

public:

int MyInt;

// property block

property int intProperty

{

int get()

{

return MyInt;

}

void set(int value)

{

MyInt= value;

}

}

Page 35: C++ Windows Forms L06 - Utlitity and Strings

Properties

• So, how to use it? Like the following …property int intProperty

{

int get()

{

return MyInt;

}

void set(int value)

{

MyInt= value;

}

}

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

textBox1->Text= intProperty.ToString();

}

Page 36: C++ Windows Forms L06 - Utlitity and Strings

Properties

Page 37: C++ Windows Forms L06 - Utlitity and Strings

Properties

• So, how to use it? Like the following …property int intProperty

{

int get()

{

return MyInt;

}

void set(int value)

{

MyInt= value;

}

}

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

intProperty= 5;

textBox1->Text= intProperty.ToString();

}

Page 38: C++ Windows Forms L06 - Utlitity and Strings

Properties

Page 39: C++ Windows Forms L06 - Utlitity and Strings

Properties

int MyInt;

// property block

property int intProperty

{

int get()

{

return MyInt;

}

void set(int value)

{

MyInt= value;

}

}

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

intProperty= 5;

textBox1->Text= intProperty.ToString();

intProperty= 14;

textBox1->Text += " " + intProperty.ToString();

}

Page 40: C++ Windows Forms L06 - Utlitity and Strings

Properties

Page 41: C++ Windows Forms L06 - Utlitity and Strings

Propertiesint MyInt;

// property block

property int intProperty

{

int get()

{

MessageBox::Show("You are lucky to know the property

before others:P:D");

return MyInt;

}

void set(int value)

{

MyInt= value;

}

}

#pragma endregion

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

intProperty= 5;

textBox1->Text= intProperty.ToString();

}

Page 42: C++ Windows Forms L06 - Utlitity and Strings

Properties

Page 43: C++ Windows Forms L06 - Utlitity and Strings

Propertiesint MyInt;

// property block

property int intProperty

{

int get()

{

return MyInt;

MessageBox::Show("You are lucky to know the property

before others:P:D");

}

void set(int value)

{

MyInt= value;

}

}

#pragma endregion

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

intProperty= 5;

textBox1->Text= intProperty.ToString();

}

Note where it is!

Page 44: C++ Windows Forms L06 - Utlitity and Strings

Properties

No message box

Page 45: C++ Windows Forms L06 - Utlitity and Strings

string\int Conversion

Page 46: C++ Windows Forms L06 - Utlitity and Strings

string\int Conversion

• NewLine:– Environment::NewLine;

• Converting from string to int:– Int32::Parse(textBox1->Text);

– Int32::TryParse(textBox1->Text);

Page 47: C++ Windows Forms L06 - Utlitity and Strings

string\int Conversion

• Converting from int to string:– Exp #1:

• int x;• textBox1->Text= x.ToString();

– Exp #2:• int x̂; • textBox1->Text= x->ToString();

– Exp #3:• int x̂;• String ^S= x->ToString();

Page 48: C++ Windows Forms L06 - Utlitity and Strings

string\int Conversionprivate: System::Void button1_Click_1(System::Object^ sender,

System::EventArgs^ e)

{

int i= 3;

i= int::Parse(textBox1->Text);

}

What happens if we have “435345” in textBox1?

What happens if we have “wewe” in textBox1?

What happens if we have “213s” in textBox1?

What happens if we have “” in textBox1?

Page 49: C++ Windows Forms L06 - Utlitity and Strings

private: System::Void button1_Click_1(System::Object^ sender,

System::EventArgs^ e)

{

int i= 3;

i= int::TryParse(textBox1->Text, i ;

}

What happens if we have “435345” in textBox1?

What happens if we have “wewe” in textBox1?

What happens if we have “213s” in textBox1?

What happens if we have “” in textBox1?

string\int Conversion

Page 50: C++ Windows Forms L06 - Utlitity and Strings

Files! The Concept

Page 51: C++ Windows Forms L06 - Utlitity and Strings

Working with Files

• using!

using namespace System::IO;

Page 52: C++ Windows Forms L06 - Utlitity and Strings

void FileManipulate::PrintTextFile (){

String ^FilePath = "D:\\HashFile.Txt" ;

FileStream ^MyFile = gcnew FileStream(FilePath, FileMode::Open);

StreamReader ^SR = gcnew StreamReader(MyFile);String ^str="" ;

// Extracting words while(SR->Peek() > -1){

str = SR->ReadLine();MyForm->textBox1->Text += str + Environment::NewLine ;

}

MyFile->Close();}

using namespace System::IO;

Page 53: C++ Windows Forms L06 - Utlitity and Strings

void FileManipulate::PrintTextFile (){

String ^FilePath = "D:\\HashFile.Txt" ;

FileStream ^MyFile = gcnew FileStream ;

StreamReader ^SR = gcnew StreamReader(MyFile);String ^str="" ;

// Extracting words while(SR->Peek() > -1){

str = SR->ReadLine();MyForm->textBox1->Text += str + Environment::NewLine ;

}

MyFile->Close();}

Compiler errorFileStream ^MyFile = gcnew FileStream(FilePath, FileMode::Open);

using namespace System::IO;

Page 54: C++ Windows Forms L06 - Utlitity and Strings

Working with Filesprivate: System::Void Form1_Load(System::Object^ sender,

System::EventArgs^ e)

{

SaveFileDialog ^Dialog = gcnew SaveFileDialog();

FileStream ^MyFC ;

String ^FilePath = "" ;

if (Dialog->ShowDialog() ==

System::Windows::Forms::DialogResult::OK)

{

FilePath = Dialog->FileName ;

}

Page 55: C++ Windows Forms L06 - Utlitity and Strings

Working with FilesMyFC = File::Create(FilePath) ;

MyFC->Close();

MyFC = File::Open(FilePath ,FileMode::Append );

StreamWriter ^SW = gcnew StreamWriter(MyFC);

SW->WriteLine("I AM ZGTR");

SW->WriteLine("I AM ALWAYS ZGTR");

SW->Close();

MyFC->Close();

MyFC = File::Open(FilePath ,FileMode::Open );

StreamReader ^SR = gcnew StreamReader(MyFC);

String ^str="" ;

// Extracting words

while(SR->Peek() > -1)

{

str = SR->ReadLine();

this->textBox1->Text += str + Environment::NewLine ;

}

MyFC->Close();

}

Page 56: C++ Windows Forms L06 - Utlitity and Strings

Working with FilesMyFC = File::Create(FilePath) ;

MyFC->Close();

MyFC = File::Open(FilePath ,FileMode::Append );

StreamWriter ^SW = gcnew StreamWriter(MyFC);

while (textBox1->Text!= “000”)

{ SW-> WriteLine(textBox2->Text) ;}

SW->Close();

MyFC->Close();

MyFC = File::Open(FilePath ,FileMode::Open );

StreamReader ^SR = gcnew StreamReader(MyFC);

String ^str="" ;

// Extracting words

while(SR->Peek() > -1)

{

str = SR->ReadLine();

this->textBox1->Text += str + Environment::NewLine ;

}

MyFC->Close();

}

Page 57: C++ Windows Forms L06 - Utlitity and Strings
Page 58: C++ Windows Forms L06 - Utlitity and Strings

Working with Files

Page 59: C++ Windows Forms L06 - Utlitity and Strings

Working with FilesLive test

Page 60: C++ Windows Forms L06 - Utlitity and Strings

Use the console to track\debug values!Do not use messagebox for tracking!

Page 61: C++ Windows Forms L06 - Utlitity and Strings

Change between Console and Windows Form• In the project properties for all configurations (Project |

Properties, choose Configuration 'All Configurations', locate Config Properties -> Linker -> System), change the SubSystemfrom Console to Windows.

Page 62: C++ Windows Forms L06 - Utlitity and Strings

Change between Console and Windows Form

Page 63: C++ Windows Forms L06 - Utlitity and Strings

Change between Console and Windows Form

Page 64: C++ Windows Forms L06 - Utlitity and Strings

Change between Console and Windows Form

Page 65: C++ Windows Forms L06 - Utlitity and Strings

Project output type example

• Let’s have the following design

Page 66: C++ Windows Forms L06 - Utlitity and Strings

Project output type example

• And the following code-behind

Page 67: C++ Windows Forms L06 - Utlitity and Strings

Project output type example

• After setting the project output to “console”, Run

Page 68: C++ Windows Forms L06 - Utlitity and Strings

Project output type example

• Now, press the button multiple times and there you go!

Page 69: C++ Windows Forms L06 - Utlitity and Strings

Creating You Own Windows Forms Control (C++)

http://msdn.microsoft.com/en-us/library/vstudio/ms235628(v=vs.100).aspx

Page 70: C++ Windows Forms L06 - Utlitity and Strings

Peak on Exception Handlingtry, catch, throw and finally

Page 71: C++ Windows Forms L06 - Utlitity and Strings

private: System::Void panel1_MouseClick(System::Object^ sender,

System::Windows::Forms::MouseEventArgs^ e)

{

try

{

if (textBox1->text == “Hi”)

{

MessageBox::Show(“It’s time for Exceptions! ");

}

}

catch (System::FormatException ^e)

{

MessageBox::Show("You Should Enter a String in textBox first ");

}

}

Peak on Exception Handling

• Test it live!

Page 72: C++ Windows Forms L06 - Utlitity and Strings

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

Strings

Page 73: C++ Windows Forms L06 - Utlitity and Strings

Welcome!

Page 74: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

Page 75: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action-> and ::

Page 76: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• Let’s have the following form …

Page 77: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• What happens?

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

int i = 3;

i++;

String ^Str = textBox1->Text;

String ^TempStr =String::Format("{0}{1}{2}" , Str , "What?" ,

i.ToString());

textBox2->Text += TempStr;

}

Page 78: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

Page 79: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

int i = 3;

i++;

String ^Str = textBox1->Text;

String ^TempStr = String::Format("{0}{1} Go! {2}" , Str ,

" What? " , i.ToString());

textBox2->Text += TempStr;

}

Page 80: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

Page 81: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• Is it okay?

private: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

int i = 3;

i++;

String ^Str = textBox1->Text;

String ^TempStr = String::Format("{0}{1} Go! {2}" , Str ,

" What? " , i);

textBox2->Text += TempStr;

}

Page 82: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

Page 83: C++ Windows Forms L06 - Utlitity and Strings

String Operations by Category

Page 84: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• Comparing Strings (int):– Compare :

• returns an integer that indicates the relationship of one string to a second string in the sort order.

Page 85: C++ Windows Forms L06 - Utlitity and Strings
Page 86: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

Value Condition

Less than zero strA is less than strB.

Zero strA equals strB.

Greater than zero strA is greater than strB.

Page 87: C++ Windows Forms L06 - Utlitity and Strings

Strings in Actionprivate: System::Void button1_Click(System::Object^

sender, System::EventArgs^ e)

{

String ^str1 = "I Wanna go!";

String ^str2 = "I decided not to go!";

int i = String::Compare(str1,str2);

textBox1->Text = i.ToString();

}

Page 88: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• Testing Strings for Equality (bool):– You call the Equals method to determine whether two strings are

equal.

Page 89: C++ Windows Forms L06 - Utlitity and Strings
Page 90: C++ Windows Forms L06 - Utlitity and Strings
Page 91: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

String ^str1 = "I Wanna go!";

String ^str2 = "I decided not to go!";

bool b= String::Equals(str1,str2);

textBox1->Text = b.ToString();

}

Page 92: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

String ^str1 = "I Wanna go!";

String ^str2 = "I Wanna go!";

bool b = String::Equals(str1,str2);

textBox1->Text = b.ToString();

}

true

Page 93: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

String ^str1 = "I Wanna go!";

String ^str2 = "I Wanna go! ";

bool b = String::Equals(str1,str2);

textBox1->Text = b.ToString();

}

false

Page 94: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• Finding Characters in a String :– The String class includes two kinds of search methods:

• Methods that return a “ bool” value to indicate whether a particular substring is present in a string instance. These include the Contains, EndsWith, and StartsWith methods.

• Methods that indicate the starting position of a substring in a string instance “int”. These include the IndexOf,IndexOfAny, LastIndexOf, and LastIndexOfAny methods.

Page 95: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• String::Contains Method (bool)– Returns a value indicating whether the specified String object occurs

within this string.

Page 96: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

{String^ s1 = "The brown fox jumps over the lazy dog";String^ s2 = "fox";bool b;b = s1->Contains( s2 );Console::WriteLine( "Is the string, s2, in the string, s1?: {0}", b );

}

Is string , s2, in string, s1? True

Page 97: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• String::IndexOf Method (int)– Reports the index of the first occurrence of one or more characters,

or the first occurrence of a string, within this string.

– This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

Page 98: C++ Windows Forms L06 - Utlitity and Strings

Name Description

IndexOf(Char) Reports the index of the first occurrence of the specified Unicode character in this string.

IndexOf(String) Reports the index of the first occurrence of the specified string in this instance.

IndexOf(Char, Int32) Reports the index of the first occurrence of the specified Unicode character in this string. The search starts at a specified character position.

IndexOf(String, Int32) Reports the index of the first occurrence of the specified string in this instance. The search starts at a specified character position.

IndexOf(String, StringComparison)

Reports the index of the first occurrence of the specified string in the current Stringobject. A parameter specifies the type of search to use for the specified string.

IndexOf(Char, Int32, Int32)

Reports the index of the first occurrence of the specified character in this instance. The search starts at a specified character position and examines a specified number of character positions.

IndexOf(String, Int32, Int32)

Reports the index of the first occurrence of the specified string in this instance. The search starts at a specified character position and examines a specified number of character positions.

IndexOf(String, Int32, StringComparison)

Reports the index of the first occurrence of the specified string in the current Stringobject. Parameters specify the starting search position in the current string and the type of search to use for the specified string.

IndexOf(String, Int32, Int32, StringComparison)

Reports the index of the first occurrence of the specified string in the current Stringobject. Parameters specify the starting search position in the current string, the number of characters in the current string to search, and the type of search to use for the specified string.

Page 99: C++ Windows Forms L06 - Utlitity and Strings

int main(array<System::String ^> ^args)

{

Console::WriteLine(L"Hello World");

// Create a Unicode String with 5 Greek Alpha characters

String^ szGreekAlpha = gcnew String( L'\x0319',5 );

// Create a Unicode String with a Greek Omega character

wchar_t charArray5[3] = {L'\x03A9',L'\x03A9',L'\x03A9'};

String^ szGreekOmega = gcnew String( charArray5,2,1 );

String^ szGreekLetters = String::Concat( szGreekOmega, szGreekAlpha, szGreekOmega-

>Clone() );

// Examine the result

Console::WriteLine( szGreekLetters );

// The first index of Alpha

int ialpha = szGreekLetters->IndexOf( L'\x0319' );

// The last index of Omega

int iomega = szGreekLetters->LastIndexOf( L'\x03A9' );

Console::WriteLine( String::Concat( "The Greek letter Alpha first appears at

index ", Convert::ToString( ialpha ) ) );

Console::WriteLine( String::Concat( " and Omega last appears at index ",

Convert::ToString( iomega ), " in this String." ) );

return 0;

}

Page 100: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

Hello World

Ω?????Ω

The Greek letter Alpha first appears at index 1

and Omega last appears at index 6 in this String.

Press any key to continue . . .

Page 101: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action–Trim() method

• String::Trim Method– Removes all leading and trailing white-space characters from the

current String object.

Page 102: C++ Windows Forms L06 - Utlitity and Strings

int main(){

String^ animal1 = "fox";String^ animal2 = "dog";String^ strTarget = String::Format( "The {0} jumped over the {1}.", animal1, animal2 );

Console::WriteLine( "The original string is:{0}{1}{0}", Environment::NewLine, strTarget );

Console::Write( "Enter an adjective (or group of adjectives) to describe the {0}: ==> ", animal1 );

String^ adj1 = Console::ReadLine();

Console::Write( "Enter an adjective (or group of adjectives) to describe the {0}: ==> ", animal2 );

String^ adj2 = Console::ReadLine();adj1 = String::Concat( adj1->Trim(), " " );adj2 = String::Concat( adj2->Trim(), " " );strTarget = strTarget->Insert( strTarget->IndexOf( animal1 ), adj1 );strTarget = strTarget->Insert( strTarget->IndexOf( animal2 ), adj2 );

Console::WriteLine( " {0}The final string is: {0} {1}", Environment::NewLine, strTarget );}

Page 103: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

The original string is:The fox jumped over the dog.

Enter an adjective (or group of adjectives) to describe the fox: ==> bold

Enter an adjective (or group of adjectives) to describe the dog: ==> lazy

The final string is:The bold fox jumped over the lazy dog.

Page 104: C++ Windows Forms L06 - Utlitity and Strings

Strings in Actionprivate: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

String ^str1 = "I Wanna go!";

String ^str2 = "I DeciDEd not to go!";

str2 = str2->ToLower();

textBox1->Text = str2;

}

Page 105: C++ Windows Forms L06 - Utlitity and Strings

Strings in Actionprivate: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

String ^str1 = "I Wanna go!";

String ^str2 = "I decided not to go! ";

str2 = (str2->ToLower())->Trim() ;

textBox1->Text = str2+str1;

}

Page 106: C++ Windows Forms L06 - Utlitity and Strings

Strings in Actionprivate: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

String ^str1 = "I Wanna go!";

String ^str2 = “ I DeCided not to go! ";

str2 = (str2->ToLower())->Trim() ;

textBox1->Text = str2;

}

Page 107: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• String::Copy Method (String to String)– Creates a new instance of String with the same value as a specified String.

// Sample for String::Copy()using namespace System;int main(){

String^ str1 = "abc";String^ str2 = "xyz";Console::WriteLine( "1) str1 = '{0}'", str1 );Console::WriteLine( "2) str2 = '{0}'", str2 );Console::WriteLine( "Copy..." );str2 = String::Copy( str1 );Console::WriteLine( "3) str1 = '{0}'", str1 );Console::WriteLine( "4) str2 = '{0}'", str2 );

}

1) str1 = 'abc'2) str2 = 'xyz'Copy...3) str1 = 'abc'4) str2 = 'abc'

Page 108: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• String::CopyTo Method (String)– Copies a specified number of characters from a specified position in

this instance to a specified position in an array of Unicode characters.

Page 109: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• Parameters– sourceIndexType: System::Int32

The index of the first character in this instance to copy.

– destinationType: array<System::Char>An array of Unicode characters to which characters in this instance are copied.

– destinationIndexType: System::Int32The index in destination at which the copy operation begins.

– countType: System::Int32The number of characters in this instance to copy to destination.

Page 110: C++ Windows Forms L06 - Utlitity and Strings

using namespace System;int main(){

// Embed an array of characters in a stringString^ strSource = "changed";array <Char> ^destination = {'T','h','e',' ','i','n','i','t','i','a','l','

','a','r','r','a','y'};

// Print the char arrayConsole::WriteLine( destination );

// Embed the source string in the destination stringstrSource->CopyTo( 0, destination, 4, strSource->Length );

// Print the resulting arrayConsole::WriteLine( destination );strSource = "A different string";

// Embed only a section of the source string in the destinationstrSource->CopyTo( 2, destination, 3, 9 );

// Print the resulting arrayConsole::WriteLine( destination );

}

Page 111: C++ Windows Forms L06 - Utlitity and Strings
Page 112: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

The initial array

The changed array

Thedifferentarray

Press any key to continue . . .

Page 113: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

• String::Replace Method (String , String)– Returns a new string in which all occurrences of a specified string in

the current instance are replaced with another specified string.

Page 114: C++ Windows Forms L06 - Utlitity and Strings

Strings in Action

using namespace System;int main(){

String^ errString = "This docment uses 3 other docments to docment the docmentation";

Console::WriteLine( "The original string is:\n'{0}'\n", errString );

// Correct the spelling of S"document".String^ correctString = errString->Replace( "docment", "document" );Console::WriteLine( "After correcting the string, the result is:\n'{0}'",

correctString );}

This code example produces the following output:The original string is:'This docment uses 3 other docments to docment the docmentation'

After correcting the string, the result is:'This document uses 3 other documents to document the documentation'

Page 115: C++ Windows Forms L06 - Utlitity and Strings

SubString()

Page 116: C++ Windows Forms L06 - Utlitity and Strings
Page 117: C++ Windows Forms L06 - Utlitity and Strings

SubString()private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

String ^str1 = "I Wanna go!";

String ^str2 = "I decided not to go! ";

String ^TempStr = "";

TempStr = str2->Substring(3);

textBox1->Text = TempStr;

}

Page 118: C++ Windows Forms L06 - Utlitity and Strings

SubString()private: System::Void button1_Click(System::Object^ sender,

System::EventArgs^ e)

{

String ^str1 = "I Wanna go!";

String ^str2 = "I decided not to go! ";

String ^TempStr = "";

TempStr = str2->Substring(3,3);

textBox1->Text = TempStr;

}

Page 119: C++ Windows Forms L06 - Utlitity and Strings

That’s it for today!