如何切割字符串的内容直到特定的字符串或位置? [英] How to cut the content of a string till a particular string or position?

查看:148
本文介绍了如何切割字符串的内容直到特定的字符串或位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

毕竟我的研究,我在这里剪下这个字符串:

  {[(2015/11/30 | 01 :07:53.357 | -1227639088 | DefaultThread | ./src/Myprogram.cpp:564 | int main(int,argv **))]}服务器启动.... 
/ pre>

到字符串:

  ./ src / Myprogram。 cpp:564 | int main(int,argv **))]}服务器启动.... 

下面是我的代码:

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

using namespace std;

void func(std :: ostream& obj){
std :: string dbgmsg = dynamic_cast< std :: ostringstream&>(obj).str
cout<< dbgmsg; //我需要将这个字符串转换为./src/Myprogram.cpp:564|int main(int,argv **))]}服务器启动....
}

int main(){

std :: ostringstream ss;
ss<<{[(2015/11/30 | 01:07:53.357 | -1227639088 | DefaultThread | ./src/Myprogram.cpp:564 | int main(int,argv **)) ]}服务器启动....;
func(ss);

return 0;
}

我的GCC:gcc版本4.5.3(GCC)

b $ b

解决方案

晚会有点迟了,但这里是我的解决方案

  #include< iostream> 
#include< string>

std :: string getString(std :: string fullString){
int pos = 0;
for(int i = 0; i <4; i ++){
pos = fullString.find(|,pos + 1);
}
return fullString.substr(pos + 1);
}
int main(void){
std :: string str({[(2015/11/30 | 01:07:53.357 | -1227639088 | DefaultThread | ./src/ Myprogram.cpp:564 | int main(int,argv **))]}服务器启动....);
std :: cout<< getString(str)<< std :: endl;
return 0;
}


After all my research, I am here to cut this string :

{[(2015/11/30|01:07:53.357|-1227639088|DefaultThread|./src/Myprogram.cpp:564|int main(int, argv**))]} Server Starting....

to string:

./src/Myprogram.cpp:564|int main(int, argv**))]} Server Starting....

Below is my code:

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

using namespace std;

    void func(std::ostream&obj){
        std::string dbgmsg =  dynamic_cast<std::ostringstream&>(obj).str();
        cout << dbgmsg; //I need to convert this string to ./src/Myprogram.cpp:564|int main(int, argv**))]} Server Starting....
    }

    int main() {

        std::ostringstream ss;
        ss <<"{[(2015/11/30|01:07:53.357|-1227639088|DefaultThread|./src/Myprogram.cpp:564|int main(int, argv**))]} Server Starting....";
        func(ss);

        return 0;
    }

My GCC: gcc version 4.5.3 (GCC)

解决方案

A bit late to the party but here's my solution

#include <iostream>
#include <string>

std::string getString(std::string fullString){
    int pos = 0;
    for(int i = 0; i < 4; i++){
        pos = fullString.find("|", pos+1);
    }
    return fullString.substr(pos+1);
}
int main( void ){
    std::string str("{[(2015/11/30|01:07:53.357|-1227639088|DefaultThread|./src/Myprogram.cpp:564|int main(int, argv**))]} Server Starting....");
    std::cout << getString(str) << std::endl;
    return 0;
}

这篇关于如何切割字符串的内容直到特定的字符串或位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆