Linux string manipulation function _ Android

Source: Internet
Author: User
Tags function definition strcmp
[Zz]linux string manipulation functionPublished 1 years ago (1) strcat (connecting two strings)

Related functions

bcopy,memccpy,memcpy,strcpy,strncpy

Header file

#include <string.h>

function definition

Char *strcat (char *dest,const char *src);

Function description

strcat () copies the parameter src string to the end of the string referred to by the parameter dest. The first argument dest to have enough space to hold the string to be copied.

return value

Returns the string start address of the parameter dest

Example

#include <string.h>

#include <stdio.h>

Main ()

{

Char a[30]= "string (1)";

Char b[]= "string (2)";

printf ("Before strcat ():%s\n", a);

printf ("After strcat ():%s\n", strcat (a,b));

}

Perform

Before Strcat (): string (1)

After strcat (): String (1) string (2)

 

(2) STRCHR (find the first occurrence of the specified character in the string)

Related functions

Index,memchr,rinex,strbrk,strsep,strspn,strstr,strtok

Header file

#include <string.h>

function definition

char * STRCHR (const char *s,int c);

Function description

STRCHR () is used to find the first parameter C address that appears in the parameter S string, and then returns the address where the character appears.

return value

If the specified character is found, the address of the character is returned, or 0 is returned.

Example

#include <string.h>

#include <stdio.h>

Main ()

{

Char *s= "0123456789012345678901234567890";

Char *p;

P=STRCHR (S, ' 5 ');

if (p)

{

printf ("%s\n", p);

while ((p) && (* (p+1)!= ' "))

{

P=STRCHR (p+1, ' 5 ');

if (p)

{

printf ("%s\n", p);

}

}

Perform

56789012345678901234567890

5678901234567890

567890

 

(3) strcmp (comparison string)

Related functions

Bcmp,memcmp,strcasecmp,strncasecmp,strcoll

Header file

#include <string.h>

function definition

int strcmp (const char *s1,const char *S2);

Function description

strcmp () is used to compare parameter S1 and S2 strings. The comparison of string sizes is based on the order of the ASCII code table, which is also the value of the character. strcmp () First S1 the first character value minus S2 First character value, if the difference is 0 then continue to compare the next character, if the difference is not 0 will return the difference value. For example, the string "Ac" and "ba" Comparisons return the Difference (-33) of the character "A" (65) and ' B ' (98).

return value

Returns 0 if the parameter S1 and the S2 string are the same. S1 returns a value greater than 0 if it is greater than S2. S1 returns a value less than 0 if it is less than S2.

Example

#include <string.h>

#include <stdio.h>

Main ()

{

Char *a= "ABcDeF";

Char *b= "AbCdEf";

Char *c= "Aacdef";

Char *d= "ABcDeF";

printf ("strcmp (a,b):%d\n", strcmp (a,b));

printf ("A-b =%d\n", (*a)-(*b));

printf ("strcmp (a,c):%d\n", strcmp (A,c));

printf ("strcmp (a,d):%d\n", strcmp (a,d));

}

Perform

strcmp (a,b): 1

A-b = 32

strcmp (A,C):-1

strcmp (a,d): 0

 

(4) Strcoll (using the character order of the current region to compare strings)

Related functions

strcmp,bcmp,memcmp,strcasecmp,strncasecmp

Header file

#include <string.h>

Defining functions

int strcoll (const char *S1, const char *S2);

Function description

Strcoll () compares the S1 and S2 strings according to the text order specified by the environment variable lc_collate.

return value

Returns 0 if the parameter S1 and the S2 string are the same. S1 returns a value greater than 0 if it is greater than S2. S1 returns a value less than 0 if it is less than S2.

Additional Instructions

If Lc_collate is "POSIX" or "C", the Strcoll () is exactly the same as strcmp ().

Example

Reference strcmp ().

 

(5) strcpy (copy string)

Related functions

Bcopy,memcpy,memccpy,memmove

Header file

#include <string.h>

Defining functions

Char *strcpy (char *dest,const char *src);

Function description

strcpy () copies the parameter SRC string to the address that the parameter dest refers to.

return value

Returns the string start address of the parameter dest.

Additional Instructions

If the parameter dest is not large enough in memory, it may cause a buffer overflow (buffer Overflow) error condition, please pay special attention when writing the program, or replace it with strncpy ().

Example

#include <string.h>

#include <stdio.h>

Main ()

{

Char a[30]= "string (1)";

Char b[]= "string (2)";

printf ("Before strcpy ():%s\n", a);

printf ("After strcpy ():%s\n", strcpy (a,b));

}

Perform

Before strcpy (): string (1)

After strcpy (): String (2)

(6) Strcspn (returns the number of characters in a string that do not contain the specified string content)

Related functions

Strspn

Header file

#include <string.h>

Defining functions

size_t strcspn (const char *s,const char * reject);

Function description

STRCSPN () computes contiguous characters from the beginning of the argument s string, none of which are entirely in the string referred to by the parameter reject. Simply put, if STRCSPN () returns a value of N, then the string s begins with n characters that do not contain characters within the string reject.

return value

Returns the number of characters in the beginning of the string s that do not contain the string reject.

Example

#include <string.h>

#include <stdio.h>

Main ()

{

Char *str= "Linux was-a-developed for 386/486-based PCs."

printf ("%d\n", strcspn (str, ""));

printf ("%d\n", strcspn (str, "/-"));

printf ("%d\n", strcspn (str, "1234567890"));

}

Perform

5

33

30

 

(7) StrDup (copy string)

Related functions

Calloc,malloc,realloc,free

Table header File

#include <string.h>

Defining functions

char * strdup (const char *s);

Function description

StrDup () Configures the same space size as the parameter S string first with Maolloc (), and then copies the contents of the parameter S string to the memory address and returns the address. The address can finally be released using free ().

return value

Returns a string pointer to the copied new string address. Returning NULL indicates insufficient memory.

Example

#include <string.h>

#include <stdio.h>

Main ()

{

Char a[]= "StrDup";

Char *b;

B=strdup (a);

printf ("b[]="%s "\ n", b);

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.