Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 比较系统间缓存Objectscript中的字符串_String_String Comparison_Intersystems Cache_Intersystems_Objectscript - Fatal编程技术网

String 比较系统间缓存Objectscript中的字符串

String 比较系统间缓存Objectscript中的字符串,string,string-comparison,intersystems-cache,intersystems,objectscript,String,String Comparison,Intersystems Cache,Intersystems,Objectscript,给定: 2弦strA、strB 我想要: 在系统间缓存ObjectScript中执行它们之间的比较并返回0 到目前为止: 我在文档中找到了满足我需要的功能。不幸的是,这个函数不是Cache ObjectScript的一部分,而是来自CachéBasic 我已将函数包装为实用程序类的classMethod: ClassMethod StrComp( pstrElem1 As %String, pstrElem2 As %String) As %Integer [ Language

给定:

2弦strA、strB

我想要:

在系统间缓存ObjectScript中执行它们之间的比较并返回0

到目前为止:

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

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

ClassMethod StrComp(
    pstrElem1 As %String,
    pstrElem2 As %String) As %Integer [ Language = basic ]
{
    Return StrComp(pstrElem1,pstrElem2)
}
是否建议采用这种方法? 有什么功能可用吗


提前感谢。

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

这个字符串比较到底要做什么还不清楚,但似乎您正在寻找
follow]
sorts after]
操作符

文件(摘自):

  • 二进制跟随运算符(
    ]
    )测试ASCII排序序列中左操作数中的字符是否在右操作数中的字符之后
  • 二进制排序运算符(
    ]]
    )测试在数字下标排序序列中,左操作数是否排序在右操作数之后
语法看起来很奇怪,但它应该满足您的需要

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

如果您想要纯ObjectScript,可以使用它;它假设您真的想做一些类似Java的
Compariable

///
///根据Java中的比较器比较两个字符串
///
///此方法只进行字符比较;而且差不多
///假设您的Caché安装是Unicode。
///
///这意味着将不考虑排序规则等。
///
///@param o1:要比较的第一个字符串
///@param o2:要比较的第二个字符串
///@返回一个正、0或负的整数,具体取决于
///o1是否被视为字典大于、等于或
///低于o2
类方法strcmp(o1作为%String,o2作为%String)作为%Integer
{
#dim len为%Integer
#dim len2为%Integer
设置长度=$length(o1)
设置len2=$length(o2)
/*
*在这里,我们依靠$ascii的特殊性来返回-1
*在大于其长度的字符串文本中请求索引。
*
*例如,$ascii(“x”,2)将返回-1。
*
*请注意,此行为未记录在案!
*/
如果(len2>len){
len=len2
}
#dim c1作为%Integer
#dim c2为%Integer
对于索引=1:1:len{
集合c1=$ascii(o1,索引)
设置c2=$ascii(o2,索引)
如果(c1'=c2){
返回c1-c2
}
}
/*
*我们能到达这里的唯一方法是如果两个字符串具有相同的
*字符数(实际上是UTF-16代码单位)和
*等长
*/
返回0
}

是的,使用该运算符,我可以编写一个函数,首先比较两个字符串,如果它们相等,则返回0。如果它们不同,我使用您建议的运算符,如果strA“排序之后”,则返回1或-1strB@matmat您可能还想看看我的解决方案,因为它只遍历所有字符串一次;尽管可能会得到改进,但文档是不正确的!(我在InterSystems工作时实际实现了这些运算符)。
]
将基于8位字符或16位UCS-2字符进行比较(即非BMP Unicode字符,即那些>0xffff将基于其作为一对代理字符的表示进行排序).
]
将根据当前排序规则序列设置进行比较。默认情况下,这将首先排序“”,然后是所有数字(和规范数字字符串),最后是所有其他字符串。这是一个更面向对象的解决方案!我一直在寻找@BrandonHorst提供的解决方案,但这个答案很好,谢谢@fge!;)
///
/// 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
}