简体   繁体   English

如何在C ++中获取子字符串并在字符串之间添加字符

[英]How to get substring in C++ and add character in between string

I have a string Hello foo how are you. 我有一个字符串Hello foo你好吗?

I want to change it to Hello\\r\\nfoo how are you. 我想将它改为Hello \\ r \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\

I wanted to know the way to get the substring Hello add \\r\\n in place of space and add all other strings as they are. 我想知道获取子字符串Hello add \\ r \\ n代替空格的方法,并按原样添加所有其他字符串。 This is to show the multiline. 这是为了显示多行。

EDIT: 编辑:

We don't know the length of first substring. 我们不知道第一个子串的长度。 We would not know how long the first substring will be. 我们不知道第一个子串会有多长。

Thanks. 谢谢。

To get a substring your answer lies with the function string::substr : 要获得子字符串,您的答案就在于函数string :: substr

string::substr (size_t pos = 0, size_t len = npos) const;
  1. pos parameter is the index of the first character to be copied as a substring. pos参数是要作为子字符串复制的第一个字符的索引。
  2. len parameter is the number of characters to include in the substring starting from the index. len参数是从索引开始包含在子字符串中的字符数。

Returns a newly instantiated string object with its value being the substring from the specified string object it was invoked on. 返回一个新实例化的字符串对象,其值是调用它的指定字符串对象的子字符串。

// Example:
#include <iostream>
#include <string>

int main () {
  std::string str1= "Hello Stack Overflow";
  std::string str2 = str.substr (0,5); // "Hello
  std::cout << str2 << std::endl; // Prints "Hello"

  return 0;

}

UPDATE: However it looks like different from your title, what you're needing for is to change some characters in between not knowing the substring's length 更新:然而它看起来与你的标题不同,你需要的是在不知道子串的长度之间改变一些字符

For that your answer is string::replace : 为此你的答案是string :: replace

string& replace (size_t pos,  size_t len,  const string& str);

Replaces the portion of the string that starts at index pos and goes up to index len. 替换从索引pos开始的字符串部分,然后上升到索引len。

  1. pos parameter is the index of the first character to be replaced. pos参数是要替换的第一个字符的索引。
  2. len parameter is the number of characters to be replaced starting from the index. len参数是从索引开始要替换的字符数。
  3. str string parameter to replace it with. str字符串参数替换它。

    // Example
    int main() 
       std::string str = "Hello Stack Overflow.";
       std::string str2 = "good";
       str.replace(6, 4, str2);   // str = "Hello goodStackOverflow"
       return 0;
    }

In some compilers you might not need to add it, but you will need to include the string header to make sure your code is portable and maintainable with: 在某些编译器中,您可能不需要添加它,但是您需要包含字符串标头以确保您的代码可移植且可维护:

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

int main() {
  std::string s = "Hello foo how are you.";
  s.replace(s.find_first_of(" "),1,"\r\n");
  std::cout << s << std::endl; #OUTPUTS: "Hello
                               #          foo how are you."
  return 0;
}

What you want to use here is the string::replace(pos,len,insert_str); 你想在这里使用的是string::replace(pos,len,insert_str); , this function allows you to replace a specified sub-string in s with your "\\r\\n" . ,此函数允许您用"\\r\\n"替换s的指定子字符串。

Edit: You want to use s.find_first_of(str) to find the first occurrence of the string " " 编辑:您想使用s.find_first_of(str)查找字符串" "的第一个匹配项

Not a complete answer, because this looks like homework, but other approaches you could use include std::find() in <algorithm> and strchr() in <string.h> . 不是一个完整的答案,因为这看起来像家庭作业,但你可以使用的其他方法包括<algorithm> std::find()<string.h> strchr() If you need to search for any whitespace and not just the ' ' character, you might use std::find_first_of() or strcspn() . 如果需要搜索任何空格而不仅仅是' '字符,可以使用std::find_first_of()strcspn()

In the future, I'd check out the documentation for: the member functions of std::basic_string , the utility functions in <string> , the functions in <algorithm> , and the functions in <string.h> , as those will generally be the tools you have to work with. 将来,我将查看以下文档: std::basic_string的成员函数, <string>的实用程序函数, <algorithm>的函数以及<string.h>的函数,因为这些将是通常是您必须使用的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM