您的位置:首页 > 其它

演示了字符串在String ,ansi char ,std::string之间的转换

2007-10-10 17:43 323 查看
这篇文章演示了字符串之间的转换使用如:

1. String转为ansi char

2. String转为std::string

3. ansi char转为String

4. std::string 转为String


#pragma once


#include <vcclr.h>


#include <atlstr.h>


#include <stdio.h>


#using <mscorlib.dll> //Marshal::StringToHGlobalAnsi


using namespace System::Runtime::InteropServices; //Marshal::StringToHGlobalAnsi


#using <system.windows.forms.dll>


#undef MessageBox // avoid error C2653 'MessageBoxA': is not a class or namespace name


using namespace std;


#include <string>






namespace ttttt




...{


using namespace System;


using namespace System::ComponentModel;


using namespace System::Collections;


using namespace System::Windows::Forms;


using namespace System::Data;


using namespace System::Drawing;






/**//// <summary>


/// Summary for Form1


///


/// WARNING: If you change the name of this class, you will need to change the


/// 'Resource File Name' property for the managed resource compiler tool


/// associated with all .resx files this class depends on. Otherwise,


/// the designers will not be able to interact properly with localized


/// resources associated with this form.


/// </summary>


public __gc class Form1 : public System::Windows::Forms::Form




...{


public:


Form1(void)




...{


InitializeComponent();


}




void String2Ansichar(char* strdst,String* strSrc)




...{


const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(strSrc).ToPointer());


strcpy(strdst,chars);


Marshal::FreeHGlobal(System::IntPtr((void*)chars));


}




void String2string(std::string& strdst,String*strSrc)




...{


const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(strSrc).ToPointer());


strdst = chars;


Marshal::FreeHGlobal(System::IntPtr((void*)chars));




}




protected:


void Dispose(Boolean disposing)




...{


if (disposing && components)




...{


components->Dispose();


}


__super::Dispose(disposing);


}




private:




/**//// <summary>


/// Required designer variable.


/// </summary>


System::ComponentModel::Container * components;






/**//// <summary>


/// Required method for Designer support - do not modify


/// the contents of this method with the code editor.


/// </summary>


void InitializeComponent(void)




...{


//


// Form1


//


this->AutoScaleBaseSize = System::Drawing::Size(6, 14);


this->ClientSize = System::Drawing::Size(292, 266);


this->Name = S"Form1";


this->Text = S"Form1";


this->Load += new System::EventHandler(this, Form1_Load);




}


private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)




...{


//把ansi char 转为 String




char test1[50] = ...{"abcdefg"};


String* pStr = test1;


MessageBox::Show(pStr, "测试字符转换",MessageBoxButtons::OK, MessageBoxIcon::Information);






String* pStr2 = new String("123456789");


//把String 转为 ansi char




char test2[50] = ...{""};


String2Ansichar(test2,pStr2);


MessageBox::Show(test2, "测试字符转换",MessageBoxButtons::OK, MessageBoxIcon::Information);




//把String转为std::string


std::string test3 = "";


String2string(test3,pStr2);


MessageBox::Show(test3.c_str(), "测试字符转换",MessageBoxButtons::OK, MessageBoxIcon::Information);




//如何获取String的第一个字符串


int first = pStr->Chars[0];


sprintf(test2,"%d",first);


MessageBox::Show(test2, "测试字符转换",MessageBoxButtons::OK, MessageBoxIcon::Information);




}




};


}





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: