如何擦除字符串中的所有空格? [英] How to erase all spaces in a string?

查看:88
本文介绍了如何擦除字符串中的所有空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



i需要一个非常简单的解决方案来做到这一点:

例如:

 string str =   Alex; 
string result = Alex;



i尝试了擦除和替换的一些方法,但它们不适合我的情况。



实际上;我想要按对象排序对象的向量NAME(第一个字符)。



谢谢!

解决方案

假设您使用的是托管代码,那么修剪方法 [ ^ ]如果您要做的是删除前导和尾随空格,那么这样做它,并解释为什么替换不适合


如果你想在bo修剪空间th以std :: string类对象结尾,你可以使用 std :: string :: find_first_not_of [ ^ ]和std::string::string/find_last_not_of [ ^



有点逻辑和std::string::substr [ ^ ],您可以创建一个与.NET相同的函数 System :: String :: Trim [ ^ ]函数用于托管字符串。



代码应如下所示(未经测试)



 使用 < span class =code-keyword> namespace  std; 

string trim( const string& source, const string& trim_chars )
{
size_t pos1 = source.find_first_not_of(trim_chars);
size_t pos2 = source.find_last_not_of(trim_chars);

// 可选:优化字符串已被裁剪的情况。
if (pos1 == string :: npos&& pos2 == string :: npos){ return 来源; }

if (pos1 == string :: npos){pos1 = 0 ; }
if (pos2 == string :: npos){pos2 = source.length(); }

size_t trimmed_length = pos2 - pos1;
return source.substr(pos1,trimmed_length);
}

string trim( const string& source)
{
string trim_chars( \\\\ n); // 默认情况下您想要使用的是什么。
return trim(source,trim_chars);
}





如果你想删除所有空格,那么你可以在循环中使用类似的函数并构造一个新的字符串在飞行中。基本上,你会发现第一个不是空格的字符和之后的下一个空格并复制该部分然后重复,直到字符串的剩余部分不再包含空格。


http://stackoverflow.com/questions/83439/remove-spaces-from-stdstring-in- c [ ^

hello

i need a very simple solution to do this:
for example :

string str="  Alex       ";
 string result="Alex";


i tried some ways with erase and replace,but they weren''t suitable for my case.

actually;i want sort a vector of Objects by Their NAME(first char).

thanks !

解决方案

Assuming you are using managed code, there is the Trim method[^] If what you are trying to do is remove the leading and trailing spaces, then that will do it, and would explain why replace "wasn''t suitable"


If you want to trim spaces at both ends with std::string class object, you can use std::string::find_first_not_of[^] and std::string::string/find_last_not_of[^.

A bit of logic and std::string::substr[^], you are able to create a function that will do the same as .NET System::String::Trim[^] function does for managed strings.

The code should look like this (not tested):

using namespace std;

string trim(const string &source, const string &trim_chars)
{
  size_t pos1 = source.find_first_not_of(trim_chars);
  size_t pos2 = source.find_last_not_of(trim_chars);

  // Optional: optimize case where the string is already trimmed.
  if (pos1 == string::npos && pos2 == string::npos) { return source; }

  if (pos1 == string::npos) { pos1 = 0; }
  if (pos2 == string::npos) { pos2 = source.length(); }

  size_t trimmed_length = pos2 - pos1;
  return source.substr(pos1, trimmed_length);
}

string trim(const string &source)
{
  string trim_chars(" \t\r\n");  // Whatever you want to uses by default.
  return trim(source, trim_chars);
}



If you want to remove all spaces, then you might uses similar functions in a loop and construct a new string on the fly. Essentially, you would find first char that is not a space and next space after that and copy that part and then repeat until the remainding part of the string does not contains spaces anymore.


http://stackoverflow.com/questions/83439/remove-spaces-from-stdstring-in-c[^]


这篇关于如何擦除字符串中的所有空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆