• 主页

# 请各位帮我看看做一个字符串相关的函数！分数多多！谢谢！！！

sintenb 2002-07-30 09:40:48

CString A[2];
A[0] = "ABC";
A[1] = "XYZ";

CString Str = "12ABC345XYZ67890";

MyData[5]，被分割为5份："12"、"ABC"、"345"、"XYZ"、"67890"，就是碰到A数组里有的就拆开。

MyData[0] 的值为 "12"
MyData[1] 的值为 "ABC"
MyData[2] 的值为 "345"
MyData[3] 的值为 "XYZ"
MyData[4] 的值为 "67890"

oicq:104140055
...全文
8 点赞 收藏 17

17 条回复

sintenb 2002-07-31

punpuny 2002-07-31

A[0] = "ABCD";
A[1] = "BC"

punpuny 2002-07-31
const int nDim = 4;

CString A[nDim];
CList<CString, CString> list;
int start = 0, nLen = str.GetLength(), index;
int temppos = 0, pos = 0;

A[0] = "XYZ";
A[1] = "ABC";
A[2] = "sf";
A[3] = "bcd";

do
{
temppos = 0;
index = 0;
pos = nLen -1;

// Find the minimize split position
for (int i = 0; i < nDim; i++)
{
temppos = str.Find(A[i], start);

// The current split position is less than pos, record the current position
if (temppos < pos && temppos > -1)
{
pos = temppos;
index = i;
}
}

if (pos != nLen -1)
{
start = pos + A[index].GetLength();
}
else
{
}
} while (start < nLen && pos != nLen -1);

sintenb 2002-07-31

int MyData_Num = 0;
CString tmpString;
int temp1 = Str.Find(A[0]);
//int temp2 = Str.Find(A[1]);
while (temp1!=-1)
{
tmpString = Str.Left(temp1);
temp2 = tmpString.Find(A[1]); // 判断该串中是否有 A[1]
if (temp2==-1)
{
MyData[MyData_Num] = Str.Left(temp1);
MyData_Num++;
Str = Str.Right(Str.GetLength()-temp1);
MyData[MyData_Num] = A[0];
MyData_Num++;
Str = Str.Right(Str.GetLength()-A[0].GetLength());
}
else
{
while (temp2!=-1)
{
MyData[MyData_Num] = tmpString.Left(temp2);
MyData_Num++;
tmpString = tmpString.Right(tmpString.GetLength()-temp2);
MyData[MyData_Num] = A[1];
MyData_Num++;
tmpString = tmpString.Right(tmpString.GetLength()-A[1].GetLength());
temp2 = tmpString.Find(A[1]);
}
if (tmpString.GetLength()!=0)
{
MyData[MyData_Num] = tmpString;
MyData_Num++;
}
}
temp1 = Str.Find(A[0]);
}

sinten 2002-07-31

punpuny 2002-07-31

peterguan 2002-07-31
to punpuny

sintenb 2002-07-31
punpuny() 能留个qq号吗， 交个朋友:)

sintenb 2002-07-31

punpuny 2002-07-31

sintenb 2002-07-31

sintenb 2002-07-31
to punpuny
nDim 不是已知道的

rokia 2002-07-30

while(Str.GetLength()>0)
{
int a=Str.Find(A[0]);
int b=Str.Find(A[1]);

if(a==-1 && b==-1)
{
MessageBox(Str);
Str="";
}
else if((a==-1)||(b<a))
{
if(b==0)
b=A[1].GetLength();
tmp=Str.Left(b);
Str=Str.Right(Str.GetLength()-b);
MessageBox(tmp);
}
else if((b==-1)||(a<b))
{
if (a==0)
a=A[0].GetLength();
tmp=Str.Left(a);
Str=Str.Right(Str.GetLength()-a);
MessageBox(tmp);
}
}

mooncat2000 2002-07-30

VC/MFC

1.5w+

VC/MFC相关问题讨论