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

8 篇文章 0 订阅

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

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);

             }


    }
;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值