Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > [NEWBIE] from C string to std::string

Reply
Thread Tools

[NEWBIE] from C string to std::string

 
 
Stefano Sabatini
Guest
Posts: n/a
 
      01-17-2008
Hi guys,
which is the best way to define a std::string using some C strings
(arrays of chars)?

Here it is the code:

#include <iostream>
#include <string>

using namespace std;

int main(void) {
string s;
// this won't even compile, since in this case "this is a" and
// others string are interpreted like char arrays
// s = "this is a" + " string.";

// OK, but awkward
s = string("this is a") + string(" string.");
cout << s;

// a better way??
// s = ...

return 0;
}

--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
 
Reply With Quote
 
 
 
 
Stefano Sabatini
Guest
Posts: n/a
 
      01-17-2008
On 2008-01-17, Stefano Sabatini <(E-Mail Removed)> wrote:
> Hi guys,
> which is the best way to define a std::string using some C strings
> (arrays of chars)?
>
> Here it is the code:
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main(void) {
> string s;
> // this won't even compile, since in this case "this is a" and
> // others string are interpreted like char arrays
> // s = "this is a" + " string.";
>
> // OK, but awkward
> s = string("this is a") + string(" string.");
> cout << s;
>
> // a better way??
> // s = ...
>
> return 0;
> }


Mmh..., it was very simple, reading another thread and FAQ-C++-lite
then I finally found a satisfying solution:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(void) {
string s;
// this won't work, since in this case "this" are interpreted like char arrays
// s = "this is a" + " string.";

// OK, but awkward
s = string("this is a") + string(" string");
cout << s;

// a better way??
std:stringstream o;
int i=-1;
o << "this is " << " a string " << "and this is a number: " << i << endl;
cout << o.str();

return 0;
}

for (int i=0; i < 10; i++)
cout << "regards ";
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      01-17-2008
Stefano Sabatini wrote:
> On 2008-01-17, Stefano Sabatini <(E-Mail Removed)> wrote:
>> Hi guys,
>> which is the best way to define a std::string using some C strings
>> (arrays of chars)?
>>
>> Here it is the code:
>>
>> #include <iostream>
>> #include <string>
>>
>> using namespace std;
>>
>> int main(void) {
>> string s;
>> // this won't even compile, since in this case "this is a" and
>> // others string are interpreted like char arrays
>> // s = "this is a" + " string.";
>>
>> // OK, but awkward
>> s = string("this is a") + string(" string.");
>> cout << s;
>>
>> // a better way??
>> // s = ...
>>
>> return 0;
>> }

>
> Mmh..., it was very simple, reading another thread and FAQ-C++-lite
> then I finally found a satisfying solution:
>
> #include <iostream>
> #include <string>
> #include <sstream>
>
> using namespace std;
>
> int main(void) {
> string s;
> // this won't work, since in this case "this" are interpreted like char arrays
> // s = "this is a" + " string.";
>
> // OK, but awkward
> s = string("this is a") + string(" string");
> cout << s;
>
> // a better way??
> std:stringstream o;
> int i=-1;
> o << "this is " << " a string " << "and this is a number: " << i << endl;
> cout << o.str();
>
> return 0;
> }


This doesn't corespond to what you asked, as you haven't mentioned
numbers - only char arrays. I expected something like this:

string s( "a very "
"long "
"string" );
 
Reply With Quote
 
Stefano Sabatini
Guest
Posts: n/a
 
      01-17-2008
On 2008-01-17, anon <(E-Mail Removed)> wrote:
> Stefano Sabatini wrote:
>> On 2008-01-17, Stefano Sabatini <(E-Mail Removed)> wrote:

[...]
>> Mmh..., it was very simple, reading another thread and FAQ-C++-lite
>> then I finally found a satisfying solution:
>>
>> #include <iostream>
>> #include <string>
>> #include <sstream>
>>
>> using namespace std;
>>
>> int main(void) {
>> string s;
>> // this won't work, since in this case "this" are interpreted like char arrays
>> // s = "this is a" + " string.";
>>
>> // OK, but awkward
>> s = string("this is a") + string(" string");
>> cout << s;
>>
>> // a better way??
>> std:stringstream o;
>> int i=-1;
>> o << "this is " << " a string " << "and this is a number: " << i << endl;
>> cout << o.str();
>>
>> return 0;
>> }

>
> This doesn't corespond to what you asked, as you haven't mentioned
> numbers - only char arrays. I expected something like this:
>
> string s( "a very "
> "long "
> "string" );


Hi, yes indeed, though what I was trying to ask for was if that was
possible to use a string in way similar to an iostream. sstream
addresses this requirement.

Thanks for your attention.

Regards.
--
Stefano Sabatini
Linux user number 337176 (see http://counter.li.org)
 
Reply With Quote
 
Linonut
Guest
Posts: n/a
 
      01-17-2008
* Stefano Sabatini peremptorily fired off this memo:

>> string s( "a very "
>> "long "
>> "string" );

>
> Hi, yes indeed, though what I was trying to ask for was if that was
> possible to use a string in way similar to an iostream. sstream
> addresses this requirement.
>
> Thanks for your attention.


Don't forget about:

s += "string value";

--
Intellect annuls Fate.
So far as a man thinks, he is free.
-- Ralph Waldo Emerson
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      01-17-2008
Stefano Sabatini wrote:
> Hi guys,
> which is the best way to define a std::string using some C strings
> (arrays of chars)?
>
> Here it is the code:
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main(void) {
> string s;
> // this won't even compile, since in this case "this is a" and
> // others string are interpreted like char arrays
> // s = "this is a" + " string.";


Adding two string literals together is done by the preprocessor. You
just have to put them adjacent to each other, WITHOUT any operators:

s = "this is a" " string.";


Of course, why would you want to do this?


Bo Persson


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-18-2008
Stefano Sabatini wrote:
> Hi guys,
> which is the best way to define a std::string using some C strings
> (arrays of chars)?
>
> Here it is the code:
>
> #include <iostream>
> #include <string>
>
> using namespace std;
>
> int main(void) {
> string s;
> // this won't even compile, since in this case "this is a" and
> // others string are interpreted like char arrays
> // s = "this is a" + " string.";
>
> // OK, but awkward
> s = string("this is a") + string(" string.");
> cout << s;
>
> // a better way??
> // s = ...
>
> return 0;
> }


Only the first string has to be converted to a std::string. Consider.

#include <string>

int main()
{
std::string s1;
s1 = "String1";
s1 += "string2";

std::string s2;
s2 = std::string("String1") + "String2";

std::string s3;
s3 = std::string("String1") + "String2" + "String3" + "String4";
}


--
Jim Langston
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      01-22-2008
In message <(E-Mail Removed)>, Bo Persson <(E-Mail Removed)>
writes
>Stefano Sabatini wrote:
>> Hi guys,
>> which is the best way to define a std::string using some C strings
>> (arrays of chars)?
>>
>> Here it is the code:
>>
>> #include <iostream>
>> #include <string>
>>
>> using namespace std;
>>
>> int main(void) {
>> string s;
>> // this won't even compile, since in this case "this is a" and
>> // others string are interpreted like char arrays
>> // s = "this is a" + " string.";

>
>Adding two string literals together is done by the preprocessor. You
>just have to put them adjacent to each other, WITHOUT any operators:
>
>s = "this is a" " string.";
>
>
>Of course, why would you want to do this?
>


One reason might be to embed comments:

std::string angleExpression(
"^\\s*" /* skip leading whitespace */
"([-+]?)" /* exp1 is optional sign */
"\\s*" /* skip whitespace */
"(\\d*)" /* exp2 is integer degrees */
"([:nsew])" /* exp3 is punctuation, maybe quadrant letter */
"(\\d*)" /* exp4 is integer minutes */
"[:']" /* punctuator between min & sec */
"(\\d*)" /* exp5 is integer part of seconds */
"[.,]?" /* maybe there's a decimal part */
"(\\d*)" /* exp6 is fraction of seconds */
"[:"]?" /* may be final punctuation */
"\\s*" /* whitespace */
"([nsew]?)" /* exp7 is quadrant letter */
);

--
Richard Herring
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      01-22-2008
Bo Persson wrote:
> s = "this is a" " string.";
>
> Of course, why would you want to do this?


I use this quite a lot with strings which are longer than a
reasonably-sized code line. I also often use it with multi-lined strings
(ie. strings which have newlines in them), eg like this:

s = "This is a long text with\n"
"several lines using the\n"
"newline character.\n";
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments