JavaScript 相当于 C strncmp(比较字符串长度)

发布于 2024-08-18 14:36:48 字数 213 浏览 7 评论 0原文

JavaScript 中是否有与 C 函数 strncmp 等效的函数? strncmp 接受两个字符串参数和一个整数length 参数。它会比较两个字符串最多 length 个字符,并确定它们是否相等 length

JavaScript 是否有等效的内置函数?

Is there an equivalent in JavaScript to the C function strncmp? strncmp takes two string arguments and an integer length argument. It would compare the two strings for up to length chars and determine if they were equal as far as length went.

Does JavaScript have an equivalent built in function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

掩耳倾听 2024-08-25 14:36:48

您可以轻松构建该函数:

function strncmp(str1, str2, n) {
  str1 = str1.substring(0, n);
  str2 = str2.substring(0, n);
  return ( ( str1 == str2 ) ? 0 :
                              (( str1 > str2 ) ? 1 : -1 ));
}

函数末尾三元的替代方案可以是 localeCompare 方法,例如 return str1.localeCompare(str2);

You could easily build that function:

function strncmp(str1, str2, n) {
  str1 = str1.substring(0, n);
  str2 = str2.substring(0, n);
  return ( ( str1 == str2 ) ? 0 :
                              (( str1 > str2 ) ? 1 : -1 ));
}

An alternative to the ternary at the end of the function could be the localeCompare method e.g return str1.localeCompare(str2);

许仙没带伞 2024-08-25 14:36:48

事实并非如此。您可以将其定义为:

function strncmp(a, b, n){
    return a.substring(0, n) == b.substring(0, n);
}

It does not. You could define one as:

function strncmp(a, b, n){
    return a.substring(0, n) == b.substring(0, n);
}
凤舞天涯 2024-08-25 14:36:48

自 ECMAScript 2015 以来,有 startsWith()

str.startsWith(searchString[, position])

这涵盖了非常常见的用例,其中比较的长度是 searchString 的长度,并且只需要一个布尔返回值(strcmp( ) 返回一个整数来指示相对顺序。)

Mozilla 文档页面 还包含一个 polyfill 用于 <代码>String.prototype.startsWith()。

Since ECMAScript 2015 there is startsWith():

str.startsWith(searchString[, position])

This covers the very frequent use case where the length of the comparison is the length of the searchString, and only a boolean return value is required (strcmp() returns an integer to indicate relative order, instead.)

The Mozilla doc page also contains a polyfill for String.prototype.startsWith().

旧人哭 2024-08-25 14:36:48

没有,但您可以在此处找到一个,以及许多其他有用的 JavaScript 函数

function strncmp ( str1, str2, lgth ) {
    // Binary safe string comparison  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strncmp
    // +      original by: Waldo Malqui Silva
    // +         input by: Steve Hilder
    // +      improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +       revised by: gorthaur
    // + reimplemented by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strncmp('aaa', 'aab', 2);
    // *     returns 1: 0
    // *     example 2: strncmp('aaa', 'aab', 3 );
    // *     returns 2: -1
    var s1 = (str1+'').substr(0, lgth);
    var s2 = (str2+'').substr(0, lgth);

    return ( ( s1 == s2 ) ? 0 : ( ( s1 > s2 ) ? 1 : -1 ) );
}

It doesn't but you can find one here, along with many other useful javascript functions.

function strncmp ( str1, str2, lgth ) {
    // Binary safe string comparison  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strncmp
    // +      original by: Waldo Malqui Silva
    // +         input by: Steve Hilder
    // +      improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +       revised by: gorthaur
    // + reimplemented by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: strncmp('aaa', 'aab', 2);
    // *     returns 1: 0
    // *     example 2: strncmp('aaa', 'aab', 3 );
    // *     returns 2: -1
    var s1 = (str1+'').substr(0, lgth);
    var s2 = (str2+'').substr(0, lgth);

    return ( ( s1 == s2 ) ? 0 : ( ( s1 > s2 ) ? 1 : -1 ) );
}
酒浓于脸红 2024-08-25 14:36:48

您始终可以先对字符串进行子串,然后进行比较。

you could always substring the strings first, and then compare.

檐上三寸雪 2024-08-25 14:36:48
function strncmp(a, b, length) {
   a = a.substring(0, length);
   b = b.substring(0, length);

   return a == b;
}
function strncmp(a, b, length) {
   a = a.substring(0, length);
   b = b.substring(0, length);

   return a == b;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文