C language is not as rich as a string operation function as Java. Many useful functions have to be written by themselves. After a day, several common functions have been written, which will be useful in the future.
[CPP]View plaincopy
- # Include <string. h>
- # Include <stdio. h>
- /* Replace the str2 string that appears for the first time in the str1 string with str3 */
- Void replacefirst (char * str1, char * str2, char * str3)
- {
- Char str4 [strlen (str1) + 1];
- Char * P;
- Strcpy (str4, str1 );
- If (P = strstr (str1, str2 ))! = NULL)/* P points to the first position of str2 in str1 */
- {
- While (str1! = P & str1! = NULL)/* move the str1 pointer to the position of P */
- {
- Str1 ++;
- }
- Str1 [0] = '\ 0';/* converts the value pointed by str1 pointer to/0 to intercept str1 and discard str2 and subsequent content, only content earlier than str2 is retained */
- Strcat (str1, str3);/* concatenate str3 after str1 to form a new str1 */
- Strcat (str1, strstr (str4, str2) + strlen (str2);/* strstr (str4, str2) points to str2 and later content (including str2 ), strstr (str4, str2) + strlen (str2) is to move the Pointer Forward to the strlen (str2) bit, skip str2 */
- }
- }
- /* Replace all str2 in str1 with str3 */
- Void Replace (char * str1, char * str2, char * str3)
- {
- While (strstr (str1, str2 )! = NULL)
- {
- Replacefirst (str1, str2, str3 );
- }
- }
- /* In the SRC string, the string starting from subscript start to end-1 (before end) is saved in DEST (subscript starts from 0 )*/
- Void substring (char * DEST, char * SRC, int start, int end)
- {
- Int I = start;
- If (Start> strlen (SRC) return;
- If (end> strlen (SRC ))
- End = strlen (SRC );
- While (I <End)
- {
- Dest [I-start] = SRC [I];
- I ++;
- }
- Dest [I-start] = '\ 0 ';
- Return;
- }
- /* Returns the index character in SRC */
- Char charat (char * SRC, int index)
- {
- Char * P = SRC;
- Int I = 0;
- If (index <0 | index> strlen (SRC ))
- Return 0;
- While (I <index) I ++;
- Return P [I];
- }
- /* Returns the position of str2 that appears in str1 for the first time (index in the following table).-1 */
- Int indexof (char * str1, char * str2)
- {
- Char * P = str1;
- Int I = 0;
- P = strstr (str1, str2 );
- If (P = NULL)
- Return-1;
- Else {
- While (str1! = P)
- {
- Str1 ++;
- I ++;
- }
- }
- Return I;
- }
- /* Returns the position (subscript) of the last occurrence of str2 in str1.-1 */
- Int lastindexof (char * str1, char * str2)
- {
- Char * P = str1;
- Int I = 0, Len = strlen (str2 );
- P = strstr (str1, str2 );
- If (P = NULL) Return-1;
- While (P! = NULL)
- {
- For (; str1! = P; str1 ++) I ++;
- P = P + Len;
- P = strstr (p, str2 );
- }
- Return I;
- }
- /* Delete the white space (space character and horizontal tab) before the first non-white space character on the left of Str )*/
- Void ltrim (char * Str)
- {
- Int I = 0, J, Len = strlen (STR );
- While (STR [I]! = '\ 0 ')
- {
- If (STR [I]! = 32 & STR [I]! = 9) break;/* 32: space, 9: horizontal tab */
- I ++;
- }
- If (I! = 0)
- For (j = 0; j <= len-I; j ++)
- {
- STR [J] = STR [J + I];/* move the subsequent characters forward and add the deleted blank position */
- }
- }
- /* Delete all blank characters (space characters and horizontal tabs) after the last non-blank character of Str )*/
- Void rtrim (char * Str)
- {
- Char * P = STR;
- Int I = strlen (STR)-1;
- While (I> = 0)
- {
- If (P [I]! = 32 & P [I]! = 9) break;
- I --;
- }
- STR [++ I] = '\ 0 ';
- }
- /* Delete the blank characters at both ends of Str */
- Void trim (char * Str)
- {
- Ltrim (STR );
- Rtrim (STR );
- }
Save as mystr. C, and create the header file mystr. h:
[CPP]View plaincopy
- Extern void replacefirst (char * str1, char * str2, char * str3 );
- Extern void Replace (char * str1, char * str2, char * str3 );
- Extern void substring (char * DEST, char * SRC, int start, int end );
- Extern char charat (char * SRC, int index );
- Extern int indexof (char * str1, char * str2 );
- Extern int lastindexof (char * str1, char * str2 );
- Extern void ltrim (char * Str );
- Extern void rtrim (char * Str );
- Extern void trim (char * Str );
Write another test file test. C:
[CPP]View plaincopy
- # Include <string. h>
- # Include <stdio. h>
- # Include "mystr. H"
- Void main ()
- {
- Char Buf [20] = "012345126 ";
- Char buf2 [10];
- Replacefirst (BUF, "12", "9999 ");
- Printf ("replacefirst: % s/n", Buf );
- Strcpy (BUF, "012345126 ");
- Replace (BUF, "12", "9999 ");
- Printf ("Replace: % s/n", Buf );
- Strcpy (BUF, "01234560 ");
- Substring (buf2, Buf, 2, 5 );
- Printf ("substring: % s/n", buf2 );
- Printf ("charat: % C/N", charat (BUF, 4 ));
- Printf ("indexof: % d/N", indexof (BUF, "234 "));
- Printf ("lastindexof: % d/N", lastindexof (BUF, "0 "));
- Strcpy (BUF, "0123 ");
- Ltrim (BUF );
- Printf ("ltrim: | % S |/N", Buf );
- Strcpy (BUF, "0123 ");
- Rtrim (BUF );
- Printf ("rtrim: | % S |/N", Buf );
- Strcpy (BUF, "0123 ");
- Trim (BUF );
- Printf ("Trim: | % S |/N", Buf );
- Strcpy (BUF ,"");
- Trim (BUF );
- Printf ("trim2: | % S |/N", Buf );
- }
Input in Shell
GCC test. c mystr. C-o Str
./Str
Run.
The result is as follows:
Replacefirst: 09999345126
Replace: 0999934599996
Substring: 234
Charat: 4
Indexof: 2
Lastindexof: 7
Ltrim: | 0123 |
Rtrim: | 0123 |
Trim: | 0123 |
Trim2: |