Hello i want to cut string in this situation:
if (std::count(text.begin(), text.end(), ',') > 3 {
//Cut String after 3 ','
}
text look like this:
"item1, item2, item3, item4, item5"
and i want this:
"item1, item2, item3, item4[Cut everything out on the right side >>], item5" and return "item1, item2, item3, item4"
Looking on first answer im looking for something like this(but working):
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
string s1 = "item1, item2, item3, item4, item5";
if(std::count(s1.begin(), s1.end(), ',') > 3) {
int comma = 0;
int x = sizeof(s1);
for(int i = 0; i < x; i++) {
if (comma == 3) {
string sub = s1.substr(0, i);
cout << "String is: " << sub;
}
if(s1[i] == ',') {
comma++;
}
}
}
return 0;
}
Look at std::string::substr
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
// Take any string
string s1 = "item1, item2, item3, item4, item5";
// Start at position 0 to position of character ',' in "item4,"
string sub = s1.substr(0, 26);
// prints item1, item2, item3, item4
cout << "String is: " << sub;
return 0;
}
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
string s1 = "item1, item2, item3, item4, item5";
if(std::count(s1.begin(), s1.end(), ',') > 3) {
int comma = 0;
int x = s1.length();
for(int i = 0; i < x; i++) {
if (comma == 3) {
string sub = s1.substr(0, i-1);
cout << "String is: " << sub;
return sub;
}
if(s1[i] == ',') {
comma++;
}
}
}
return 0;
}
There is no need to use std::count()
, that is just wasted overhead. You are scanning the string twice when once would suffice.
Also, your use of sizeof()
is wrong, you need to use std::string::size()
instead. sizeof(s1)
tells you the byte size of s1
(ie, of the std::string
class itself) at compile-time, whereas s1.size()
tells you how many characters are in s1
at runtime.
I suggest using std::string::find()
in a loop, eg:
#include <string>
#include <iostream>
std::string cutAtNComma(const std::string &s, int n) {
std::string::size_type idx = 0;
while (n-- > 0) {
idx = s.find(',', idx);
if (idx == std::string::npos) break;
if (n == 0) return s.substr(0, idx);
++idx;
}
return s;
}
int main() {
std::string s1 = "item1, item2, item3, item4, item5";
std::string sub = cutAtNComma(s1, 4);
std::cout << "String is: " << sub;
return 0;
}
Alternatively, you can use std::find_if()
instead, eg:
#include <string>
#include <iostream>
#include <algorithm>
std::string cutAtNComma(const std::string &s, int n) {
auto iter = std::find_if(s.cbegin(), s.cend(),
[n](char c) mutable {
if (c == ',') {
if (--n == 0) return true;
}
return false;
}
);
if (iter != s.cend()) return std::string(s.cbegin(), iter);
return s;
}
int main() {
std::string s1 = "item1, item2, item3, item4, item5";
std::string sub = cutAtNComma(s1, 4);
std::cout << "String is: " << sub;
return 0;
}
You need to change little code as below.
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
string s1 = "item1, item2, item3, item4, item5";
int comma = 0;
int x = s1.length();
bool flag = false;
for(int i = 0; i < x; i++) {
if (comma == 4) {
string sub = s1.substr(0, i-1);
cout << "String is: " << sub;
flag = false;
return 0;
}
if(s1[i] == ',') {
comma++;
}
else{
flag = true;
}
}
if(flag)
cout<<"String not contain more then 3 comma's \n String is :: "<<s1;
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.