【问题标题】:How to print substring contained between braces?如何打印大括号之间包含的子字符串?
【发布时间】:2021-09-25 22:12:56
【问题描述】:

我正在编写一个函数,应该输出字符串中大括号之间的任何内容
(例如,hello world 代表 text{hello world})。
但是当我尝试运行它时它没有输出任何东西。您知道可能出了什么问题吗?

#include <iostream>
#include <string>

using namespace std;

int main(){
    start:    
    std::string funct;
    
    std::cout << "-->";
    std::cin >> funct;
    
    int temp = 0;

//focus on what's down here V
    
    string word;
    if (funct.find("text") != string::npos)
        {
            for(int i=0; i<1; i){
                if(funct.at(temp)=='}'){i=1;}
                    else{
                    temp += temp;
                    word = funct.at(temp + 5);}
            }
        cout<<word<<endl;    
        }
    
    cout<<endl<<endl<<"=================================================="<<endl<<endl;
    goto start;
    
    return 0;
}

*Edit,'funct' 是原始用户输入,'temp' 只是一个占位符整数。

【问题讨论】:

  • 请发minimal reproducible example(即分享一个程序而不是sn-p)。
  • 什么是函数? temp 已定义。
  • @Jorengarenar。 OP 想要创建一个函数,该函数接受一个看起来像 text{...} 的输入,并且函数应该返回大括号内的所有内容
  • @smac89 哦,更有意义。
  • 尝试cout &lt;&lt; word &lt;&lt; endl; 而不是cout &lt;&lt; endl &lt;&lt; word;

标签: c++ substring


【解决方案1】:

据我了解,您需要一个函数来解析字符串并打印 'text{...}' 这样的构造中的任何内容 以下是您的代码中的一些问题:

  1. std::cin &gt;&gt; funct 只会读取一个单词(空格分隔的文本)。如果要获取整行,请使用std::getline(std::cin, yourString)

  2. 您查找“文本”的出现,但不保存索引:

    if (funct.find("text") != string::npos)
  1. std::string::at() 确实是一种访问字符串元素的安全方法,但是如果不处理,您的程序可能会以 std::out_of_range 异常终止。

  2. std::string::at() 返回的不是字符串,而是一个字符&

    word = funct.at(temp + 5);

这是我的函数实现,它在字符串中找到 text{...} 构造并打印内容:

#include <iostream>
//using reference, so that the object is not copied
void print_text(const std::string& string) {
        //starting and ending points of the needed text
        size_t start = string.find("text"),
        end;
        //if "text" is not found, exit function
        if (start == std::string::npos)
                return;
        //adding to start, so that it points to the last character of "test"
        start += sizeof("text") - 2;
        try {
                //ignoring all the spaces after "text"
                while (isspace(string.at(++start)));
                //testing for the open brace before the text
                if (string.at(start) != '{')
                        return;
                end = start;
                ++start;
                //testing for the closing brace after the text needed
                while (string.at(++end) != '}');
                //printing the substring if all the conditions are met
                std::cout <<  string.substr(start, end - start) << '\n';
        }
        //any time we reach the end of the string, we bail printing nothing
        catch(std::out_of_range) {
                return;
        }
}

int main() {
        print_text(std::string("sample text{test1}"));
        print_text(std::string("sample space text {test2}"));
        print_text(std::string("empty sample text{}"));
        print_text(std::string("sample text{test4} some after"));
        print_text(std::string("sample forgot closing text{test5"));
        print_text(std::string("sample forgot starting texttest6}"));
        print_text(std::string("sample forgot starting text test7}"));
        print_text(std::string("sample extra characters text hi {test8}"));
}

输出:

test1
test2

test4

注意: 您可以在主函数中有效地询问用户输入并在print_text 函数中传递字符串,它会起作用。 请记住,它只会打印第一次出现的 text{},如果您需要找到任意数量的出现,请评论此回复。

【讨论】:

  • “我不太明白您提供的代码应该如何工作” -- 这可能会让您难以回答所提出的问题(“什么可能是错的”,而不是“什么是另一种使用方法”)。
  • @JaMiT。我同意,但它似乎不像一个工作代码,所以至少我可以提供我的实现。有答案总比没有答案好。
  • “有答案总比没有答案好。” -- 这不是How to Answer 中提出的观点(“回答好问题”部分)。
  • @JaMiT。我希望,您会更喜欢编辑后的版本。我是stackoverflow的新手,还不知道所有规则。感谢您的文章并指出我的错误。
猜你喜欢
  • 2013-02-04
  • 2021-04-16
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多