怎么把字符串分开

x1x5x9 2009-07-13 03:17:13
我得到192.168.0.1字符串 怎么样才能提取最后一个点后的数字?
...全文
282 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yayafu 2009-07-14
  • 打赏
  • 举报
回复
这都拿来问,根本不是技术问题,是懒,就那么几个点还找不到了吗?从后面网前找,找到的第一个.不就是了吗?
chehw 2009-07-14
  • 打赏
  • 举报
回复
CHAR szIp[]="192.168.0.1";
CHAR *pCursor=strrchr(szIp, '.');
int nValue=-1;
if(NULL!=*pCursor) nValue=atol(pCursor+1);
roman_v 2009-07-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huangh123 的回复:]
你写的已经很清楚了啊!
就是找字符串中的'.',找到第三个'.',取后面的数字即可.
[/Quote]


找第三个和找最后一个实现起来可不是一回事~~
beautyloveme 2009-07-14
  • 打赏
  • 举报
回复
找了个比较通用的
/*
strContent:准备分离的字符串,例如:a;b;c;d
arString:分离后的数组
nCount:数组大小
cToken:分隔符*/
void Splite(CString strContent, CString arString[], int &nCount, char cToken)
{
int nBegin;
int nEnd;

nBegin = 0;
strContent += cToken;
nEnd = strContent.Find(cToken, 0);
nCount = 0;

while (nEnd >= 0)
{
arString[nCount] = strContent.Mid(nBegin, nEnd - nBegin);
nCount++;
nBegin = nEnd + 1;
nEnd = strContent.Find(cToken, nBegin);
}
}

使用方法
CString strContnt;
CString arContent[100];
int nCount;
strContnt = "你好;谢谢;chendana;大拿万能老板键";
Splite(strContnt, arContent, nCount, ';');
for (int i = 0; i < nCount; i++)
{
AfxMessageBox(arContent[i]);
}

转自:http://www.chendana.com/bbs/viewthread.php?tid=466
dayong576 2009-07-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huangh123 的回复:]
你写的已经很清楚了啊!
就是找字符串中的'.',找到第三个'.',取后面的数字即可.
[/Quote]
难道我的那个不满足你的要求么???

得到的不是你想要的结果么???
Eleven 2009-07-14
  • 打赏
  • 举报
回复
m_ip.GetAddress(BYTE,BYTE,BYTE,BYTE);
danxuezx 2009-07-14
  • 打赏
  • 举报
回复
您的要求很有规律啊
Zhurikeji 2009-07-14
  • 打赏
  • 举报
回复
如果你的字符串是IP地址性质的,mfc中有一个专用函数,具体函数名想不起来啦,查查看
liaoy747 2009-07-14
  • 打赏
  • 举报
回复
12楼正解.
wuda00001 2009-07-14
  • 打赏
  • 举报
回复
#include<iostream>
#include<sstream>
using namespace std;

void main()
{
string str="192.168.0.1";
string str1="";
int j=0,m=0;
char *ch,*ch1;
for(int i=0;i<str.size();i++)
{
ch=str.begin()+i;
if(*ch=='.')j++;
}

for(i=0;i<str.size();i++)
{
ch=str.begin()+i;
if(*ch=='.')m++;

if(m==j)
{
if(i+1>=str.size())break;
ch1=str.begin()+i+1;
str1=str1+*ch1;
}
}
cout<<str1;
}
找到最后一个'.',做标记,然后把最后一个'.'后的内容输出来即可···以上为程序,C++环境下的···
zoulie 2009-07-13
  • 打赏
  • 举报
回复
#include<string.h>
#include<stdlib.h>
#include<iostream.h>
IpToNum(char *ip, int Num[4])
{
char buffer[16];
memcpy(buffer, ip, strlen(ip));
buffer[strlen(ip)] = '\0';
char * pos, *tempPos = buffer;
for(int k = 0; k < 3; k++)
{
pos= strchr(tempPos, '.');
char ipNum[4];
int nLen1 = strlen(tempPos);
int nLen2 = strlen(pos);
for(int j = 0; j < nLen1 - nLen2; j++)
ipNum[j] = tempPos[j];
ipNum[j] = '\0';
int num = atoi(ipNum);
Num[k] = num;
pos = pos + 1;
tempPos = pos;
}
Num[3] = atoi(tempPos);
}

int main()
{
char * str = "223.4.4.202";
int num[4];
IpToNum(str, num);
for(int i = 0; i < 4; i++)
cout<<num[i]<<".";
return 0;
}
huangh123 2009-07-13
  • 打赏
  • 举报
回复
你写的已经很清楚了啊!
就是找字符串中的'.',找到第三个'.',取后面的数字即可.
dayong576 2009-07-13
  • 打赏
  • 举报
回复

CString str= "192.168.0.1";
while(str.Find('.') != -1)
{
str = str.Right(str.Find('.'));
}
快乐鹦鹉 2009-07-13
  • 打赏
  • 举报
回复
CString 的ReverseFind('.'),得到最后一个.的位置。然后用Right函数截取

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧