473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

concatenation operators

I'm coming from PHP into the world of C++ and there is one simple thing
I always did:
$othervar = " with another.";
$var = "My string" . $othervar;
output: "My string with another."

Now in C++ you can do this:
char* var "My string" " with another.";

But you cannot do this as you can in PHP:
char othervar [] " with another.";
char* var = "My string" othervar;
I have used strcat() but isn't there a better way?

Sep 21 '05 #1
8 3423
ar*****@gmail.com wrote:
I'm coming from PHP into the world of C++ and there is one simple thing
I always did:
$othervar = " with another.";
$var = "My string" . $othervar;
output: "My string with another."

Now in C++ you can do this:
char* var "My string" " with another.";

But you cannot do this as you can in PHP:
char othervar [] " with another.";
char* var = "My string" othervar;
I have used strcat() but isn't there a better way?


Use 'std::string' which has operator + defined for it. Do not use
bare pointers to char.

V
Sep 21 '05 #2
ar*****@gmail.com wrote:
I'm coming from PHP into the world of C++ and there is one simple thing
I always did:
$othervar = " with another.";
$var = "My string" . $othervar;
output: "My string with another."

Now in C++ you can do this:
char* var "My string" " with another.";

But you cannot do this as you can in PHP:
char othervar [] " with another.";
char* var = "My string" othervar;
I have used strcat() but isn't there a better way?

#include <string>

/* and later */

std::string othervar=" with another.";
std::string var="Mi string"+othervar;

There are better ways to do it, but this one is the one you will
recognize more easily
Sep 21 '05 #3
arto...@gmail.com wrote:
I'm coming from PHP into the world of C++ and there is one simple thing
I always did:
$othervar = " with another.";
$var = "My string" . $othervar;
output: "My string with another."

Now in C++ you can do this:
char* var "My string" " with another.";

But you cannot do this as you can in PHP:
char othervar [] " with another.";
char* var = "My string" othervar;
I have used strcat() but isn't there a better way?


Yes. Use std::string. Look it up in any C++ reference of your choice,
e.g., Stroustrup's _The C++ Programming Language_ (3rd Ed.). Here's an
example:

#include <iostream>
#include <string>
using namespace std;

int main()
{
const string s1 = "some string";
const string s2 = "some other string";
const string s3 = s1 + " and " + s2; // Concatenate
cout << s3 << endl;
return 0;
}

Cheers! --M

Sep 21 '05 #4
* ar*****@gmail.com:
I'm coming from PHP into the world of C++ and there is one simple thing
I always did:
$othervar = " with another.";
$var = "My string" . $othervar;
output: "My string with another."

Now in C++ you can do this:
char* var "My string" " with another.";

But you cannot do this as you can in PHP:
char othervar [] " with another.";
char* var = "My string" othervar;
I have used strcat() but isn't there a better way?


#include <string>

std::string const othervar = " with another.";
std::string const var = "My string" + othervar;

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 21 '05 #5
Can't we get some sort of consensus here........Geez!

Sep 21 '05 #6
Thanks.

I use http://cplusplus.com as a reference but might have missed this
somewhere.

Sep 22 '05 #7
ar*****@gmail.com wrote:
Thanks.

I use http://cplusplus.com as a reference but might have missed this
somewhere.


Missed what?

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Sep 22 '05 #8
ar*****@gmail.com wrote:
Thanks.

I use http://cplusplus.com as a reference but might have missed this
somewhere.


You missed it because, at present, they don't have any coverage of the
STL or strings. I'd suggest you get a book if you want a tutorial:
Stroutrup's _The C++ Programming Language_ (3rd ed.), Lippman et al.'s
_C++ Primer_ (4th Ed.), Josuttis' _The C++ Standard Library: A Tutorial
and Reference_, or Koenig and Moo's _Accelerated C++_. They're more
complete than the online source, and chances are your local library has
at least one of them.

Cheers! --M

Sep 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jonathan | last post by:
Why did the designers of PHP decide to use period (".") as the string concatenation operator rater than the obviously more logical plus ("+")? //JJ
14
by: Uwe Mayer | last post by:
Hi, I know the python community is not very receptive towards extending the python syntax. Nevertheless I'd like to make a suggestion and hear your pro and cons. I want so suggest a...
5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
8
by: mrstephengross | last post by:
I'm using gcc 3.3.1 to compile the following code (below). I've written a macro to simplify writing operators. The macro uses the '##' operator to paste together 'operator' and the name of the...
9
by: Rebecca Smith | last post by:
In past versions of Access it was a simple matter to create a persons full name from the three strings 'FirstName' 'MiddleInit' and 'LastName' and include a period after the middle initial. Using a...
23
by: Bonj | last post by:
what is the correct form of string concatenation in VB.NET, + or &? Both seem to work, but which is correct? I know it's + in C# and & in VB6, but which in VB.NET?
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.