比较系统间缓存对象脚本中的字符串



给定:

2个字符串strA、strB

我想要:

为了在它们之间执行比较并返回<0,=0或>0,在系统间缓存对象脚本中。

到目前为止:

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

我已经将函数包装为类实用程序类的方法:

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

是否推荐这种方法?有什么可用的功能吗?

提前谢谢。

您希望这个字符串比较做什么还不清楚,但您似乎在寻找follows ]sorts after ]]运算符。

文件(取自此处(:

  • 二进制跟随运算符(](测试ASCII排序序列中左操作数中的字符是否在右操作数中
  • 二进制排序后运算符(]](测试左操作数是否在数字下标排序序列中排序在右操作数之后

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

if "apple" ] "banana" ...
if "apple" ]] "banana" ...

如果您想要纯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
}

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

相关内容

  • 没有找到相关文章

最新更新