javascript 中的 StringUtils

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9860814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:04:28  来源:igfitidea点击:

StringUtils in javascript

javascriptstringutilities

提问by Freewind

I'm looking for a js library like StringUtilsof commons-lang in java, which contains a lot of common methods to operating strings.

我在java中寻找commons-lang的StringUtils之类的js库,里面有很多操作字符串的常用方法。

Such as:

如:

  • IsEmpty/IsBlank - checks if a String contains text
  • Trim/Strip - removes leading and trailing whitespace
  • Equals - compares two strings null-safe
  • startsWith - check if a String starts with a prefix null-safe
  • endsWith - check if a String ends with a suffix null-safe
  • IndexOf/LastIndexOf/Contains - null-safe index-of checks
  • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-of any of a set of Strings
  • ContainsOnly/ContainsNone/ContainsAny - does String contains only/none/any of these characters
  • Substring/Left/Right/Mid - null-safe substring extractions
  • SubstringBefore/SubstringAfter/SubstringBetween - substring extraction relative to other strings
  • Split/Join - splits a String into an array of substrings and vice versa
  • Remove/Delete - removes part of a String
  • Replace/Overlay - Searches a String and replaces one String with another
  • Chomp/Chop - removes the last part of a String
  • LeftPad/RightPad/Center/Repeat - pads a String
  • UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the case of a String
  • CountMatches - counts the number of occurrences of one String in another
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters in a String
  • DefaultString - protects against a null input String
  • Reverse/ReverseDelimited - reverses a String
  • Abbreviate - abbreviates a string using ellipsis
  • IsEmpty/IsBlank - 检查字符串是否包含文本
  • Trim/Strip - 删除前导和尾随空格
  • 等于 - 比较两个字符串空安全
  • startsWith - 检查字符串是否以空安全前缀开头
  • EndsWith - 检查字符串是否以空安全后缀结尾
  • IndexOf/LastIndexOf/Contains - 空安全索引检查
  • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - 任何一组字符串的索引
  • containsOnly/ContainsNone/ContainsAny - 字符串是否只包含/无/任何这些字符
  • 子串/左/右/中 - 空安全子串提取
  • SubstringBefore/SubstringAfter/SubstringBetween - 相对于其他字符串的子字符串提取
  • Split/Join - 将字符串拆分为子字符串数组,反之亦然
  • 删除/删除 - 删除字符串的一部分
  • 替换/覆盖 - 搜索字符串并将一个字符串替换为另一个
  • Chomp/Chop - 删除字符串的最后一部分
  • LeftPad/RightPad/Center/Repeat - 填充一个字符串
  • UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - 改变字符串的大小写
  • CountMatches - 计算一个字符串在另一个字符串中出现的次数
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - 检查字符串中的字符
  • DefaultString - 防止空输入字符串
  • Reverse/ReverseDelimited - 反转字符串
  • 缩写 - 使用省略号缩写字符串

It'll be better if it contains some other methods for arrays/date, etc.

如果它包含一些其他的数组/日期方法等会更好。

采纳答案by timrwood

String utils - Underscore.string

字符串实用程序 - Underscore.string

Object/array utils - Underscore

对象/数组实用程序 -下划线

Date utils - Moment.js

日期工具 - Moment.js

回答by ?ime Vidas

Here we go:

开始了:

IsEmpty

是空的

str.length === 0

IsBlank

是空白的

str.trim().length === 0

Trim

修剪

str.trim()

Equals

等于

str1 === str2

startsWith

以。。开始

str.indexOf( str2 ) === 0

IndexOf

指数

str.indexOf( str2 )

LastIndexOf

上次索引

str.lastIndexOf( str2 )

Contains

包含

str.indexOf( str2 ) !== -1

Substring

子串

str.substring( start, end )

Left

剩下

str.slice( 0, len )

Mid

str.substr( i, len )

Right

str.slice( -len, str.length )

And so on... (should I continue?)

等等......(我应该继续吗?)

回答by Axel Podehl

I'm constantly switching between Java backend and JavaScript frontend so for me it makes a lot of sense just to blindly use the StringUtils methods and don't even think about it. It would be great if someone would take the time to port all of the Apache StringUtils methods into a JavaScript ;-)

我经常在 Java 后端和 JavaScript 前端之间切换,所以对我来说,盲目使用 StringUtils 方法甚至不考虑它是很有意义的。如果有人愿意花时间将所有 Apache StringUtils 方法移植到 JavaScript 中,那就太好了;-)

Here's my contribution:

这是我的贡献:

  String.prototype.startsWith = function(prefix) {
    return this.indexOf(prefix,0) === 0;
  };

  String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
  };

  String.prototype.substringBefore = function(str) {
    var idx = this.indexOf(str);
    if( idx!==-1 ) {
      return this.substr(0,idx);
    }
    return this;
  };

  String.prototype.substringBeforeLast = function(str) {
    var idx = this.lastIndexOf(str);
    if( idx!==-1 ) {
      return this.substr(0,idx);
    }
    return this;
  };

  String.prototype.substringAfter = function(str) {
    var idx = this.indexOf(str);
    if( idx!==-1 ) {
      return this.substr(idx+str.length);
    }
    return this;
  };

  String.prototype.substringAfterLast = function(str) {
    var idx = this.lastIndexOf(str);
    if( idx!==-1 ) {
      return this.substr(idx+str.length);
    }
    return this;
  };

  // left pad with spaces (or the specified character) to this length 
  String.prototype.leftPad = function (length,c) {
    c = c || " ";
    if( length <= this.length ) return this;
    return new Array(length-this.length+1).join(c) + this;
  };

  // right pad with spaces (or the specified character) to this length 
  String.prototype.rightPad = function (length,c) {
    c = c || " ";
    if( length <= this.length ) return this;
    return this + new Array(length-this.length+1).join(c);
  };

回答by Redger

Use both Javascript basic methods and JQuery for DOM and moment.jsfor dates.

对 DOM 使用 Javascript 基本方法和 JQuery,对日期使用moment.js

Read this: Utils libraryif you're looking for compatibility between browsers.

如果您正在寻找浏览器之间的兼容性,请阅读:Utils 库

Or you can write your own Apache-like commons-lang too!

或者您也可以编写自己的类似 Apache 的 commons-lang!