4

鉴于:

2 串 strA, strB

我想:

在 Intersystems Cache ObjectScript 中执行它们之间的比较并返回 <0、=0 或 >0。

至今:

我在文档中找到了一个满足我需求的函数StrComp。不幸的是,这个函数不是 Cache ObjectScript 的一部分,而是来自 Caché Basic。

我已将该函数包装为实用程序类的 classMethod:

ClassMethod StrComp(
    pstrElem1 As %String,
    pstrElem2 As %String) As %Integer [ Language = basic ]
{
    Return StrComp(pstrElem1,pstrElem2)
}

推荐这种方法吗?有没有可用的功能?

提前致谢。

4

3 回答 3

3

有点不清楚您希望此字符串比较做什么,但您似乎正在寻找follows ]orsorts after ]]运算符。

文档(取自此处):

  • 二进制后跟运算符 ( ]) 测试左操作数中的字符是否在 ASCII 整理顺序中位于右操作数中的字符之后。
  • 运算符 ( ) 之后的二元排序]]测试在数字下标排序规则中,左操作数是否排在右操作数之后。

语法看起来很奇怪,但它应该可以满足您的需求。

if "apple" ] "banana" ...
if "apple" ]] "banana" ...
于 2015-06-05T13:16:07.947 回答
2

如果你想要纯 ObjectScript,你可以使用它;它假设你真的想做类似 Java 的事情Comparable<String>

///
/// Compare two strings as per a Comparator<String> in Java
///
/// This method will only do _character_ comparison; and it pretty much
/// assumes that your Caché installation is Unicode.
///
/// This means that no collation order will be taken into account etc.
///
/// @param o1: first string to compare
/// @param o2: second string to compare
/// @returns an integer which is positive, 0 or negative depending on
/// whether o1 is considered lexicographically greater than, equal or
/// less than o2
ClassMethod strcmp(o1 as %String, o2 as %String) as %Integer
{
    #dim len as %Integer
    #dim len2 as %Integer
    set len = $length(o1)
    set len2 = $length(o2)
    /*
     * Here we rely on the particularity of $ascii to return -1 for any
     * index asked within a string literal which is greater than it length.
     *
     * For instance, $ascii("x", 2) will return -1.
     *
     * Please note that this behavior IS NOT documented!
     */
    if (len2 > len) {
        len = len2
    }

    #dim c1 as %Integer
    #dim c2 as %Integer

    for index=1:1:len {
        set c1 = $ascii(o1, index)
        set c2 = $ascii(o2, index)

        if (c1 '= c2) {
            return c1 - c2
        }
    }

    /*
     * The only way we could get here is if both strings have the same
     * number of characters (UTF-16 code units, really) and are of
     * equal length
     */
    return 0
}
于 2015-07-28T12:04:24.350 回答
1

可以在您的代码中使用不同的语言,如果它能解决您的任务,为什么不呢。但是您必须注意,并非所有语言都适用于服务器端。JavaScript 仍然是客户端语言,不能以这种方式使用。

于 2015-06-05T12:39:14.770 回答