Status StrReplace(HString *str1, HString str2, int pos, int length) { Status status; status = StrDelete(str1, pos, length); if (status != OK) return status; return StrInsert(str1, str2, pos); }
/** * @function GUI_W_UsrEntryDelete * @brief Delete the selected string part (if any), or the car corresponding to insert line position * @param void *_g_obj: generic object * @return none */ void GUI_W_UsrEntryDelete(g_obj_st *obj) { uint16_t from, to; usr_entry_st *entry; if(obj != NULL && obj->draw == EntryDraw) { entry = (usr_entry_st *) obj->obj; if(entry->bEditable) { from = _MIN(entry->cursStart, entry->cursStop); to = _MAX(entry->cursStart, entry->cursStop); /*delecte the selection, or car located before the insert line*/ StrDelete(entry->buffer, entry->sizeMax, from, to); /*if there was no user selection, decrease the position of the insert bar*/ if(from == to && from > 0) { entry->cursStart--; entry->cursStop--; } /*always ensure that the resulting insert line is bounded into the string size*/ entry->len = gstrlen(entry->buffer); if(entry->len < entry->cursStart) entry->cursStart = entry->len; /*after a deletion, set insert line to the lower cursor; since insert line -> entry->cursStop = entry->cursStart*/ entry->cursStart = _MIN(entry->cursStart, entry->cursStop); entry->cursStop = entry->cursStart; /*force refresh*/ GUI_ObjSetNeedRefresh(obj, true); } } }
void main() { int i; char c, *p = "God bye!", *q = "God luck!"; HString t, s, r; InitString(t); InitString(s); InitString(r); StrAssign(t, p); printf("串t为"); StrPrint(t); printf("串长为%d,串空否?%d(1:空 0:否)\n", StrLength(t), StrEmpty(t)); StrAssign(s, q); printf("串s为"); StrPrint(s); i = StrCompare(s, t); if (i < 0) c = '<'; else if (i == 0) c = '='; else c = '>'; printf("串s%c串t\n", c); Concat(r, t, s); printf("串t连接串s产生的串r为"); StrPrint(r); StrAssign(s, "oo"); printf("串s为"); StrPrint(s); StrAssign(t, "o"); printf("串t为"); StrPrint(t); Replace(r, t, s); printf("把串r中和串t相同的子串用串s代替后,串r为"); StrPrint(r); ClearString(s); printf("串s清空后,串长为%d,空否?%d(1:空 0:否)\n", StrLength(s), StrEmpty(s)); SubString(s, r, 6, 4); printf("串s为从串r的第6个字符起的4个字符,长度为%d,串s为", s.length); StrPrint(s); StrCopy(t, r); printf("由串r复制得串t,串t为"); StrPrint(t); StrInsert(t, 6, s); printf("在串t的第6个字符前插入串s后,串t为"); StrPrint(t); StrDelete(t, 1, 5); printf("从串t的第1个字符起删除5个字符后,串t为"); StrPrint(t); printf("%d是从串t的第1个字符起,和串s相同的第1个子串的位置\n", Index(t, s, 1)); printf("%d是从串t的第2个字符起,和串s相同的第1个子串的位置\n", Index(t, s, 2)); DestroyString(t); }
/** * T是非空串,用V替换主串中所有与T相等的不重叠的子串 */ Status Replace(SString &S, SString T, SString V) { int pos; pos = 1; pos = Index(S, T, pos); while (0 != pos && ERROR != pos) { //找到了子串 StrDelete(S, pos, T[0]); //先删除子串 StrInsert(S, pos, V); //再插入要替换的串 pos = Index(S, T, pos+V[0]); } return OK; }
void main() { int i; char c,*p="God bye!",*q="God luck!"; HString t,s,r; InitString(&t); /* HString类型必需初始化 */ InitString(&s); InitString(&r); StrAssign(&t,p); printf("串t为: "); StrPrint(t); printf("串长为%d 串空否?%d(1:空 0:否)\n",StrLength(t),StrEmpty(t)); StrAssign(&s,q); printf("串s为: "); StrPrint(s); i=StrCompare(s,t); if(i<0) c='<'; else if(i==0) c='='; else c='>'; printf("串s%c串t\n",c); Concat(&r,t,s); printf("串t联接串s产生的串r为: "); StrPrint(r); StrAssign(&s,"oo"); printf("串s为: "); StrPrint(s); StrAssign(&t,"o"); printf("串t为: "); StrPrint(t); Replace(&r,t,s); printf("把串r中和串t相同的子串用串s代替后,串r为:\n"); StrPrint(r); ClearString(&s); printf("串s清空后,串长为%d 空否?%d(1:空 0:否)\n",StrLength(s),StrEmpty(s)); SubString(&s,r,6,4); printf("串s为从串r的第6个字符起的4个字符,长度为%d 串s为: ",s.length); StrPrint(s); StrCopy(&t,r); printf("复制串t为串r,串t为: "); StrPrint(t); StrInsert(&t,6,s); printf("在串t的第6个字符前插入串s后,串t为: "); StrPrint(t); StrDelete(&t,1,5); printf("从串t的第1个字符起删除5个字符后,串t为: "); StrPrint(t); printf("%d是从串t的第1个字符起,和串s相同的第1个子串的位置\n",Index(t,s,1)); printf("%d是从串t的第2个字符起,和串s相同的第1个子串的位置\n",Index(t,s,2)); }
char *StrTrim(char *szText, const char *szTrimChars) { size_t i = strlen(szText) - 1; while (strchr(szTrimChars, szText[i])) szText[i--] = '\0'; i = 0; while ((i < strlen(szText)) && (strchr(szTrimChars, szText[i]))) i++; if (i) StrDelete(szText, 0, i); return szText; }
wchar_t *StrTrim(wchar_t *szText, const wchar_t *szTrimChars) { size_t i = wcslen(szText) - 1; while (wcschr(szTrimChars, szText[i])) szText[i--] = '\0'; i = 0; while ((i < wcslen(szText)) && (wcschr(szTrimChars, szText[i]))) i++; if (i) StrDelete(szText, 0, i); return szText; }
Status Replace(HString *S, HString T, HString V){ int i = 1, len_t, len_v; if(StrEmpty(*S)){ return ERROR; } len_t = StrLength(T); len_v = StrLength(V); do{ i = Index(*S, T, i); if(i){ StrDelete(S, i, len_t); StrInsert(S, i, V); i += len_v; } }while(i); return OK; }
/* 操作结果: 用V替换主串S中出现的所有与T相等的不重叠的子串 */ Status Replace(String S,String T,String V) { int i=1; /* 从串S的第一个字符起查找串T */ if(StrEmpty(T)) /* T是空串 */ return ERROR; do { i=Index(S,T,i); /* 结果i为从上一个i之后找到的子串T的位置 */ if(i) /* 串S中存在串T */ { StrDelete(S,i,StrLength(T)); /* 删除该串T */ StrInsert(S,i,V); /* 在原串T的位置插入串V */ i+=StrLength(V); /* 在插入的串V后面继续查找串T */ } } while(i); return OK; }
Status Replace(HString &S,HString T,HString V) { int i=1; while(i) { i=Index(S,T,i); if(i) { StrDelete(S,i,T.length); StrInsert(S,i,V); i+=V.length; } } return OK; }
void Replace() { // 替换字符串 int i,k,f=1; // f为继续替换标志 char b[2]; HString s,t; printf("请输入待替换的字符串: "); scanf("%s%*c",str); InitString(s); StrAssign(s,str); printf("替换为: "); scanf("%s%*c",str); InitString(t); StrAssign(t,str); for(i=0;i<n&&f;i++) // 逐行查找、替换 { k=1; // 由每行第1个字符起查找 while(k) { k=Index(T[i],s,k); // 由本行的第k个字符开始查找 if(k) // 找到 { printf("第%d行: ",i+1); StrPrint(T[i]); printf("第%d个字符处找到。是否替换(Y/N)? ",k); gets(b); if(b[0]=='Y'||b[0]=='y') { StrDelete(T[i],k,StrLength(s)); StrInsert(T[i],k,t); } printf("继续替换吗(Y/N)?"); gets(b); if(b[0]!='Y'&&b[0]!='y') // 中断查找、替换 { f=0; break; } else k+=StrLength(t); } } } if(f) printf("没找到\n"); }
static Status Replace(LString &S,LString T,LString V) { // 初始条件: 串S,T和V存在,T是非空串(此函数与串的存储结构无关) // 操作结果: 用V替换主串S中出现的所有与T相等的不重叠的子串 int i=1; // 从串S的第一个字符起查找串T if(StrEmpty(T)) // T是空串 return ERROR; do { i=Index(S,T,i); // 结果i为从上一个i之后找到的子串T的位置 if(i) // 串S中存在串T { StrDelete(S,i,StrLength(T)); // 删除该串T StrInsert(S,i,V); // 在原串T的位置插入串V i+=StrLength(V); // 在插入的串V后面继续查找串T } }while(i); return OK; }
Status Replace(LString *S,LString T,LString V) { /* 初始条件: 串S,T和V存在,T是非空串(此函数与串的存储结构无关) */ /* 操作结果: 用V替换主串S中出现的所有与T相等的不重叠的子串 */ int i=1; /* 从串S的第一个字符起查找串T */ if(StrEmpty(T)) /* T是空串 */ return ERROR; do { i=Index(*S,T,i); /* 结果i为从上一个i之后找到的子串T的位置 */ if(i) /* 串S中存在串T */ { StrDelete(S,i,StrLength(T)); /* 删除该串T */ StrInsert(S,i,V); /* 在原串T的位置插入串V */ i+=StrLength(V); /* 在插入的串V后面继续查找串T */ } }while(i); return OK; }
char *StrReplace(char *source, const char *what, const char *withWhat) { size_t whatLen = strlen(what); size_t withWhatLen = strlen(withWhat); char *pos; while ((pos = strstr(source, what))) { size_t minLen = min(whatLen, withWhatLen); StrCopy(source, pos - source, withWhat, minLen); size_t index = pos - source + minLen; if (whatLen > withWhatLen) StrDelete(source, index, whatLen - withWhatLen); else { if (whatLen < withWhatLen) StrInsert(source, index, withWhat + minLen); } } return source; }
Status Replace(String &S, String T, String V) { int i = 1; Status k; if (StrEmpty(T)) return ERROR; while (i) { i = Index(S, T, i); if (i) { StrDelete(S, i, StrLength(T)); k = StrInsert(S, i, V); if (!k) return ERROR; i += StrLength(V); } } ; return OK; }
void main() { char *s1="ABCDEFGHI",*s2="12345",*s3="",*s4="asd#tr",*s5="ABCD"; Status k; int pos,len; LString t1,t2,t3,t4; InitString(&t1); InitString(&t2); printf("初始化串t1后,串t1空否?%d(1:空 0:否) 串长=%d\n",StrEmpty(t1),StrLength(t1)); k=StrAssign(&t1,s3); if(k==OK) { printf("串t1为: "); StrPrint(t1); } else printf("出错\n"); /* 不能生成空串 */ k=StrAssign(&t1,s4); if(k==OK) { printf("串t1为: "); StrPrint(t1); } else printf("出错\n"); /* 不能生成含有变量blank所代表的字符的串 */ k=StrAssign(&t1,s1); if(k==OK) { printf("串t1为: "); StrPrint(t1); } else printf("出错\n"); printf("串t1空否?%d(1:空 0:否) 串长=%d\n",StrEmpty(t1),StrLength(t1)); StrAssign(&t2,s2); printf("串t2为: "); StrPrint(t2); StrCopy(&t3,t1); printf("由串t1拷贝得到串t3,串t3为: "); StrPrint(t3); InitString(&t4); StrAssign(&t4,s5); printf("串t4为: "); StrPrint(t4); Replace(&t3,t4,t2); printf("用t2取代串t3中的t4串后,串t3为: "); StrPrint(t3); ClearString(&t1); printf("清空串t1后,串t1空否?%d(1:空 0:否) 串长=%d\n",StrEmpty(t1),StrLength(t1)); Concat(&t1,t2,t3); printf("串t1(=t2+t3)为: "); StrPrint(t1); Zip(&t1); printf("去除不必要的占位符后,串t1为: "); StrPrint(t1); pos=Index(t1,t3,1); printf("pos=%d\n",pos); printf("在串t1的第pos个字符之前插入串t2,请输入pos: "); scanf("%d",&pos); k=StrInsert(&t1,pos,t2); if(k) { printf("插入串t2后,串t1为: "); StrPrint(t1); } else printf("插入失败!\n"); printf("求从t1的第pos个字符起,长度为len的子串t2,请输入pos,len: "); scanf("%d,%d",&pos,&len); SubString(&t2,t1,pos,len); printf("串t2为: "); StrPrint(t2); printf("StrCompare(t1,t2)=%d\n",StrCompare(t1,t2)); printf("删除串t1中的子字符串:从第pos个字符起删除len个字符。请输入pos,len:"); scanf("%d,%d",&pos,&len); k=StrDelete(&t1,pos,len); if(k) { printf("从第%d位置起删除%d个元素后串t1为:",pos,len); StrPrint(t1); } }
int main() { int i,j; Status k; char s; String t,s1,s2; printf("请输入串s1: "); k=StrAssign(s1,"abcd"); if(!k) { printf("串长超过MAXSIZE(=%d)\n",MAXSIZE); exit(0); } printf("串长为%d 串空否?%d(1:是 0:否)\n",StrLength(s1),StrEmpty(s1)); StrCopy(s2,s1); printf("拷贝s1生成的串为: "); StrPrint(s2); printf("请输入串s2: "); k=StrAssign(s2,"efghijk"); if(!k) { printf("串长超过MAXSIZE(%d)\n",MAXSIZE); exit(0); } i=StrCompare(s1,s2); if(i<0) s='<'; else if(i==0) s='='; else s='>'; printf("串s1%c串s2\n",s); k=Concat(t,s1,s2); printf("串s1联接串s2得到的串t为: "); StrPrint(t); if(k==FALSE) printf("串t有截断\n"); ClearString(s1); printf("清为空串后,串s1为: "); StrPrint(s1); printf("串长为%d 串空否?%d(1:是 0:否)\n",StrLength(s1),StrEmpty(s1)); printf("求串t的子串,请输入子串的起始位置,子串长度: "); i=2; j=3; printf("%d,%d \n",i,j); k=SubString(s2,t,i,j); if(k) { printf("子串s2为: "); StrPrint(s2); } printf("从串t的第pos个字符起,删除len个字符,请输入pos,len: "); i=4; j=2; printf("%d,%d \n",i,j); StrDelete(t,i,j); printf("删除后的串t为: "); StrPrint(t); i=StrLength(s2)/2; StrInsert(s2,i,t); printf("在串s2的第%d个字符之前插入串t后,串s2为:\n",i); StrPrint(s2); i=Index(s2,t,1); printf("s2的第%d个字母起和t第一次匹配\n",i); SubString(t,s2,1,1); printf("串t为:"); StrPrint(t); Concat(s1,t,t); printf("串s1为:"); StrPrint(s1); Replace(s2,t,s1); printf("用串s1取代串s2中和串t相同的不重叠的串后,串s2为: "); StrPrint(s2); return 0; }
void test() { char ch[2][255]; int i=0,pos=1; CString S[2],T; InitString(S[0]); InitString(S[1]); InitString(T); for(i=0;i<2;i++) { printf("ÊäÈë×Ö·û´®%d:",i+1); scanf("%s",ch[i]); StrAssign(S[i],ch[i]); } for(i=0;i<2;i++) { printf("Êä³ö×Ö·û´®%d:",i+1); StrPrint(S[i]); printf("×Ö·ûÊÇ·ñΪ¿Õ%d(1-·Ç 0-¿Õ),×Ö·û´®³¤¶È:%d\n",!StrEmpty(S[i]),StrLength(S[i])); } printf("---Á¬½Ó´®--\n"); Concat(T,S[0],S[1]); printf("Á¬½Óºó:"); StrPrint(T); printf("---Çó×Ó´®---\n"); printf("ÊäÈë×Ö´®:"); scanf("%s",ch[0]); printf("ÊäÈë²éÕÒλÖÃ:"); scanf("%d",&pos); StrAssign(S[0],ch[0]); i=Index(T,S[0],pos); printf("²éÕÒµ½µÄλÖÃ:%d\n",i); printf("ɾ³ý%sºó:",ch[0]); if(i) StrDelete(T,i,S[0].length); StrPrint(T); printf("²åÈë%sºó:",ch[0]); if(i) StrInsert(T,S[0],i); StrPrint(T); printf("---Ìæ´ú---\n"); printf("ÊäÈëÌæ´úµÄ×Ó´®:"); scanf("%s",ch[1]); printf("Ìæ»»%sºó:",ch[0]); StrAssign(S[1],ch[1]); Replace(T,S[0],S[1]); StrPrint(T); printf("±È½Ï×Ö·û´®:S%d%cS%d\n",1,StrCompare(S[0],S[1])>0?'>':StrCompare(S[0],S[1])<0?'<':'=',2); StrCopy(S[0],S[1]); printf("---¸´Öƺó---\n"); for(i=0;i<2;i++) { printf("Êä³ö×Ö·û´®%d:",i+1); StrPrint(S[i]); printf("×Ö·ûÊÇ·ñΪ¿Õ%d(1-·Ç 0-¿Õ),×Ö·û´®³¤¶È:%d\n",!StrEmpty(S[i]),StrLength(S[i])); } printf("±È½Ï×Ö·û´®:S%d%cS%d\n",1,StrCompare(S[0],S[1])>0?'>':StrCompare(S[0],S[1])<0?'<':'=',2); ClearString(S[0]); ClearString(S[1]); ClearString(T); }
int main() { int i, j, opp, pos; char s, str; String t,s1,s2,sub; Status k; printf("\n1.StrAssign 生成串 \n2.StrLength 求串长 \n3.StrCompare 串比较 "); printf("\n4.Concat 串连接 \n5.SubString 求子串 \n6.Index 求子串位置"); printf("\n7.StrInsert 子串插入 \n8.StrDelete 子串删除 \n9.Replace 子串替换"); printf("\n0.退出 \n请选择你的操作:\n"); while(opp != '0') { scanf("%d",&opp); switch(opp) { case 1: k=StrAssign(s1,"nowamagic.net"); if(!k) { printf("串长超过MAXSIZE(=%d)\n",MAXSIZE); exit(0); } printf("串s1为:"); StrPrint(s1); printf("\n"); break; case 2: printf("串s1长为%d \n",StrLength(s1)); break; case 3: k=StrAssign(s2,"google.com"); if(!k) { printf("串长超过MAXSIZE(%d)\n",MAXSIZE); exit(0); } printf("串s2为:"); StrPrint(s2); printf("\n"); i=StrCompare(s1,s2); if(i < 0) s=' < '; else if(i==0) s='='; else s='>'; printf("串s1%c串s2\n",s); break; case 4: Concat(t,s1,s2); StrPrint(t); break; case 5: printf("求串s1的子串,请输入子串的起始位置: "); scanf("%d", &i); printf("请输入子串的长度: "); scanf("%d", &j); printf("起始位置:%d,子串长度:%d\n", i, j); k=SubString(sub,s1,i,j); if(k) { printf("子串sub为: "); StrPrint(sub); } break; case 6: printf("主串s1为: "); StrPrint(s1); k=StrAssign(sub,"magic"); printf("子串sub为: "); StrPrint(sub); i=Index(s1,sub,1); printf("s1的第%d个字母起和sub第一次匹配\n",i); break; case 7: printf("主串s1为: "); StrPrint(s1); k=StrAssign(sub,"lol"); printf("子串sub为: "); StrPrint(sub); printf("请输入要插入的位置: "); scanf("%d", &pos); StrInsert(s1,pos,sub); StrPrint(s1); break; case 8: printf("从串s1的第pos个字符起,删除len个字符,请输入pos: \n"); scanf("%d", &i); printf("再输入len: \n"); scanf("%d", &j); StrDelete(s1,i,j); StrPrint(s1); break; case 9: printf("主串s1为: "); StrPrint(s1); StrAssign(t,"a"); printf("串t为:"); StrPrint(t); StrAssign(sub,"aa"); printf("串sub为:"); StrPrint(sub); printf("用串s2取代串s1中和串t相同的不重叠的串后,串s1为: "); Replace(s1,t,sub); StrPrint(s1); break; case 0: exit(0); } } }