字符串分割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();//首先得到一行输入(以逗号分隔的一组数据),字符串形式str
		String [] strRating=str.split(","); //去掉str的分割逗号,得到字符数组strRating
		int [] 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++没有提供先成的字符串分割函数。

参考:http://www.cplusplus.com/faq/sequences/strings/split/

 

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

// C++_IOgetline.cpp : 定义控制台应用程序的入口点。

//C++ string 分割:
//http://www.cplusplus.com/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函数
	https://blog.csdn.net/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 expression

	for (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 _Off
			return (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 string
			return (_Myt(*this, _Off, _Count, get_allocator()));
		}
	*/
}

 

其中substr()函数原型:

	_Myt substr(size_type _Off = 0, size_type _Count = npos) const
		{	// return [_Off, _Off + _Count) as new string
		return (_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 because size_t is an unsigned integral type, it is the largest possible representable value for this type.

参考[1]http://www.cplusplus.com/reference/string/string/npos/

public static member constant

<string>

std::string::npos

npos is a static member constant value with the greatest possible value for an element of type size_t.

This value, when used as the value for a len (or sublen) parameter in string'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 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,可以使用字符串split()方法来按照指定分隔符将一个字符串拆分多个列,并存储在一个列表中。 例如,假设我们有一个字符串s,内容为"apple,banana,orange,mango",我们想按照逗号","将它拆分多个列。可以使用以下代码来实现: s = "apple,banana,orange,mango" columns = s.split(",") split()方法将字符串按照逗号进行拆分,并将拆分后的存储在一个列表中。在上述代码中,拆分的结果会保存在columns这个列表中,该列表的内容分别为"apple"、"banana"、"orange"和"mango"。 你也可以使用其他的分隔符,比如空格、分号等,只需要在split()方法的参数中传入对应的分隔符即可实现。例如,如果我们有一个字符串s,内容为"red;blue;green;yellow",我们想按照分号";"将其拆分多个列,可以使用以下代码: s = "red;blue;green;yellow" columns = s.split(";") 拆分的结果会保存在columns列表中,内容分别为"red"、"blue"、"green"和"yellow"。 需要注意的是,split()方法默认情况下会按照空格作为分隔符进行拆分。如果不希望使用任何分隔符,可以直接调用空字符串""作为参数,如下所示: s = "hello world" columns = s.split("") 这样会将字符串s中的每个字符都拆分一个列,并存储在columns列表中。拆分的结果为["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]。 总结起来,可以使用Python的split()方法来按照指定分隔符将一个字符串拆分多个列,并将拆分后的存储在一个列表中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值