Convert char* to String

Hi guys

How can i move the contents of one string into another string ?

Do you mean string or do you mean String ?
The topic title says one thing, your question say another

There is a vital difference

Do you mean String or string (note cases)?

Assuming a character array (string) then you can use strcpy or strncpy.

OK UKHeliBob beat me to asking :grinning:

String

Use the assignment operator
Better still, don't use Strings

String thisString;
String thatString = "something";
this String = thatString;

I want to move the "Credit" content, which is of type String, into "SendMessage", which is of type char*, so that I can send it to someone.

#include <Sim800L.h>

#define RX  10
#define TX  11

Sim800L GSM(RX, TX);

String PhoneNumber = "+ZZ***********" ;

char* ReceiveMessage ;

char* SendMessage ;

String Credit ;

String Cache ;

boolean Allow = false;

void setup()
{

 Serial.begin(9600);

 GSM.begin(9600);

 Basic_Settings();

}

void loop()
{

 if(GSM.available())
 {

  ReceiveMessage = GSM.readStringUntil('\n');
  Serial.println(ReceiveMessage);  

  if(ReceiveMessage == "C\r")
  {

   GSM.println("AT+CUSD=1,\"*141*1#\"");

   Allow = true;
      
  }  

  if(Allow)
  {

   Cache = ReceiveMessage.substring(ReceiveMessage.indexOf("Credit"));

   if(Cache.substring(0,6) == "Credit")
   {

    Cache = ReceiveMessage.substring(ReceiveMessage.indexOf("Credit"));

    Credit = Cache.substring(0,18);

    Sender_Message();

    Allow = false;
    
   }
    
  }

 }

}

void Sender_Message()
{

 GSM.sendSms(PhoneNumber , SendMessage);
  
}

void Basic_Settings()
{

 GSM.println("AT");
 
 delay(1000);

 GSM.println("AT+CMGF=1");
 
 delay(1000);

 GSM.println("AT+CNMI=1,2,0,0,0");
 
 delay(1000);

 GSM.println("AT+CSMP=17,167,2,25\r");

}

This is a String

char* SendMessage ;

and this a "pointer to char".
Two objects completely different...

In order to copy content of Credit to SendMessage you have to reallocate enought memory to contains the number of chars of Credit + 1.
Not simple and surely not reccommendable for newbies (as in general).

Why you don't use two String vars (or two char arrays) instead?

Because this is not possible.

@mstfa, and why not?
"don't know how" it's not exactly the same as "it's not possible"

String SendMessage = "This is possible";

GSM.sendSms(PhoneNumber , SendMessage.c_str() );

I changed the program this way and got the answer.

#include <Sim800L.h>

#define RX  10
#define TX  11

Sim800L GSM(RX, TX);

char* PhoneNumber = "+ZZ***********" ;

String ReceiveMessage ;

char* SendMessage ;

String Credit ;

String Cache ;

boolean Allow = false;

void setup()
{

 Serial.begin(9600);

 GSM.begin(9600);

 Basic_Settings();

}

void loop()
{

 if(GSM.available())
 {

  ReceiveMessage = GSM.readStringUntil('\n');
  Serial.println(ReceiveMessage);  

  if(ReceiveMessage == "C\r")
  {

   GSM.println("AT+CUSD=1,\"*141*1#\"");

   Allow = true;
      
  }  

  if(Allow)
  {

   Cache = ReceiveMessage.substring(ReceiveMessage.indexOf("Credit"));

   if(Cache.substring(0,6) == "Credit")
   {

    Cache = ReceiveMessage.substring(ReceiveMessage.indexOf("Credit"));

    Credit = Cache.substring(0,18);

    SendMessage = Credit.c_str() ;

    Sender_Message();

    Allow = false;
    
   }
    
  }

 }

}

void Sender_Message()
{

 GSM.sendSms(PhoneNumber , SendMessage);
  
}

void Basic_Settings()
{

 GSM.println("AT");
 
 delay(1000);

 GSM.println("AT+CMGF=1");
 
 delay(1000);

 GSM.println("AT+CNMI=1,2,0,0,0");
 
 delay(1000);

 GSM.println("AT+CSMP=17,167,2,25\r");

}

Thank you for your help !

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.