Replacing a quote by a "\n" ...

C

Choi

Hi !

I wonder how I can replace a quote by a "\n" in a string.

For example my string is rrrrrrr'ttttttt and I want to obtain :

rrrrrrr
ttttttt

I tried the function find, but I don't know how to specify in its
parameters the quote character.

Thanks guys !
 
E

Erik Wikström

Hi !

I wonder how I can replace a quote by a "\n" in a string.

For example my string is rrrrrrr'ttttttt and I want to obtain :

rrrrrrr
ttttttt

I tried the function find, but I don't know how to specify in its
parameters the quote character.

Does not "'" work?
 
E

Eric Viloin

Thanks for your answer, but it did not work ... Have you got another
idea Erik ?
 
A

Alf P. Steinbach

* Choi:
I wonder how I can replace a quote by a "\n" in a string.

For example my string is rrrrrrr'ttttttt and I want to obtain :

rrrrrrr
ttttttt

I tried the function find, but I don't know how to specify in its
parameters the quote character.

The following can be optimized in a great many ways, but don't do that
unless measurements show you need it:

std::string replace(
std::string const& s,
std::string const& match,
std::string const& replacement )
{
std::string result;
for( std::size_t i = 0; i < s.length(); ++i )
{
if( s.substr( i, match.length() ) == match )
{
result += replacement;
i += match.length() - 1;
}
else
{
result += s;
}
}
return result;
}

Again, re optimization: std::string is not well suited for string
handling. It's not even well suited for passing strings around,
technically. But it's standard, and that's one great plus point, so
great that std::string is the default choice for that.

However, if optimization is required, consider some other string class
first, instead of fiddling with std::string-based code.

Cheers, & hth.,

- Alf
 
T

Tomás Ó hÉilidhe

Choi:
I wonder how I can replace a quote by a "\n" in a string.

For example my string is rrrrrrr'ttttttt and I want to obtain :

rrrrrrr
ttttttt


We call that an apostrophe in my part of the world (as opposed to an
inverted comma). I don't know if there's a char replacement function in
the Standard, but it's trivial to write:

void Replace(char *p,char const before,char const after)
{
assert(p); assert(before);

for (; p = strchr(p,before); *p++ = after);
}
 
P

peter koch

Hi !

I wonder how I can replace a quote by a "\n" in a string.

For example my string is rrrrrrr'ttttttt and I want to obtain :

rrrrrrr
ttttttt

I tried the function find, but I don't know how to specify in its
parameters the quote character.

Thanks guys !

How about using std::replace? I wonder why noone have suggested that
before?

std::replace(str.begin(),str.end(),'\'','\n');

/Peter
 
J

James Kanze

Does not "'" work?

std::find, on a string, will want something that is a char, or
convertible to a char. "'" is an array.

What he needs is '\''. (More generally, I'd write "\'" as well.
For reasons of orthorgonality, I always escape both " and ',
both in strings and in character constants.)
 
J

James Kanze

[...]
Again, re optimization: std::string is not well suited for string
handling. It's not even well suited for passing strings around,
technically. But it's standard, and that's one great plus point, so
great that std::string is the default choice for that.

As a summary of std::string, this is actually pretty good. But
to be fair: std::string does define both the copy constructor
and an assignment operator---what else do you need for passing
strings around?
However, if optimization is required, consider some other
string class first, instead of fiddling with std::string-based
code.

More generally: don't use fundamental library types as part of
your application level abstraction. If your abstraction deals
with text, for example, don't use std::string for that text; use
some user defined class. A class which almost certainly uses
std::string in its implementation, at least until the profiler
says otherwise, but a class which defines very exactly the
interface you need. This is a good general rule, as it keeps
your options open. Use std::string directly, or a typedef to
std::string, and you're locked in; use a user defined class with
a narrower interface, and you can change the implementation in
any way that conforms to the interface.
 
A

Alf P. Steinbach

* James Kanze:
[...]
Again, re optimization: std::string is not well suited for string
handling. It's not even well suited for passing strings around,
technically. But it's standard, and that's one great plus point, so
great that std::string is the default choice for that.

As a summary of std::string, this is actually pretty good. But
to be fair: std::string does define both the copy constructor
and an assignment operator---what else do you need for passing
strings around?

Efficiency, safety and possibly more information.

Efficiency:

- contiguous buffer (being addressed in C++0x)
- being able to avoid copying or new-ing when you have a buffer
- being able to avoid copying for forming substrings

Safety:

- especially to be able to form a string for passing as exception
text, without possibility of exception

More information:

- e.g. character set.

Achieving this "normality" (what one naturally expect of a string type,
unless exposed to std::string for too long...) needs a new string interface.

More generally: don't use fundamental library types as part of
your application level abstraction. If your abstraction deals
with text, for example, don't use std::string for that text; use
some user defined class. A class which almost certainly uses
std::string in its implementation, at least until the profiler
says otherwise, but a class which defines very exactly the
interface you need. This is a good general rule, as it keeps
your options open. Use std::string directly, or a typedef to
std::string, and you're locked in; use a user defined class with
a narrower interface, and you can change the implementation in
any way that conforms to the interface.

Yup.


Cheers,

- Alf
 
J

James Kanze

* James Kanze:
[...]
Again, re optimization: std::string is not well suited for string
handling. It's not even well suited for passing strings around,
technically. But it's standard, and that's one great plus point, so
great that std::string is the default choice for that.
As a summary of std::string, this is actually pretty good. But
to be fair: std::string does define both the copy constructor
and an assignment operator---what else do you need for passing
strings around?
Efficiency, safety and possibly more information.

Most of these really concern creation of strings, rather than
just "passing them around". One point, however:

[...]
- being able to avoid copying for forming substrings

This is a deficiency in most current implementations, but is not
necessary according to the standard. An implementation more or
less like that of Java could be used, in which you don't need a
copy for substrings.

[...]
- e.g. character set.
Achieving this "normality" (what one naturally expect of a
string type, unless exposed to std::string for too long...)
needs a new string interface.

Having had to deal with "bad" string types in other languages
(in the past), I don't naturally expect very much:). (Or is
that I naturally don't expect very much?)

The lack of information concerning the encoding (character set)
is an awkward problem. It is one, however, that we're used to
dealing with: it affects a lot of other types as well. (Is that
double meters, or millimeters? Is that Decimal Euros, or
Pounds?)

In the end, I think we need both: a "raw" type, for cases where
the "units" are implicitly known, and a more complete type which
associates a unit with each value. (Note that I'm not talking
here about typing issues, where the user assigns a double
representing meters to a variable containing the weight. That's
a different problem---which probably needs solving as well.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top