473,320 Members | 1,976 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,320 software developers and data experts.

joining 2 strings

Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";

Is there a better way of writing this?

Cheers,
Sam
Aug 31 '08 #1
7 1961
cch
于 Sat, 30 Aug 2008 21:01:48 -0700,sam.barker0写到:
Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";

Is there a better way of writing this?

Cheers,
Sam
I am worry that the w was treated as four chars, not what you want.

The stringstream might help.
Aug 31 '08 #2
sa*********@gmail.com wrote:
Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";

Is there a better way of writing this?
Repeat after me. C++ is not Java.

Now, your problem. String literals are of type const char *. You can't
add them. Also, what you think the adding of w does is not right.

I assume you're trying to get "blah0blah". For that you should use a
stringstream.

#include <sstream>
#include <string>

int main()
{
int w = 0;
std::ostreamstring ss;
ss << "blah" << w << "blah";
std::string z = ss.str();
}
Aug 31 '08 #3
sa*********@gmail.com wrote:
Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";
Define works. Have you checked the result?

--
Ian Collins.
Aug 31 '08 #4
On Aug 31, 5:17*am, red floyd <no.spam.h...@example.comwrote:
sam.bark...@gmail.com wrote:
Hi,
I tried to use the java approach to join 2 strings
int w=0;
string z="blah " + w + "blah "
but this gives me an error
but this works
string z("blah");
z+=w;
z+="blah";
Is there a better way of writing this?

Repeat after me. *C++ is not Java.

Now, your problem. *String literals are of type const char *. *You can't
add them. *Also, what you think the adding of w does is not right.

I assume you're trying to get "blah0blah". *For that you should use a
stringstream.

#include <sstream>
#include <string>

int main()
{
* *int w = 0;
* *std::ostreamstring ss;
* *ss << "blah" << w << "blah";
* *std::string z = ss.str();

}- Hide quoted text -

- Show quoted text -
I am not keen on the insistence that std::ostringstream is the only
valid way to convert a variable to its string representation.
ostringstream is very useful for building a sentence out of assorted
variables, but its flexibility does cost in performance. So while I do
use the 'stream solution in UI code and the like, in more performance
sensitive situations I would generally solve the original problem
using code along the lines of:

string z("blah");
char buffer[12];
z += _atol(w, buffer, 10); // convert long to C string
z += "blah";

(Where I think I'm assuming posix conformance? Is _atol more than
likely to be about?)

I would not even try to use operator+ in this situation, as it uses a
temporary to store the return value for each invocation. Whereas
operator+= simply appends the new string to the buffer of the existing
instance. But if you were determined to do it, then the following
"works".

char buffer[12];
string z = string("blah") + _ltoa(w, buffer, 10) + "blah ";

Note that I would generally wrap the _atol and buffer in a naive
little class. This class is only intended to be used with operator+=
and does will not even compile in most other situations.

class ToString
{
public:
ToString(long value)
{
_ltoa(value, buffer, radix);
}

operator const char*() const
{
return buffer;
}

private:
static const int radix = 10;
// INT_MIN (-2147483647 - 1)
// INT_MAX 2147483647
// longest string = 11 chars + 1 for null terminator
static const int bufferSize = 12;
char buffer[bufferSize];
};

Which allows you to write:

string z("blah");
z += ToString(w);
z += "blah";

The boost.lexical_cast also allows you to write code of a similar form

using boost::lexical_cast;

string z("blah");
z += lexical_cast<string>(w);
z += "blah";

but lexical_cast uses std::ostreamstream to do the work so has the
same performance as the normal 'stream solution.

Andy

Aug 31 '08 #5
On 2008-08-31 12:23, an**********@hotmail.com wrote:
On Aug 31, 5:17 am, red floyd <no.spam.h...@example.comwrote:
>sam.bark...@gmail.com wrote:
Hi,
I tried to use the java approach to join 2 strings
int w=0;
string z="blah " + w + "blah "
but this gives me an error
but this works
string z("blah");
z+=w;
z+="blah";
Is there a better way of writing this?

Repeat after me. C++ is not Java.

Now, your problem. String literals are of type const char *. You can't
add them. Also, what you think the adding of w does is not right.

I assume you're trying to get "blah0blah". For that you should use a
stringstream.

#include <sstream>
#include <string>

int main()
{
int w = 0;
std::ostreamstring ss;
ss << "blah" << w << "blah";
std::string z = ss.str();

}- Hide quoted text -

- Show quoted text -

I am not keen on the insistence that std::ostringstream is the only
valid way to convert a variable to its string representation.
ostringstream is very useful for building a sentence out of assorted
variables, but its flexibility does cost in performance. So while I do
use the 'stream solution in UI code and the like, in more performance
sensitive situations I would generally solve the original problem
using code along the lines of:

string z("blah");
char buffer[12];
z += _atol(w, buffer, 10); // convert long to C string
z += "blah";

(Where I think I'm assuming posix conformance? Is _atol more than
likely to be about?)
Not very likely at all, as far as I can see. POSIX only defines atol()
but no _atol(), and I think that holds for most functions beginning with
an underscore (I could only find 5 functions in POSIX beginning with an
underscore).

If you are in need of performance and want to construct strings I think
using snprintf() is probably the best idea, just make sure that the
return-value is less than n.

--
Erik Wikström
Aug 31 '08 #6
Erik Wikström wrote:
On 2008-08-31 12:23, an**********@hotmail.com wrote:
>On Aug 31, 5:17 am, red floyd <no.spam.h...@example.comwrote:
>>sam.bark...@gmail.com wrote:
Hi,
I tried to use the java approach to join 2 strings

int w=0;
string z="blah " + w + "blah "
but this gives me an error

but this works
string z("blah");
z+=w;
z+="blah";

Is there a better way of writing this?

Repeat after me. C++ is not Java.

Now, your problem. String literals are of type const char *. You can't
add them. Also, what you think the adding of w does is not right.

I assume you're trying to get "blah0blah". For that you should use a
stringstream.

#include <sstream>
#include <string>

int main()
{
int w = 0;
std::ostreamstring ss;
ss << "blah" << w << "blah";
std::string z = ss.str();

}- Hide quoted text -

- Show quoted text -

I am not keen on the insistence that std::ostringstream is the only
valid way to convert a variable to its string representation.
ostringstream is very useful for building a sentence out of assorted
variables, but its flexibility does cost in performance. So while I do
use the 'stream solution in UI code and the like, in more performance
sensitive situations I would generally solve the original problem
using code along the lines of:

string z("blah");
char buffer[12];
z += _atol(w, buffer, 10); // convert long to C string
z += "blah";

(Where I think I'm assuming posix conformance? Is _atol more than
likely to be about?)

Not very likely at all, as far as I can see. POSIX only defines atol()
but no _atol(), and I think that holds for most functions beginning with
an underscore (I could only find 5 functions in POSIX beginning with an
underscore).
I think Andy is thinking of ltoa (convert int to string), which is not
defined by POSIX, AFAIK. The idiomatic (though kind of ugly) POSIX solution
is
char buf[12];
snprintf(buf, sizeof(buf), "%d", i);
or
char buf[200];
snprintf(buf, sizeof(buf), "Blah %dblah", w);
std::string z(buf);
Aug 31 '08 #7
On Aug 31, 10:23*pm, andy_west...@hotmail.com wrote:
I am not keen on the insistence that std::ostringstream is the only
valid way to convert a variable to its string representation.
ostringstream is very useful for building a sentence out of assorted
variables, but its flexibility does cost in performance.
class ToString
{
public:
* * ToString(long value)
* * {
* * * * _ltoa(value, buffer, radix);
* * }

* * operator const char*() const
* * {
* * * * return buffer;
* * }

private:
* * static const int radix = 10;
* * // INT_MIN * * (-2147483647 - 1)
* * // INT_MAX * * 2147483647
* * // longest string = 11 chars + 1 for null terminator
* * static const int bufferSize = 12;
* * char buffer[bufferSize];
};
This seems like a poor idea to me, if your code
is compiled on a system where ints are bigger then
it will silently cause a buffer overflow.

Anyway, I think you will find that stringstream
does something like this internally anyway,
except it doesn't need to allocate as much memory!

(You have to allocate a 'ToString' object, but the
stringstream may already have enough memory allocated
in its internal buffer to convert the int without
having to allocate more memory).
Sep 1 '08 #8

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

Similar topics

4
by: socialism001 | last post by:
I'm new to php and have read the implode statement on php.net but I guess I still don't understand it. Below is what I have: $value = implode($disp_cap," - ",substr($value,0,300)); $disp_cap...
1
by: Joe | last post by:
I've got 3 columns in a mysql database that I am trying to join. I'm using CONCAT(), but it's giving me NULL results. This has worked for me before ... What am I doing wrong? Here's a straight...
2
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2...
9
by: Eric Sabine | last post by:
Can someone give me a practical example of why I would join threads? I am assuming that you would typically join a background thread with the UI thread and not a background to a background, but...
10
by: Captain Nemo | last post by:
Hi I'm working on an ASP project where the clients want to be able to effectively perform SELECT queries joining tables from two different databases (located on the same SQL-Server). Does...
2
by: Wally | last post by:
Dim var1 as String Dim var2 as String var1="message1" var2="message2" how do I join the two string variables?
13
by: benoit.lefebvre | last post by:
How can I join args from a function ? Here is what I've done so far but I'm getting weird results code: ------------8<-------------------------- #include<stdio.h> int main (int argc, char...
2
by: Supermansteel | last post by:
I am joining these 2 tables together in Access 2003 and can't figure out the exact way of writing this script......Can anyone help? I have the following SQL: SELECT...
13
by: SUBHABRATA | last post by:
Dear Group, I am trying the following code line: def try2(n): a1=raw_input("PRINT A STRING:") a2=a1.split() a3="God Godess Heaven Sky" for x in a2: a4=a3.find(x) if a4>-1: a5=a3
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.