比较V +中的2个字符串

时间:2016-06-28 17:16:18

标签: v+

在V +中比较2个字符串是否相同的最佳方法是什么。是否有strcmp样式函数?是否可以根据字符串的lexicographical顺序获取值?

1 个答案:

答案 0 :(得分:0)

没有strcmp功能,你可以做两件事:

  1. 如果您想要直线bool值,可以使用==

    $in1 = "lalala"
    $in2 = "lalala"
    
    IF $in1 == $in2 THEN
        TYPE "LALALA"
    END
    
  2. 如果你想要一个基于字典顺序返回数字的函数,你需要写一个,这样就足够了:

  3. .PROGRAM strcmp($a, $b, result)
            AUTO len.a, len.b, pos
    
            len.a = LEN($a)
            len.b = LEN($b)
            pos = 1
    
            ; Loop to the end of the string that is the same
            WHILE (pos-1 < len.a) AND (pos-1 < len.b) AND (ASC($a,pos) == ASC($b,pos)) DO
                pos = pos+1
            END
    
            ; If they are still the same at the end return 0
            IF (ASC($a,pos) == ASC($b,pos)) THEN
                result = 0
                RETURN
            END
    
            ; If $a is -1 then we reached the end of a before b
            IF (ASC($a,pos) == -1) THEN
                result = -1
                RETURN
            END
    
            ; If $b is -1 then we reached the end of b before a
            IF (ASC($b,pos) == -1) THEN
                result = 1
                RETURN
            END
    
            ; Otherwise get the comparison of the current value of $a and $b
            result = ASC($a,pos)-ASC($b,pos)
            RETURN
    .END