Visual C++ .NET/Data Type/String
Содержание
- 1 Compare strings for equality with ==
- 2 Compare strings for equality with String::Equals
- 3 Compare strings for equality with String::ReferenceEquals
- 4 Compare two strings with String::Compare
- 5 Convert back to a String using the String constructor that takes a Unicode character array.
- 6 Convert String to char array
- 7 Convert String to char pointer
- 8 Convert text read from stdin to uppercase and write to stdout
- 9 Create a copy, then concatenate new text
- 10 Create a String
- 11 Create some strings
- 12 Insert into a string
- 13 Output a string
- 14 Remove text from strings
- 15 Replace stuff in a concatenated string
- 16 Reverse a string
- 17 String Literals
- 18 String operator plus
- 19 Use static method String::Compare to compare two strings
- 20 Using string for each loop to output chars
Compare strings for equality with ==
#include "stdafx.h"
using namespace System;
int main()
{
String^ str1 = "1";
String^ str2 = "1";
String^ str3 = str1;
if (str1 == str2)
{
Console::WriteLine(" str1 == str2" );
}
if (str1 == str3)
{
Console::WriteLine(" str1 == str3" );
}
if (str1->Equals(str2))
{
Console::WriteLine(" str1 Equals str2" );
}
if (str1->Equals(str3))
{
Console::WriteLine(" str1 Equals str3");
}
if (String::ReferenceEquals(str1, str2))
{
Console::WriteLine(" str1 ReferenceEquals str2");
}
if (String::ReferenceEquals(str1, str3))
{
Console::WriteLine(" str1 ReferenceEquals str3");
}
}
Compare strings for equality with String::Equals
#include "stdafx.h"
using namespace System;
int main()
{
String^ str1 = "1";
String^ str2 = "1";
String^ str3 = str1;
if (str1 == str2)
{
Console::WriteLine(" str1 == str2" );
}
if (str1 == str3)
{
Console::WriteLine(" str1 == str3" );
}
if (str1->Equals(str2))
{
Console::WriteLine(" str1 Equals str2" );
}
if (str1->Equals(str3))
{
Console::WriteLine(" str1 Equals str3");
}
if (String::ReferenceEquals(str1, str2))
{
Console::WriteLine(" str1 ReferenceEquals str2");
}
if (String::ReferenceEquals(str1, str3))
{
Console::WriteLine(" str1 ReferenceEquals str3");
}
}
Compare strings for equality with String::ReferenceEquals
#include "stdafx.h"
using namespace System;
int main()
{
String^ str1 = "1";
String^ str2 = "1";
String^ str3 = str1;
if (str1 == str2)
{
Console::WriteLine(" str1 == str2" );
}
if (str1 == str3)
{
Console::WriteLine(" str1 == str3" );
}
if (str1->Equals(str2))
{
Console::WriteLine(" str1 Equals str2" );
}
if (str1->Equals(str3))
{
Console::WriteLine(" str1 Equals str3");
}
if (String::ReferenceEquals(str1, str2))
{
Console::WriteLine(" str1 ReferenceEquals str2");
}
if (String::ReferenceEquals(str1, str3))
{
Console::WriteLine(" str1 ReferenceEquals str3");
}
}
Compare two strings with String::Compare
#include "stdafx.h"
using namespace System;
int main()
{
String^ str1 = "cat";
String^ str2 = "cab";
if (str1->CompareTo( str2 ) < 0)
{
Console::WriteLine(str1 + " is less than " + str2);
}
// For variety, use the static method.
else if ( String::Compare(str1, str2) > 0 )
{
Console::WriteLine("{0} is less than {1}", str2, str1);
}
else if ( str1->CompareTo( str2 ) == 0)
{
Console::WriteLine("The strings are both equal, with value {0}.", str1);
}
}
Convert back to a String using the String constructor that takes a Unicode character array.
#include "stdafx.h"
using namespace System;
int main()
{
String^ str = "A quick sly fox jumped over the lazy brown dog.";
array<Char>^ character_array = str->ToCharArray();
Console::WriteLine( str);
for (int i = 0; i < character_array->Length; i++)
{
if ( character_array[i] >= L"a" && character_array[i] <= "z")
{
character_array[i] -= (L"a" - L"A");
}
}
str = gcnew String(character_array);
Console::WriteLine( str);
}
Convert String to char array
#include "stdafx.h"
using namespace System;
int main()
{
String^ str = "A quick sly fox jumped over the lazy brown dog.";
array<Char>^ character_array = str->ToCharArray();
Console::WriteLine( str);
for (int i = 0; i < character_array->Length; i++)
{
if ( character_array[i] >= L"a" && character_array[i] <= "z")
{
character_array[i] -= (L"a" - L"A");
}
}
str = gcnew String(character_array);
Console::WriteLine( str);
}
Convert String to char pointer
#include "stdafx.h"
#include <vcclr.h>
#include <iostream>
using namespace std;
using namespace System;
int main()
{
String^ s = "Testing String conversion to iostream.";
pin_ptr<const wchar_t> ptr = PtrToStringChars(s);
wcout << static_cast<const wchar_t*>( ptr ) << endl;
}
Convert text read from stdin to uppercase and write to stdout
#include "stdafx.h"
using namespace System;
int main()
{
String^ str;
while ((str = Console::ReadLine()) != nullptr)
{
Console::WriteLine( str->ToUpper() );
}
}
Create a copy, then concatenate new text
#include "stdafx.h"
using namespace System;
void main()
{
String^ s1 = "This will ";
String^ s2 = "be a ";
String^ s3 = "String";
//
String^ s4 = s2;
s4 = String::Concat(s4, "new ");
Console::WriteLine(String::Concat(s1, s4, s3));
}
Create a String
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
void main(void) {
String^ Digits = "";
for (int i=0; i<5; i++) {
Digits = String::Concat(Digits, i.ToString("d"));
Console::WriteLine(Digits);
}
}
Create some strings
#include "stdafx.h"
using namespace System;
void main()
{
String^ s1 = "This will ";
String^ s2 = "be a ";
String^ s3 = "String";
Console::WriteLine(String::Concat(s1, s2, s3));
}
Insert into a string
#include "stdafx.h"
using namespace System;
void main()
{
String^ s1 = "This will ";
String^ s2 = "be a ";
String^ s3 = "String";
String^ s4 = s2;
//
String^ s6 = s3->Insert(3, "ange Str");
Console::WriteLine(String::Concat(s1, s2, s6));
}
Output a string
#include "stdafx.h"
using namespace System;
void main(void)
{
Console::WriteLine("Hello C++/CLI World");
}
Remove text from strings
#include "stdafx.h"
using namespace System;
void main()
{
String^ s1 = "This will ";
String^ s2 = "be a ";
String^ s3 = "String";
s1 = s1->Remove(4, 5);
s2 = s2->Remove(0, 3);
Console::WriteLine(String::Concat(s1, "is ", s2, s3));
}
Replace stuff in a concatenated string
#include "stdafx.h"
using namespace System;
void main()
{
String^ s1 = "This will ";
String^ s2 = "be a ";
String^ s3 = "String";
String^ s4 = s2;
//
String^ s5 = String::Concat(s1, s2, s3)->Replace("i", "*");
Console::WriteLine(s5);
}
Reverse a string
#include "stdafx.h"
using namespace System;
void main()
{
array<int>^ a = gcnew array<int>(4);
array<String^>^ b = gcnew array<String^>(4);
for (int i = 0; i < a->Length; i++)
{
a[i] = i;
}
for (int i = 0; i < b->Length; i++)
{
b[i] = a[i].ToString();
}
for (int i = 0; i < b->Length; i++)
{
Console::WriteLine(b[i]);
}
Console::WriteLine();
Array::Reverse(b);
for (int i = 0; i < b->Length; i++)
{
Console::WriteLine(b[i]);
}
}
String Literals
#include "stdafx.h"
using namespace System;
void main()
{
String^ a = "Managed String";
String^ b = L"Unicode String";
Console::WriteLine(a);
Console::WriteLine(b);
}
String operator plus
#include "stdafx.h"
using namespace System;
int main()
{
String ^hrs = "Hours", ^mins = "Minutes";
wchar_t separator = ":";
int minutes = 56, hours = 1;
Console::WriteLine( hrs + separator + " " + hours + "\n" + mins + separator + " " + minutes);
}
Use static method String::Compare to compare two strings
#include "stdafx.h"
using namespace System;
int main()
{
String^ str1 = "cat";
String^ str2 = "cab";
if (str1->CompareTo( str2 ) < 0)
{
Console::WriteLine(str1 + " is less than " + str2);
}
// For variety, use the static method.
else if ( String::Compare(str1, str2) > 0 )
{
Console::WriteLine("{0} is less than {1}", str2, str1);
}
else if ( str1->CompareTo( str2 ) == 0)
{
Console::WriteLine("The strings are both equal, with value {0}.", str1);
}
}
Using string for each loop to output chars
#include "stdafx.h"
using namespace System;
int main()
{
String^ str1 = "this is a test";
for each (Char ch in str1)
{
Console::Write(ch);
}
Console::WriteLine();
}