假设:
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)
}
推荐这种方法吗? 有没有可用的功能?
提前致谢。
答案 0 :(得分:2)
有点不清楚您希望这个字符串比较要做什么,但看起来您正在寻找follows ]
或sorts after ]]
运算符。
文档(取自Efficiently Importing Data):
]
)测试左操作数中的字符是否位于ASCII整理顺序中右操作数中的字符之后。]]
)之后的二进制排序测试左操作数是否在数字下标归类序列中的右操作数之后排序。语法看起来很奇怪但它应该做你需要的。
if "apple" ] "banana" ...
if "apple" ]] "banana" ...
答案 1 :(得分: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
}
答案 2 :(得分:1)
可以在代码中使用不同的语言,如果它解决了您的任务,为什么不呢。但是你必须注意到并非所有语言都适用于服务器端。 JavaScript仍然是客户端的语言,不能以这种方式使用。