C++ 如何使用 string.substr() 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2477850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use string.substr() function?
提问by VaioIsBorn
I want to make a program that will read some number in string format and output it like this: if the number is 12345 it should then output 12 23 34 45 . I tried using the substr() function from the c++ string library, but it gives me strange results - it outputs 1 23 345 45 instead of the expected result. Why ?
我想制作一个程序,它会以字符串格式读取一些数字并像这样输出它:如果数字是 12345 它应该输出 12 23 34 45 。我尝试使用 c++ 字符串库中的 substr() 函数,但它给了我奇怪的结果 - 它输出 1 23 345 45 而不是预期的结果。为什么 ?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(void)
{
string a;
cin >> a;
string b;
int c;
for(int i=0;i<a.size()-1;++i)
{
b = a.substr(i,i+1);
c = atoi(b.c_str());
cout << c << " ";
}
cout << endl;
return 0;
}
回答by Ghislain Fourny
回答by paxdiablo
As shown here, the second argument to substr
is the length, not the ending position:
如这里所示,第二个参数substr
是长度,而不是结束位置:
string substr ( size_t pos = 0, size_t n = npos ) const;
Generate substring
Returns a string object with its contents initialized to a substring of the current object. This substring is the character sequence that starts at character position
pos
and has a length ofn
characters.
string substr ( size_t pos = 0, size_t n = npos ) const;
生成子串
返回一个字符串对象,其内容初始化为当前对象的子字符串。该子串是从字符位置开始
pos
并具有n
字符长度的字符序列。
Your line b = a.substr(i,i+1);
will generate, for values of i
:
b = a.substr(i,i+1);
对于以下值,您的线路将生成i
:
substr(0,1) = 1
substr(1,2) = 23
substr(2,3) = 345
substr(3,4) = 45 (since your string stops there).
What you need is b = a.substr(i,2);
你需要的是 b = a.substr(i,2);
You should also be aware that your output will look funny for a number like 12045. You'll get 12 20 4 45
due to the fact that you're using atoi()
on the string section and outputting that integer. You might want to try just outputing the string itself which willbe two characters long:
您还应该注意,对于像 12045 这样的数字,您的输出看起来很有趣。12 20 4 45
由于您在atoi()
字符串部分使用并输出该整数,因此您会得到。你可能会想尝试只是outputing字符串本身,这将是两个字符:
b = a.substr(i,2);
cout << b << " ";
In fact, the entire thing could be more simply written as:
事实上,整个事情可以更简单地写成:
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string a;
cin >> a;
for (int i = 0; i < a.size() - 1; i++)
cout << a.substr(i,2) << " ";
cout << endl;
return 0;
}
回答by Rajendra Uppal
Another interesting variant question can be:
另一个有趣的变体问题可以是:
How would you make "12345"
as "12 23 34 45"
without using another string?
你会如何做"12345"
的"12 23 34 45"
,而无需使用另一个字符串?
Will following do?
下面会做吗?
for(int i=0; i < a.size()-1; ++i)
{
//b = a.substr(i, 2);
c = atoi((a.substr(i, 2)).c_str());
cout << c << " ";
}
回答by Shrihari Jadhav
You can get the above output using following code in c
您可以在 c 中使用以下代码获得上述输出
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char *str;
clrscr();
printf("\n Enter the string");
gets(str);
for(int i=0;i<strlen(str)-1;i++)
{
for(int j=i;j<=i+1;j++)
printf("%c",str[j]);
printf("\t");
}
getch();
return 0;
}
回答by lionelmessi
Possible solution without using substr()
不使用的可能解决方案 substr()
#include<iostream>
#include<string>
using namespace std;
int main() {
string c="12345";
int p=0;
for(int i=0;i<c.length();i++) {
cout<<c[i];
p++;
if (p % 2 == 0 && i != c.length()-1) {
cout<<" "<<c[i];
p++;
}
}
}
回答by user1611542
substr(i,j)
means that you start from the index i
(assuming the first index to be 0) and take next j
chars.
It does not mean going up to the index j
.
substr(i,j)
意味着您从索引开始i
(假设第一个索引为 0)并取下一个j
字符。这并不意味着上升到指数j
。
回答by antb52
Possible solution with string_view
string_view 的可能解决方案
void do_it_with_string_view( void )
{
std::string a { "12345" };
for ( std::string_view v { a }; v.size() - 1; v.remove_prefix( 1 ) )
std::cout << v.substr( 0, 2 ) << " ";
std::cout << std::endl;
}