CAPL脚本常用字符串函数

您所在的位置:网站首页 string赋值给字符数组 CAPL脚本常用字符串函数

CAPL脚本常用字符串函数

#CAPL脚本常用字符串函数| 来源: 网络整理| 查看: 265

Capl中字符串处理API strlen()strncat()strncmp()strncpy()strstr()str_replace()substr_cpy()swapWord()toLower()toUpper() Capl中字符串处理API

strlen()

字符串的长度 在这里插入图片描述

On key 'c' { char buffer[100] = "CANalyzer"; Write("strlen:%d",strlen(buffer)); Write("elCount:%d",elCount(buffer)); } strlen 结果是实际的字符串长度elCount 结果是数组的定义长度

输出: strlen:9 elCount:100

strncat()

将源字符串拼接到到目的字符串后面,注意“\0”也占用一个字节。 在这里插入图片描述

On key 'c' { int i ; char s[10]; for(i=0;i char s1[8] = "123456"; char s2[8] = "223456"; char s3[8] = "1234561"; char s4[8] = "12345"; char s5[8] = "123456"; write("s1:s2 = %d",strncmp(s1,s2,100)); write("s1:s3 = %d",strncmp(s1,s3,100)); write("s1:s4 = %d",strncmp(s1,s4,100)); write("s1:s5 = %d",strncmp(s1,s5,100)); }

输出: s1:s2 = -1 s1:s3 = -1 s1:s4 = 1 s1:s5 = 0 函数的机制是:逐个字符比较,遇见不同的字符,比较其ASCII值,然后返回值。

long strncmp_off(char s1[], long s1offset, char s2[], long s2offset, long len); 相比strncmp ,strncmp_off 多了个offset参数 在这里插入图片描述

On key 'c' { char s1[8] = "123456"; char s2[8] = "456"; write("s1:s2 = %d",strncmp_off(s1,3,s2,0,100));//s1[3:结束]和S2比较 }

输出: s1:s2 = 0

strncpy()

把源字符串拷贝到目的字符串数组,拷贝长度由len参数决定,拷贝过程会覆盖目的字符串数组原来内容。 在这里插入图片描述

On key 'c' { char s1[20] = "123456"; char s2[8] = "789"; strncpy(s1,s2,elCount(s1)); write("s1 = %s",s1); //清空字符串 strncpy(s1,"",elCount(s1)); write("s1 = %s",s1); }

输出: s1 = 789 s1 =

void strncpy_off(char dest[], long destOffset, char src[], long max); 相当于 strncpy 和 strncat 的结合 在这里插入图片描述

On key 'c' { int i; char s1[20] = "123456"; char s2[8] = "789"; for(i=0;i long pos; char s1[18] = "hello world"; char s2[11] = "world"; write("pos:%d",strstr(s1, s2)); write("pos:%d",strstr(s1, "l")); }

输出: pos:6

pos:2

long strstr_off(char s1[], long offset, char s2[]); 规定被搜索的字符串数组位置。

On key 'c' { long pos,i; char s1[18] = "hello world"; char s2[11] = "world"; for(i=0;i long pos,i; char s1[18] = "12121212"; char s2[11] = "world"; str_replace(s1, "1", "3"); write("s1:%s",s1); }

输出: s1:32323232

long str_replace(char s[], long startoffset, char replacement[], long length); // form 2 用另一个字符串替代被搜索的字符串的一部分,注意期望的结果字符串数组大小不能大于被搜索的字符串数组

On key 'c' { long pos,i; char s1[18] = "12345678"; str_replace(s1,4, "asdfg",strlen(s1));//form2 ,从索引4后面的字符串被"asdfg"替代 write("s1:%s",s1); }

输出: s1:1234asdfg

substr_cpy()

拷贝源字符串的子字符串到目的字符串 在这里插入图片描述

On key 'c' { long pos,i; char s1[18]; char s2[18] = "12345678"; for(i=0;i long pos,i; char s1[18] = "abcdefg"; char s2[18] = "12345678"; for(i=0;i write("Word:%x",swapWord(0x1234)); write("int:%x",swapInt(0x1234)); write("dword :%lx",swapDWord(0x12345678)); write("long :%lx",swapLong(0x12345678)); write("int64 :%I64x",swapInt64(0x1234567890876543LL));// 8个字节的整形,后面必须有"LL"后缀 write("qword :%I64x",swapQWord(0x1234567890876543LL)); }

输出: Word:3412 int:3412 dword :78563412 long :78563412 int64 :4365879078563412 qword :4365879078563412

toLower()

将输入字符或者字符串转换成小写 在这里插入图片描述

On key 'c' { char s[20]; write("s :%c",toLower('A')); // form1 ,转换单个字符 toLower(s, "aSdFgH", elcount(s)); // form2 ,转换字符串 write("s :%s",s); }

输出: s :a s :asdfgh

toUpper()

将输入字符或者字符串转换成大写,用法参考 toLower



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3