如何从const char *类型字符串中删除新线字符
How to remove the new-line characters from const char * type string
我想从字符串中删除新系列字符。
到目前为止#include <iostream>
#include <algorithm>
using namespace std;
int main(){
const char *a = "n remove all n the new line characters n";
cout << a << "n";
remove(a.begin(),a.end(),"n");
cout << a;
}
错误 -
ctest.cpp: In function ‘int main()’:
ctest.cpp:9:11: error: request for member ‘begin’ in ‘a’, which is of non-class type ‘const char*’
remove(a.begin(),a.end(),"n");
^
ctest.cpp:9:21: error: request for member ‘end’ in ‘a’, which is of non-class type ‘const char*’
remove(a.begin(),a.end(),"n");
我还在Internet上找到了remove_if函数,但它仅支持" ISSPACE"," ISDIGIT"过滤器类型。没有" iSnewline",直接" n"正在抛出错误。我还找到了一些传统的解决方案,这些解决方案基本上在整个字符串上都循环了,但是我避免了这些原始解决方案,因为我对这种语言非常陌生。
首先,将其放入数组中,使a
可写,因为您无法修改const char*
的内容。然后使用std::begin
和std::end
非会员函数获得序列的两端:
char a[] = "n remove all n the new line characters n";
cout << a << "n";
remove(std::begin(a), std::end(a), 'n');
cout << a;
演示。
注意:不用说在C 中这样做的更好方法是使用std::string
。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 如何从给定字符串中删除第二次和第三次出现的$
- 在C++的字符串中搜索和删除某些字符
- 从 C 样式字符串中删除子字符串 "in place" 在C++代码中
- 使用另一个字符串从字符串中删除空格
- 删除/替换C++字符串中的多字符 (ÿû)
- 删除所有字符串后如何恢复 QStringList?
- 如何删除字符串中的前 2 个字符(如果它有"-")
- 根据新的行分隔符从字符串中删除子字符串
- C ++,如何从控制台中输入的字符串中删除字母?
- 删除字符串后C++检测到堆损坏
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 如何在 c++ 中删除字符串中的某些字符?
- 删除字符串数组
- 删除字符串中具有相同值的任何相邻字母"pair"
- 高效的字符串截断算法,按顺序删除相等的前缀和后缀
- 如何使用双黑斜杠从字符串中删除单反斜杠
- 如何在编译时从string_view中删除子字符串?
- 从文件中读取多行.txt字符串删除空格并创建新文件进行输出
- c字符串 删除非字母/非空格字符 - C++