如何打印大括号之间包含的子字符串?
How to print substring contained between braces?
我正在写一个函数,应该输出字符串
中大括号之间的任何内容
(例如 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;
}
*编辑,'funct' 是原始用户输入,'temp' 只是一个占位符整数。
据我了解,您需要一个函数来解析字符串并打印 'text{...}'
等结构中的任何内容
这是您的代码中的一些问题:
std::cin >> funct
只会读取一个单词(空格分隔的文本)。如果要获取整行,请使用 std::getline(std::cin, yourString)
您查找 'text',但不保存索引:
if (funct.find("text") != string::npos)
std::string::at()
确实是一种访问字符串元素的安全方法,但是如果不处理,您的程序可能会以 std::out_of_range
异常终止。
std::string::at()
returns 不是字符串,而是字符&
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
注意:
您可以在 main 函数中有效地询问用户输入并在 print_text
函数中传递字符串,它会起作用。
请记住,它只会打印 text{}
的第一次出现,如果您需要找到任意数量的出现,请评论此回复。
我正在写一个函数,应该输出字符串
中大括号之间的任何内容
(例如 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;
}
*编辑,'funct' 是原始用户输入,'temp' 只是一个占位符整数。
据我了解,您需要一个函数来解析字符串并打印 'text{...}'
等结构中的任何内容
这是您的代码中的一些问题:
std::cin >> funct
只会读取一个单词(空格分隔的文本)。如果要获取整行,请使用std::getline(std::cin, yourString)
您查找 'text',但不保存索引:
if (funct.find("text") != string::npos)
std::string::at()
确实是一种访问字符串元素的安全方法,但是如果不处理,您的程序可能会以std::out_of_range
异常终止。std::string::at()
returns 不是字符串,而是字符&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
注意:
您可以在 main 函数中有效地询问用户输入并在 print_text
函数中传递字符串,它会起作用。
请记住,它只会打印 text{}
的第一次出现,如果您需要找到任意数量的出现,请评论此回复。