1、在长字符串String1中的指定位置开始,从左向右查找短字符串String2最先出现的位置。具体功能代码如下:
FUNCTION FINDP : INTVAR_INPUTInString1 : STRING(255);//长字符串InString2 : STRING(255);//短字符串InPos: INT; //查找起始位置END_VARVARiPostion : INT;iLength1 : INT;iLength2 : INT;END_VAR//计算从字符串String1指定的位置开始出现String2的位置(从左往右)iLength1 := Standard.LEN(InString1);iLength2 := Standard.LEN(InString2);IF iLength1 < iLength2 OR iLength2 = 0 THEN RETURN; END_IF;FOR iPostion := MAX(InPos,1) TO (iLength1 - iLength2 + 1) BY 1 DOIF Standard.MID(InString1,iLength2,iPostion) = InString2 THENFINDP := iPostion;RETURN;END_IF;END_FOR;FINDP := 0;
测试结果如下:
2、在长字符串String1中的最右端开始,从右向左查找短字符串String2最先出现的位置。具体功能代码如下:
FUNCTION FINDB : INTVAR_INPUTInString1 : STRING(255);//长字符串InString2 : STRING(255);//短字符串END_VARVARiPosition : INT;iLength1 : INT;iLength2 : INT;END_VAR//计算String2在String1出现的位置(从右往左)iLength1 := Standard.LEN(InString1);iLength2 := Standard.LEN(InString2);IF iLength1 < iLength2 THENFindB := 0;RETURN;END_IF;FOR iPosition := iLength1 - iLength2 + 1 TO 1 BY -1 DOIF Standard.MID(InString1,iLength2,iPosition) = InString2 THENFindB := iPosition;RETURN;END_IF;END_FOR;FindB := 0;
测试结果如下:
以上功能的实现依靠Standard库的字符串功能,因此查找字符串长度最大长度255。