4

与えられた:

2弦strA、strB

私が欲しい:

これらを比較して <0、=0、または >0 を返すには、Intersystems Cache ObjectScript を使用します。

ここのところ:

ドキュメントで、私のニーズを満たす関数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 に答える