主要涉及到string类的两个函数find和substr:

find()函数的用法:

原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置。
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos

//find函数返回类型 size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position; //find 函数 返回jk 在s 中的下标位置
position = s.find("jk");
if (position != s.npos) //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
{
cout << "position is : " << position << endl;
}
else
{
cout << "Not found the flag" + flag;
} //find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置
flag = "c";
position = s.find_first_of(flag);
cout << "s.find_first_of(flag) is : " << position << endl; //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
position=s.find("b",);
cout<<"s.find(b,5) is : "<<position<<endl; //查找s 中flag 出现的所有位置。
flag="a";
position=;
int i=;
while((position=s.find_first_of(flag,position))!=string::npos)
{
//position=s.find_first_of(flag,position);
cout<<"position "<<i<<" : "<<position<<endl;
position++;
i++;
} //查找flag 中与s 第一个不匹配的位置
flag="acb12389efgxyz789";
position=flag.find_first_not_of (s);
cout<<"flag.find_first_not_of (s) :"<<position<<endl; //反向查找,flag 在s 中最后出现的位置
flag="";
position=s.rfind (flag);
cout<<"s.rfind (flag) :"<<position<<endl;
}

substr()函数用法:

功能:获得子字符串
返回值:子字符串

string a=s.substr(0,5); 从第0位开始的长度为5的字符串.默认时的长度为从开始位置到尾

string字符串分割,并把子字符串放入数组:

#include <iostream>
#include <vector>
#include <string.h>
using namespace std; vector<string> fenge1(string a,char c)
{
vector<string> b;
int pos=,i,len=a.length();
for(i=;i<len;i++)
{
if(i==&&a[i]==c)
pos=i+;
else if(a[i]==c)
{
b.push_back(a.substr(pos,i-pos));
pos=i+;
}
else if(i==len-)
{
b.push_back(a.substr(pos,i-pos+));
}
}
return b;
} vector<string> fenge2(string a,char c)
{
a=a+c;
int i,len=a.length(),pos;
vector<string> b;
for(i=;i<len;i++)
{
pos=a.find(c,i);
if(pos<len)
{
b.push_back(a.substr(i,pos-i));
i=pos;
}
}
return b;
} int main()
{
string s="12,11,4";
vector<string> a=fenge2(s,',');
for(auto i:a)
cout<<i<<' ';
}

string字符串分割,并把数字子字符串转成int型,放入数组:

用到从c_str(),atoi()函数,字符串转换成int型      int k=atoi(str.substr(i,pos-i).c_str());

#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std; typedef string::size_type sz; int main()
{
string str="11,12,13,14";
string p=",";
str=str+p;
vector<int> a;
sz i,pos,len=str.size();
for(i=;i<len;i++)
{
pos=str.find(p,i);
if(pos<len)
{
int k=atoi(str.substr(i,pos-i).c_str());
a.push_back(k);
i=pos;
}
}
for(int j:a)
cout<<j<<endl;
return ;
}

C++ 字符串分割,并把子字符串转换成int型整数的更多相关文章

  1. C# 中怎么将string转换成int型

    int intA = 0;1.intA =int.Parse(str);2.int.TryParse(str, out intA);3.intA = Convert.ToInt32(str);以上都可 ...

  2. VB中将INT型转换成STRING和从STRING转换成INT型的函数

    CStr 函数示例本示例使用 CStr 函数将一数值转换为 String. Dim MyDouble, MyStringMyDouble = 437.324   ' MyDouble 为 Double ...

  3. ADO 连接数据库,取到VT_DATE型日期转换成 int型

    DATE dt = vDate;(vDate是从数据库取出来的值,类型为_variant_t) COleDateTime odt = COleDateTime(dt); CString strdate ...

  4. String转换成int型

    private boolean judge(String str){ int year = 0; try{ year = Integer.valueOf(str).intValue(); }catch ...

  5. java如何将char类型的数字转换成int型的数字,而不是Ascii

    如何把 char ‘3’ 转为 int 3, 大家应该知道,不能直接转化,那样得到是‘3’的Ascii. 如下面: public class Leet { public static void mai ...

  6. sql 将8位字符串转换成日期型

    将8位字符串转换成日期型,方法如下: ),)

  7. iOS 16进制字符串转换成int十进制

    NSRange rangeErr; rangeErr.location = 6; rangeErr.length = 2; NSString *strings = [value substringWi ...

  8. Linux c字符串中不可打印字符转换成16进制

    本文由 www.169it.com 搜集整理 如果一个C字符串中同时包含可打印和不可打印的字符,如果想将这个字符串写入文件,同时方便打开文件查看或者在控制台中打印出来不会出现乱码,那么可以将字符串中的 ...

  9. java字符串转义,把&lt;&gt;转换成<>等字符【原】

    java字符串转义,把<>转换成<>等字符 使用的是commons-lang3-3.4 中的StringEscapeUtils类 package test; import ja ...

随机推荐

  1. 利用Common-BeanUtils封装请求参数

    一.BeanUtils介绍 commons-beanutils是利用反射机制对JavaBean的属性进行处理,提供了对于JavaBean的各种处理方法.众所周知,一个JavaBean通常包含了大量的属 ...

  2. 牛客网Java刷题知识点之什么是JSP、JSP有哪些优点、JSP的9大内置对象、JSP的四大域对象、JSP的四种范围

    不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?tpId=31&tqId=21175&query=&asc= ...

  3. hdu4578Transformation(线段树多个lz标记)

    这里以3次方来举例讲一下这题的做法,其它维类似. 如果要求某一个值的3次方那么sum = t^3,设t = x+y.那也就是sum = (x+y)^3. 假如我让每个数都加z t = x+y+z,我可 ...

  4. SpringBoot实现登陆拦截

    一.创建interceptor包,在interceptor中创建一个拦截器并实现HandlerInterceptor 代码: @Componentpublic class LoginHandlerIn ...

  5. php 正则符号说明

    preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $pat_array); preg_match_all (&qu ...

  6. Adding other views to UIButton

    Q: I want to add some views to UIButton, for example multiple UILabels, UIImages etc. One I add thos ...

  7. 使用python模拟cookie登陆wooyun

    import urllib2 class SimpleCookieHandler(urllib2.BaseHandler): def http_request(self, req): simple_c ...

  8. lg、ln的表示方法

    c语言中 函数 log(x) 表示是以e为底的自然对数,即 ln(x) 函数 log10(x) 以10为底的对数,即 lg(x) 以其它数为底的对数用换底公式来表示 log(a)/log(b) 函数 ...

  9. WPF知识点全攻略09- 附加属性

    附加属性也是一种特殊的依赖属性. Canvas中的Canvas.Left,Canvas.Top ,DockPanel中DockPanel.Dock等就是附加属性. 更加.NET类属性的写法经验.这个中 ...

  10. Python协程函数

    1 协程函数 1.1 协程函数理解 协程函数就是使用了yield表达式形式的生成器 def eater(name): print("%s eat food" %name) whil ...