900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 字符串分割split:将一个字符串通过指定的分隔符分割成若干子串

字符串分割split:将一个字符串通过指定的分隔符分割成若干子串

时间:2023-08-27 18:50:51

相关推荐

字符串分割split:将一个字符串通过指定的分隔符分割成若干子串

1.先来看一下JAVA里的字符串分割代码:

首先从标准输入得到一行数据,数据之间用逗号,分隔。

将这行数据存为字符串,然后调用字符串分割函数split将其分割成字符数组String [] strRating,在定义一个整型数组,将字符数组转换成整数数组:

import java.util.Arrays;import java.util.Scanner;public class Main {public static void main(String[] args){System.out.println("请输入分数,以英文逗号分隔,:");Scanner scanner = new Scanner(System.in);String str = scanner.nextLine();//首先得到一行输入(以逗号分隔的一组数据),字符串形式strString [] strRating=str.split(","); //去掉str的分割逗号,得到字符数组strRatingint [] nums=new int[strRating.length];//整数数组for(int i=0;i<strRating.length;i++){nums[i]=Integer.parseInt(strRating[i]);//将字符数组的元素转换成整数数组}}}

2.C++ 标准库里面没有提供string的分割函数

但Boost库提供了split()函数,前提是要下载Boost库并配置好。所有C++没有提供先成的字符串分割函数。

参考:/faq/sequences/strings/split/

下面利用STL强大的模板函数来实现C++中string的分割函数:

// C++_IOgetline.cpp : 定义控制台应用程序的入口点。//C++ string 分割:///faq/sequences/strings/split/#include "stdafx.h"// extract to string#include <iostream>#include <string>#include<vector>#include<algorithm>using namespace std;/*利用STL实现字符串分割函数SplitString:参数说明:输入:s:待分割的字符串输出:v:分割结果字符串向量输入: c:分隔符字符串注意:代码只能分割单分隔符:如:20 10 30 50(即中间只能空格一个)若空格多个结果不对!*/void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c){std::string::size_type pos1, pos2;pos2 = s.find(c);pos1 = 0;while (std::string::npos != pos2){v.push_back(s.substr(pos1, pos2 - pos1));pos1 = pos2 + c.size();pos2 = s.find(c, pos1);}if (pos1 != s.length())v.push_back(s.substr(pos1));}int main(){std::string dataStr;std::cout << "Please, enter your data(以空格符分隔): " << endl;std::getline(std::cin, dataStr);std::cout << "your data: " << dataStr << "\n";vector<int> dataVec;vector<string> dataString;SplitString(dataStr, dataString, " ");/*vector<string>转为vector<int>:采用std::transform函数/ljp1919/article/details/77450074*/cout << "将字符串向量转换成int型向量:" << endl;vector<int> vecInt;std::transform(dataString.begin(), dataString.end(), std::back_inserter(vecInt), [](const std::string& str) { return std::stoi(str); });//lambda expressionfor (auto ele : vecInt){cout << ele << endl;}system("pause");return 0;}

下面我们重点看一下这个SplitString()函数是怎么实现的:

/*利用STL实现字符串分割函数SplitString:参数说明:输入:s:待分割的字符串输出:v:分割结果字符串向量输入: c:分隔符字符串注意:代码只能分割单分隔符:如:20 10 30 50(即中间只能空格一个)若空格多个结果不对!*/void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c){std::string::size_type pos1, pos2;pos2 = s.find_first_of(c);/*size_type find_first_of(const _Myt& _Right,size_type _Off = 0) const _NOEXCEPT{// look for one of _Right at or after _Offreturn (find_first_of(_Right._Myptr(), _Off, _Right.size()));}*/pos1 = 0;while (std::string::npos != pos2)/*pos=nopos时循环停止*/{v.push_back(s.substr(pos1, pos2 - pos1));pos1 = pos2 + c.size();pos2 = s.find(c, pos1);//pos2找不到分隔符的话就指向(等于)nopos处}if (pos1 != s.length())v.push_back(s.substr(pos1));/*_Myt substr(size_type _Off = 0, size_type _Count = npos) const{// return [_Off, _Off + _Count) as new stringreturn (_Myt(*this, _Off, _Count, get_allocator()));}*/}

其中substr()函数原型:

_Myt substr(size_type _Off = 0, size_type _Count = npos) const{// return [_Off, _Off + _Count) as new stringreturn (_Myt(*this, _Off, _Count, get_allocator()));}

字符串元素的位置类型使用的是size_type类型;

其中:std::string::npos可以看作是字符串的尾部,即最后一个字符,nopos可以等同于迭代器的end()吧?!。

static const size_t npos = -1;

即:Maximum value for size_t

This constant is defined with a value of-1, which becausesize_tis an unsigned integral type, it is the largest possible representable value for this type.

参考[1]/reference/string/string/npos/

public static member constant

<string>

std::string::npos

nposis a static member constant value with the greatest possible value for an element of typesize_t.

This value, when used as the value for alen(orsublen) parameter instring's member functions, means"until the end of the string".

在string的成员函数调用时,如果使用npos作为实参的话,表示直到该字符串的结尾!

As a return value, it is usually used to indicate no matches.

如果作为返回值的话,表面查找等不成功(即:不匹配)

C++ string STL 实现C++的字符串分割函数split

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。