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

这篇文章演示了字符串之间的转换使用如:

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(614);
            
this->ClientSize = System::Drawing::Size(292266);
            
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);

             }


    }
;
}



阅读更多
个人分类: C# VC
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭