字串的操作

2021-07-09 04:07:46 字數 1919 閱讀 9103



#pragma once

#define _string_h_

#include

#include

using namespace std;

#define maxsize 255

typedef struct

sstring;

void initstring(sstring *s)

//給字串賦值

void strassign(sstring *s, char *ch)

s->ch[i] = '\0';

}//將串s清為空串

void clearstr(sstring *s)

//比較兩個字串的大小

int strcompare(sstring s, sstring t)

else if (s.ch[i] < t.ch[i])

else

}if (s.ch[i] == '\0' && t.ch[i] == '\0')

if (s.ch[i] == '\0')

if (t.ch[i] == '\0')

}//拷貝字串

void strcopy(sstring *s, sstring t)

s->ch[i] = '\0';

}//求字串的長度

int strlength(sstring s)

//銷毀字串

void destorystring(sstring *s)

//字串的鏈結

void strconcat(sstring *t, sstring s1, sstring s2)

while (i <= t->ch[0] && s2.ch[j] != '\0')

t->ch[i] = '\0';

}//判斷字串是否為空

bool strempty(sstring s)

return false;

}//返回s中從pos起長度為len的子串

void substring(sstring *sub, sstring s,unsigned  int pos, int len)

}sub->ch[j] = '\0';

}int getlen(sstring s)

return i-1;

}//查詢pos後首次出現子串的位置

int  indexpos(sstring s,sstring t,int pos)

else

}if(j > lent)

}return -1;

}return -1;

}//用v替換主串中的t子串

void replace(sstring *s, sstring t, sstring v)

}i = pos + lenv;

}//插入子字串

bool strinsert(sstring *s, unsigned int pos, char *elem)

if(pos + lent > s->ch[0])

s->ch[i] = '\0';

return false;

}for(j;j <= lent;j++)

return true;

}//刪除字串

bool strdelete(sstring *s, int pos, int len)

int i = pos + len+1;

for(i;i <=  lens;i++)

s->ch[i-len] = '\0';

return true;

}//列印字串

void printstr(sstring s)}}

if (j == t->ch[0] - 1)

i = i - j + 2;

}  }

return -1;

}

字串操作 靠字串分割字串

字串分解函式。注意strtok比較複雜。要妥善運用!也可以不用strtok函式,但要實現字串靠字串分割比較困難!注意str指向的空間必須是可讀可寫的 如陣列或動態分配的空間 不能為字串常量的指標,因為strtok改變了其中的內容。include include 功能 將str中的字串按照elemon...

字串的操作

strcpy,sprintf,memcpy的區別 對於字串拷貝來說,其實現的效率和使用的方便程度不同 strcpy 無疑是最合適的選擇 效率高且呼叫方便。snprintf 要額外指定格式符並且進行格式轉化,麻煩且效率不高。memcpy 雖然高效,但是需要額外提供拷貝的記憶體長度這一引數,易錯且使用不...

字串的操作

字串擷取 var ary str.split 字串替換 將所有特殊字元都替換掉 key.replace g,將所有的 都替換為 替換特殊字串中的第乙個 key.replace 字串中查詢特殊字串 字元 str.indexof d 字串長度 str.length 字串擷取 ary explode st...