How to actually concatenate two Strings?Concatenate string constantsGet strings from Serial.read()error loading an array of stringsIs possible to concatenate integers?Declaring literal stringsHow to multiply strings?Error handling strings and charsHow to combine strings and text together?

How were medieval castles built in swamps or marshes without draining them?

How to prevent a hosting company from accessing a VM's encryption keys?

Is first Ubuntu user root?

Half filled water bottle

How does the OS tell whether an "Address is already in use"?

Curves with isogenous Jacobians

Why does Windows store Wi-Fi passwords in a reversible format?

What is this fighter jet at Weymouth NAS?

Is there a word or phrase that means "use other people's wifi or Internet service without consent"?

Shift lens vs move body?

Why all theories are Lorentz invariant?

How many petaflops does it take to land on the moon? What does Artemis need with an Aitken?

Is a memoized pure function itself considered pure?

Open subspaces of CW complexes

Book featuring a child learning from a crowdsourced AI book

Who was the most successful German spy against Great Britain in WWII, from the contemporary German perspective?

What is Soda Fountain Etiquette?

If I said I had $100 when asked, but I actually had $200, would I be lying by omission?

Redacting URLs as an email-phishing preventative?

Weighted smooth histogram

What if an AES GCM nonce overflows and collides with the GHASH nonce (0^128)?

Can you board the plane when your passport is valid less than 3 months?

What does it take for witness testimony to be believed?

1mth baby boy keeps peeing through diapers, sometimes diper seems practically unused



How to actually concatenate two Strings?


Concatenate string constantsGet strings from Serial.read()error loading an array of stringsIs possible to concatenate integers?Declaring literal stringsHow to multiply strings?Error handling strings and charsHow to combine strings and text together?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I'm using the SPISlave library, and I have the following code snippet:



String arg;

SPISlave.onData([arg](uint8_t *data, size_t len)
data[len] = 0;
arg += String((char *)data);
// ...



The compiler doesn't like my string concatenation, though:



/home/lars/sketch_apr01a/sketch_apr01a.ino: In lambda function:
sketch_apr01a:65:12: error: passing 'const String' as 'this' argument of 'String& String::operator+=(const String&)' discards qualifiers [-fpermissive]
arg += String((char *)data);



I've tried other versions as well, with similar results:



  • arg.concat((char *)data)

  • arg = arg + String((char *)data)

  • arg += String((const char *)data)

So how exactly do I write this unusual operation?









share







New contributor



larsb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Funny, on avr-g++ it's fine (with warnings about store duration). Anyway, you are capturing the arg variable by value, so it won't affect variable outside lambda function.

    – KIIV
    9 hours ago


















1















I'm using the SPISlave library, and I have the following code snippet:



String arg;

SPISlave.onData([arg](uint8_t *data, size_t len)
data[len] = 0;
arg += String((char *)data);
// ...



The compiler doesn't like my string concatenation, though:



/home/lars/sketch_apr01a/sketch_apr01a.ino: In lambda function:
sketch_apr01a:65:12: error: passing 'const String' as 'this' argument of 'String& String::operator+=(const String&)' discards qualifiers [-fpermissive]
arg += String((char *)data);



I've tried other versions as well, with similar results:



  • arg.concat((char *)data)

  • arg = arg + String((char *)data)

  • arg += String((const char *)data)

So how exactly do I write this unusual operation?









share







New contributor



larsb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Funny, on avr-g++ it's fine (with warnings about store duration). Anyway, you are capturing the arg variable by value, so it won't affect variable outside lambda function.

    – KIIV
    9 hours ago














1












1








1








I'm using the SPISlave library, and I have the following code snippet:



String arg;

SPISlave.onData([arg](uint8_t *data, size_t len)
data[len] = 0;
arg += String((char *)data);
// ...



The compiler doesn't like my string concatenation, though:



/home/lars/sketch_apr01a/sketch_apr01a.ino: In lambda function:
sketch_apr01a:65:12: error: passing 'const String' as 'this' argument of 'String& String::operator+=(const String&)' discards qualifiers [-fpermissive]
arg += String((char *)data);



I've tried other versions as well, with similar results:



  • arg.concat((char *)data)

  • arg = arg + String((char *)data)

  • arg += String((const char *)data)

So how exactly do I write this unusual operation?









share







New contributor



larsb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm using the SPISlave library, and I have the following code snippet:



String arg;

SPISlave.onData([arg](uint8_t *data, size_t len)
data[len] = 0;
arg += String((char *)data);
// ...



The compiler doesn't like my string concatenation, though:



/home/lars/sketch_apr01a/sketch_apr01a.ino: In lambda function:
sketch_apr01a:65:12: error: passing 'const String' as 'this' argument of 'String& String::operator+=(const String&)' discards qualifiers [-fpermissive]
arg += String((char *)data);



I've tried other versions as well, with similar results:



  • arg.concat((char *)data)

  • arg = arg + String((char *)data)

  • arg += String((const char *)data)

So how exactly do I write this unusual operation?







string





share







New contributor



larsb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share







New contributor



larsb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







share



share






New contributor



larsb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 9 hours ago









larsblarsb

82 bronze badges




82 bronze badges




New contributor



larsb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




larsb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















  • Funny, on avr-g++ it's fine (with warnings about store duration). Anyway, you are capturing the arg variable by value, so it won't affect variable outside lambda function.

    – KIIV
    9 hours ago


















  • Funny, on avr-g++ it's fine (with warnings about store duration). Anyway, you are capturing the arg variable by value, so it won't affect variable outside lambda function.

    – KIIV
    9 hours ago

















Funny, on avr-g++ it's fine (with warnings about store duration). Anyway, you are capturing the arg variable by value, so it won't affect variable outside lambda function.

– KIIV
9 hours ago






Funny, on avr-g++ it's fine (with warnings about store duration). Anyway, you are capturing the arg variable by value, so it won't affect variable outside lambda function.

– KIIV
9 hours ago











2 Answers
2






active

oldest

votes


















2















Your lambda is capturing arg by copy, which is most certainly not what
you want. And since it doesn't have the mutable qualifier, the
captured parameters are not modifiable. Hence the compiler error.



You could get rid of the error by qualifying the capture as mutable,
but you would then be modifying the captured copy of arg, not the
original one. The correct solution would be to capture the String by
reference instead of capturing by copy. See Lambda
expressions.



But then, I concur with Michel Keijzers that avoiding Strings altogether
is a better option.






share|improve this answer

























  • Makes perfect sense, thanks!

    – larsb
    8 hours ago











  • Thanks for the insight and correct answer too (upvoted)

    – Michel Keijzers
    6 hours ago


















1















Actually for such string concatenation (which is in a function that may be called a lot), this could result in memory fragmentation, an on most Arduinos the memory will be soon too scattered that no useful memory is left.



Instead, it's better to create beforehand a buffer with the maximum size of the string you want to handle, like:



static const int MAX_BUFFER_LENGTH = 256;

char buffer[MAX_BUFFER_LENGTH];


And use the function strcat or strncat for concatenating two strings; there are generic C functions.






share|improve this answer

























  • Fair enough, and good advice in general. But I'd still like to know the answer to my question. :-)

    – larsb
    9 hours ago











  • I wonder what error you get when using concat (cannot be a String passing operation error). Maybe some casting to const.

    – Michel Keijzers
    9 hours ago













Your Answer






StackExchange.ifUsing("editor", function ()
return StackExchange.using("schematics", function ()
StackExchange.schematics.init();
);
, "cicuitlab");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "540"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






larsb is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f68203%2fhow-to-actually-concatenate-two-strings%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2















Your lambda is capturing arg by copy, which is most certainly not what
you want. And since it doesn't have the mutable qualifier, the
captured parameters are not modifiable. Hence the compiler error.



You could get rid of the error by qualifying the capture as mutable,
but you would then be modifying the captured copy of arg, not the
original one. The correct solution would be to capture the String by
reference instead of capturing by copy. See Lambda
expressions.



But then, I concur with Michel Keijzers that avoiding Strings altogether
is a better option.






share|improve this answer

























  • Makes perfect sense, thanks!

    – larsb
    8 hours ago











  • Thanks for the insight and correct answer too (upvoted)

    – Michel Keijzers
    6 hours ago















2















Your lambda is capturing arg by copy, which is most certainly not what
you want. And since it doesn't have the mutable qualifier, the
captured parameters are not modifiable. Hence the compiler error.



You could get rid of the error by qualifying the capture as mutable,
but you would then be modifying the captured copy of arg, not the
original one. The correct solution would be to capture the String by
reference instead of capturing by copy. See Lambda
expressions.



But then, I concur with Michel Keijzers that avoiding Strings altogether
is a better option.






share|improve this answer

























  • Makes perfect sense, thanks!

    – larsb
    8 hours ago











  • Thanks for the insight and correct answer too (upvoted)

    – Michel Keijzers
    6 hours ago













2














2










2









Your lambda is capturing arg by copy, which is most certainly not what
you want. And since it doesn't have the mutable qualifier, the
captured parameters are not modifiable. Hence the compiler error.



You could get rid of the error by qualifying the capture as mutable,
but you would then be modifying the captured copy of arg, not the
original one. The correct solution would be to capture the String by
reference instead of capturing by copy. See Lambda
expressions.



But then, I concur with Michel Keijzers that avoiding Strings altogether
is a better option.






share|improve this answer













Your lambda is capturing arg by copy, which is most certainly not what
you want. And since it doesn't have the mutable qualifier, the
captured parameters are not modifiable. Hence the compiler error.



You could get rid of the error by qualifying the capture as mutable,
but you would then be modifying the captured copy of arg, not the
original one. The correct solution would be to capture the String by
reference instead of capturing by copy. See Lambda
expressions.



But then, I concur with Michel Keijzers that avoiding Strings altogether
is a better option.







share|improve this answer












share|improve this answer



share|improve this answer










answered 9 hours ago









Edgar BonetEdgar Bonet

26.9k2 gold badges25 silver badges47 bronze badges




26.9k2 gold badges25 silver badges47 bronze badges















  • Makes perfect sense, thanks!

    – larsb
    8 hours ago











  • Thanks for the insight and correct answer too (upvoted)

    – Michel Keijzers
    6 hours ago

















  • Makes perfect sense, thanks!

    – larsb
    8 hours ago











  • Thanks for the insight and correct answer too (upvoted)

    – Michel Keijzers
    6 hours ago
















Makes perfect sense, thanks!

– larsb
8 hours ago





Makes perfect sense, thanks!

– larsb
8 hours ago













Thanks for the insight and correct answer too (upvoted)

– Michel Keijzers
6 hours ago





Thanks for the insight and correct answer too (upvoted)

– Michel Keijzers
6 hours ago













1















Actually for such string concatenation (which is in a function that may be called a lot), this could result in memory fragmentation, an on most Arduinos the memory will be soon too scattered that no useful memory is left.



Instead, it's better to create beforehand a buffer with the maximum size of the string you want to handle, like:



static const int MAX_BUFFER_LENGTH = 256;

char buffer[MAX_BUFFER_LENGTH];


And use the function strcat or strncat for concatenating two strings; there are generic C functions.






share|improve this answer

























  • Fair enough, and good advice in general. But I'd still like to know the answer to my question. :-)

    – larsb
    9 hours ago











  • I wonder what error you get when using concat (cannot be a String passing operation error). Maybe some casting to const.

    – Michel Keijzers
    9 hours ago















1















Actually for such string concatenation (which is in a function that may be called a lot), this could result in memory fragmentation, an on most Arduinos the memory will be soon too scattered that no useful memory is left.



Instead, it's better to create beforehand a buffer with the maximum size of the string you want to handle, like:



static const int MAX_BUFFER_LENGTH = 256;

char buffer[MAX_BUFFER_LENGTH];


And use the function strcat or strncat for concatenating two strings; there are generic C functions.






share|improve this answer

























  • Fair enough, and good advice in general. But I'd still like to know the answer to my question. :-)

    – larsb
    9 hours ago











  • I wonder what error you get when using concat (cannot be a String passing operation error). Maybe some casting to const.

    – Michel Keijzers
    9 hours ago













1














1










1









Actually for such string concatenation (which is in a function that may be called a lot), this could result in memory fragmentation, an on most Arduinos the memory will be soon too scattered that no useful memory is left.



Instead, it's better to create beforehand a buffer with the maximum size of the string you want to handle, like:



static const int MAX_BUFFER_LENGTH = 256;

char buffer[MAX_BUFFER_LENGTH];


And use the function strcat or strncat for concatenating two strings; there are generic C functions.






share|improve this answer













Actually for such string concatenation (which is in a function that may be called a lot), this could result in memory fragmentation, an on most Arduinos the memory will be soon too scattered that no useful memory is left.



Instead, it's better to create beforehand a buffer with the maximum size of the string you want to handle, like:



static const int MAX_BUFFER_LENGTH = 256;

char buffer[MAX_BUFFER_LENGTH];


And use the function strcat or strncat for concatenating two strings; there are generic C functions.







share|improve this answer












share|improve this answer



share|improve this answer










answered 9 hours ago









Michel KeijzersMichel Keijzers

8,2576 gold badges21 silver badges41 bronze badges




8,2576 gold badges21 silver badges41 bronze badges















  • Fair enough, and good advice in general. But I'd still like to know the answer to my question. :-)

    – larsb
    9 hours ago











  • I wonder what error you get when using concat (cannot be a String passing operation error). Maybe some casting to const.

    – Michel Keijzers
    9 hours ago

















  • Fair enough, and good advice in general. But I'd still like to know the answer to my question. :-)

    – larsb
    9 hours ago











  • I wonder what error you get when using concat (cannot be a String passing operation error). Maybe some casting to const.

    – Michel Keijzers
    9 hours ago
















Fair enough, and good advice in general. But I'd still like to know the answer to my question. :-)

– larsb
9 hours ago





Fair enough, and good advice in general. But I'd still like to know the answer to my question. :-)

– larsb
9 hours ago













I wonder what error you get when using concat (cannot be a String passing operation error). Maybe some casting to const.

– Michel Keijzers
9 hours ago





I wonder what error you get when using concat (cannot be a String passing operation error). Maybe some casting to const.

– Michel Keijzers
9 hours ago










larsb is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















larsb is a new contributor. Be nice, and check out our Code of Conduct.












larsb is a new contributor. Be nice, and check out our Code of Conduct.











larsb is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Arduino Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f68203%2fhow-to-actually-concatenate-two-strings%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу