String Library
mikroBasic PRO for dsPIC30/33 and PIC24 includes a library which automatizes string related tasks.
Library Functions
- memchr
- memcmp
- memcpy
- memmove
- memset
- strcat
- strcat2
- strchr
- strcmp
- strcpy
- strlen
- strncat
- strncpy
- strspn
- strncmp
- strstr
- strcspn
- strpbrk
- strrchr
- ltrim
- rtrim
- strappendpre
- strappendsuf
- length
memchr
Prototype |
sub function memchr(dim p as ^byte, dim ch as byte, dim n as word) as word |
---|---|
Description |
The function locates the first occurrence of the byte For the parameter |
Example |
txt = "mikroElektronika" res = memchr(@txt, "e", 16) ' example locates first occurrence of the letter 'e' in the string 'txt' in the first 16 characters of the string |
memcmp
Prototype |
sub function memcmp(dim p1, p2 as ^byte, dim n as word) as integer |
---|---|
Description |
The function returns a positive, negative, or zero value indicating the relationship of first This function compares two memory areas starting at addresses Value Meaning < 0 p1 "less than" p2 = 0 p1 "equal to" p2 > 0 p1 "greater than" p2 The value returned by the function is determined by the difference between the values of the first pair of words that differ in the strings being compared. For parameters |
Example |
txt = "mikroElektronika" txt_sub = "mikro" res = memcmp(@txt, @txt_sub, 16) ' returns 69, which is ASCII code of the first differing character - letter 'E' |
memcpy
Prototype |
sub procedure memcpy(dim p1, p2 as ^byte, dim nn as word) |
---|---|
Description |
The function copies For parameters |
Example |
txt = "mikroElektronika" txt_sub = "mikr" memcpy(@txt+4, @txt_sub, 4) ' string 'txt' will be populated with the first 4 characters of the 'txt_sub' string, beginning from the 4th character |
memmove
Prototype |
sub procedure memmove(dim p1, p2, as ^byte, dim nn as word) |
---|---|
Description |
The function copies For parameters |
Example |
txt = "mikroElektronika" txt_sub = "mikr" memmove(@txt+7, @txt_sub, 4) ' string 'txt' will be populated with first 4 characters of the 'txt_sub' string, beginning from the 7th character |
memset
Prototype |
sub procedure memset(dim p as ^byte, dim character as byte, dim n as word) |
---|---|
Description |
The function fills the first For parameter |
Example |
txt = "mikroElektronika" memset(@txt, "a", 2) ' routine will copy the character 'a' into each of the first 'n' characters of the string 'txt", |
strcat
Prototype |
sub procedure strcat(dim byref s1, s2 as string) |
---|---|
Description |
The function appends the value of string |
Example |
txt = "mikroElektronika" txt[3] = 0 strcat(txt, "_test") ' routine will append the '_test' at the place of the first null character, adding terminating null character to the result |
strcat2
Prototype |
sub procedure strcat2(dim byref l, s1, s2 as string) |
---|---|
Description |
The procedure adjoins string |
Example |
dim txt as string[16] txt_sub as string[5] l as string[21] strcat2(l, txt, txt_sub) ' routine will adjoin strings txt and txt_sub and place the result into l |
strchr
Prototype |
sub function strchr(dim byref s as string, dim ch as byte) as word |
---|---|
Description |
The function searches the string The function returns the position (index) of the first character |
Example |
txt = "mikroElektronika" res = strchr(txt, "E") ' routine will locate the character 'E' in the 'txt' string, and return the position of the character |
strcmp
Prototype |
sub function strcmp(dim byref s1, s2 as string) as integer |
---|---|
Description |
The function lexicographically compares the contents of the strings Value Meaning < 0 s1 "less than" s2 = 0 s1 "equal to" s2 > 0 s1 "greater than" s2 The value returned by the function is determined by the difference between the values of the first pair of words that differ in the strings being compared. |
Example |
txt = "mikroElektronika" txt_sub = "mikr" res = strcmp(txt,txt_sub) ' compares strings 'txt' and 'txt_sub' and returns returns a difference between the first differing characters, in this case 69 |
strcpy
Prototype |
sub procedure strcpy(dim byref s1, s2 as string) |
---|---|
Description |
The function copies the value of the string |
Example |
txt = "mikroElektronika" txt_sub = "mikr" strcpy(txt,txt_sub) ' copies string 'txt_sub' to 'txt' |
strlen
Prototype |
sub function strlen(dim byref s as string) as word |
---|---|
Description |
The function returns the length, in words, of the string |
Example |
txt = "mikroElektronika" res = strlen(txt) ' calculates the length of the 'txt' string, result = 16 |
strncat
Prototype |
sub procedure strncat(dim byref s1, s2 as string, dim size as word) |
---|---|
Description |
The function appends at most |
Example |
txt = "mikroElektronika" txt_sub = "mikr" txt[5] = 0 strncat(txt,txt_sub,4) ' routine appends first 4 characters from the string 'txt_sub' at the place of first null character in the 'txt' string |
strncpy
Prototype |
sub procedure strncpy(dim byref s1, s2 as string, dim size as word) |
---|---|
Description |
The function copies at most |
Example |
txt = "mikroElektronika" txt_sub = "mikr" strncpy(txt,txt_sub,4) ' copies first 4 characters form the string 'txt_sub' to 'txt' |
strspn
Prototype |
sub function strspn(dim byref s1, s2 as string) as word |
---|---|
Description |
The function searches the string The function returns the index of first character located in |
Example |
txt = "mikroElektronika" txt_sub = "mikr" res = strspn(txt,txt_sub) ' routne returns 4 |
strncmp
Prototype |
sub function strncmp(dim byref s1, s2 as string, dim len as byte) as integer |
---|---|
Description |
The function lexicographically compares the first Value Meaning < 0 s1 "less than" s2 = 0 s1 "equal to" s2 > 0 s1 "greater than" s2 The value returned by the function is determined by the difference between the values of the first pair of words that differ in the strings being compared (within first |
Example |
txt = "mikroElektronika" txt_sub = "mikr" res = strncmp(txt_sub,txt,3) ' compares the first 3 characters from the string 'txt' with the sting 'txt_sub' and returns a difference |
strstr
Prototype |
sub function strstr(dim byref s1, s2 as string) as word |
---|---|
Description |
The function locates the first occurrence of the string The function returns a number indicating the position of the first occurrence of |
Example |
txt = "mikroElektronika" txt_sub = "mikr" res = strstr(txt, txt_sub) |
strcspn
Prototype |
sub function strcspn(dim byref s1, s2 as string) as word |
---|---|
Description |
The function searches the string The function returns the index of the first character located in |
Example |
txt = "mikroElektronika" txt_sub = "mikr" res = strcspn(txt_sub,txt) |
strpbrk
Prototype |
sub function strpbrk(dim byref s1, s2 as string) as word |
---|---|
Description |
The function searches |
Example |
txt = "mikroElektronika" txt_sub = "mikr" res = strpbrk(txt_sub,txt) |
strrchr
Prototype |
sub function strrchr(dim byref s as string, dim ch as byte) as word |
---|---|
Description |
The function searches the string |
Example |
txt = "mikroElektronika" res = strrchr(txt,"k") ' returns the index of the 'k' character of the 'txt' string |
ltrim
Prototype |
sub procedure ltrim(dim byref astring as string) |
---|---|
Description |
The procedure trims the leading spaces of the string. |
Example |
txt = " mikroE" ltrim(txt) ' trims the leading 2 spaces of the 'txt' string |
rtrim
Prototype |
sub procedure rtrim(dim byref astring as string) |
---|---|
Description |
The procedure trims the trailing spaces of the string. |
Example |
txt = "mikroE " rtrim(txt) ' trims the trailing 2 spaces of the 'txt' string and adds terminating null character to the result |
strappendpre
Prototype |
sub procedure strappendpre(dim letter as char, dim byref s1 as string) |
---|---|
Description |
The procedure appends character at the beginning of the string. |
Example |
txt = "ikroE" strappendpre("m",txt) ' adds letter 'm' at the beginning of the 'txt' string |
strappendsuf
Prototype |
sub procedure strappendsuf(dim byref s1 as string, dim letter as char) |
---|---|
Description |
The procedure appends character at the end of the string. |
Example |
txt = "mikro" strappendsuf("E",txt) ' adds letter 'E' at the end of the 'txt' string |
length
Prototype |
sub function length(dim byref s as string) as word |
---|---|
Description |
The function returns length of passed string. |
Example |
txt = "mikroE" res = length(txt) ' calculates and returns the length of the 'txt' string |
What do you think about this topic ? Send us feedback!