Terminate a c++ string [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is there a way to terminate a C++ string at any arbitary location. This is very easy in C as we can just insert a null character wherever we want. But how can the same be achieved in C++ String.
For example, Let's consider the following example,
string str = "This is Stack OverflowXXXX";
Now I want to terminate this string so that I would get "This is Stack Overflow".

Yep! Use string::erase:
str.erase(k);
will erase all characters from position k forward. There's no way to "undo" this to get those characters back, though.
Hope this helps!

Related

C++ *string convert to string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've seen a code and there was string* name. Isn't it wrong? I mean string name is already creating a vector of characters, what would there be string* ? Thank you!
It would be something like
string *xyz = new string (...)
which is a string pointer.
Can you please post the part of a code here of what you saw/made.

How can I match a pattern which is not containg any capital letter in Regular Expression? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have tried to match a string which is not contain any capital letter.But I don't Know how to do it. Can anyone help me.
Compare a lower cased version of the string to what the user entered. If they're equal, it's all in lowercase.

regex to validate string of 10 characters which are all digits [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I want to match a string of 10 characters and all of them need to be integers. How do I write a regular expression to check for this format.
Valid values should be something like - '1234567890', '4321567890'
The easiest (not all dialects support this):
[0-9]{10}
Another option:
[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
If you match the whole string, don't forget the ^ and $ markers:
^[0-9]{10}$

Format a C++ string with template [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the best way to transform a string with a special template, like a sed unix transform.
i must to do this with QT c++ :
original string : 00048500854006F85FF4B0
before c++ transform : 48500854006F85FF4B
Eliminate all the 0 in the start and the end of my string (not in the middle).
maybe a solution with sprintf ?
thank you very much for your help.
It's possible to do using regular expression:
QString("00048500854006F85FF4B0").remove(QRegExp("(^(0)+)|((0)+$)"));
You can always use the power of sed by calling system ("sed 's/foo/bar/' file"); or by using the Qt QProcess. In that case see this and this

how to overwrite portion of one string with another starting from a given position? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I mean the way that is done in editors with insert key on.
So having string like:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The effect will be:
~~~~~~~~~~Hello!~~~~~~~~~~~~~~~~~~
that is without changing length of the string.
Overwriting a portion of a string is done with one of the several overloads of std::string's replace member function, for example:
string str = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
string rep = "Hello!";
cout << str.replace(5, rep.size(), rep) << endl;
You can play with this example at ideone [link].
The simplest solution is probably to use std::copy, with the
appropriate iterators:
std::copy( newText.begin(), newText.end(), str.begin() + n );
Just be sure that the target string is big enough.