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
javascript中的StringUtils_Javascript_String_Utilities - Fatal编程技术网

javascript中的StringUtils

javascript中的StringUtils,javascript,string,utilities,Javascript,String,Utilities,我正在寻找一个类似java中commons lang的js库,它包含许多操作字符串的常用方法 例如: IsEmpty/IsBlank-检查字符串是否包含文本 修剪/剥离-删除前导和尾随空格 Equals-比较两个空安全字符串 startsWith-检查字符串是否以前缀null safe开头 endsWith-检查字符串是否以后缀null结尾 IndexOf/LastIndexOf/Contains-检查的空安全索引 IndexOfAny/LastIndexOfAny/IndexOfAnyBut

我正在寻找一个类似java中commons lang的js库,它包含许多操作字符串的常用方法

例如:

  • IsEmpty/IsBlank-检查字符串是否包含文本
  • 修剪/剥离-删除前导和尾随空格
  • Equals-比较两个空安全字符串
  • startsWith-检查字符串是否以前缀null safe开头
  • endsWith-检查字符串是否以后缀null结尾
  • IndexOf/LastIndexOf/Contains-检查的空安全索引
  • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut-一组字符串的索引
  • ContainsOnly/ContainsNone/CONTAINSANSANY-字符串是否仅包含/none/这些字符中的任何一个
  • 子字符串/左/右/中-空安全子字符串提取
  • SubstringBefore/SubstringAfter/SubstringBetween-相对于其他字符串的子字符串提取
  • 拆分/连接-将字符串拆分为子字符串数组,反之亦然
  • 移除/删除-移除字符串的一部分
  • 替换/覆盖-搜索字符串并将一个字符串替换为另一个字符串
  • Chomp/Chop-删除字符串的最后一部分
  • LeftPad/RightPad/Center/Repeat-填充字符串
  • 大写/小写/SwapCase/大写/非大写-更改字符串的大小写
  • CountMatches-统计一个字符串在另一个字符串中出现的次数
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable-检查字符串中的字符
  • DefaultString-针对空输入字符串进行保护
  • 反转/反转限制-反转字符串
  • 缩写-使用省略号缩写字符串

如果它包含用于数组/日期等的其他方法,效果会更好。

对DOM和日期使用Javascript基本方法和JQuery

阅读以下内容:如果您正在寻找浏览器之间的兼容性

或者你也可以像commons lang一样编写自己的Apache

我们开始:

我是空的

str.length === 0
str1 === str2
str.indexOf( str2 ) === 0
为空

str.trim().length === 0
修剪

str.trim()
等于

str.length === 0
str1 === str2
str.indexOf( str2 ) === 0
开始使用

str.length === 0
str1 === str2
str.indexOf( str2 ) === 0
IndexOf

str.indexOf( str2 )
str.lastIndexOf( str2 )
LastIndexOf

str.indexOf( str2 )
str.lastIndexOf( str2 )
包含

str.indexOf( str2 ) !== -1
子字符串

str.substring( start, end )

str.slice( 0, len )
str.slice( -len, str.length )
Mid

str.substr( i, len )

str.slice( 0, len )
str.slice( -len, str.length )
等等。。。(我应该继续吗?

字符串utils-

对象/数组utils-


Date utils-

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

以下是我的贡献:

  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);
  };
String.prototype.startsWith=函数(前缀){
返回这个.indexOf(前缀,0)==0;
};
String.prototype.endsWith=函数(后缀){
返回this.indexOf(后缀,this.length-suffix.length)!=-1;
};
String.prototype.substringBefore=函数(str){
var idx=this.indexOf(str);
如果(idx!=-1){
返回此.substr(0,idx);
}
归还这个;
};
String.prototype.substringBeforeLast=函数(str){
var idx=this.lastIndexOf(str);
如果(idx!=-1){
返回此.substr(0,idx);
}
归还这个;
};
String.prototype.substringAfter=函数(str){
var idx=this.indexOf(str);
如果(idx!=-1){
返回此.substr(idx+str.length);
}
归还这个;
};
String.prototype.substringAfterLast=函数(str){
var idx=this.lastIndexOf(str);
如果(idx!=-1){
返回此.substr(idx+str.length);
}
归还这个;
};
//用空格(或指定字符)填充此长度的左键盘
String.prototype.leftPad=函数(长度,c){
c=c | |“”;

if(length jQuery用于对原语值进行操作……?Dates?问题是关于字符串操作的问题……他说:“如果它包含其他一些用于数组/日期等的方法会更好。”因此,字符串的基本Javascript方法、用于DOM和JSON高级处理的jQuery(毕竟是所有字符串)以及用于日期解析的moment.js(也是字符串).我在问题的最后一部分也提到了日期:)你能更精确地描述一下你想在字符串上执行的操作吗?请提供一个链接到
StringUtils
API的文档。JavaScript程序员通常对Java没有太多经验,所以他们不知道
StringUtils
提供了什么方法……已经添加了
StringUtils
@Freew的链接从我所看到的来看,许多
stringutil
静态方法在JavaScript中都可以作为实例方法使用。请看这里:请注意,IE:-(在代码可移植性方面,JavaScript可能是一场噩梦:@Redger
trim()
是这里的例外。其他字符串方法是cross-browser.commons-lang
org.apache.commons.lang.StringUtils
到处都是
null
阻力,例如
isEmpty
是这样实现的
return str==null | | str length()==0;
虽然这个答案很有意义,因为它表明与本机JS相比并不“那么难”,但有人可能会说,如果你只是偶尔做JS开发,你可能会想使用JS UTI库来避免愚蠢的错误和耗时的学习。这是这个库的目标,请参阅[Undescorejs.org上的介绍声明](parendarejs.org)。这个subscription.js[github.com/epeli/underprounder.string](underprounder.string.js)的子集在缩小后(不是gzip!)只有7Kb。我能看到的唯一问题是浏览器支持不清楚,请参阅更正:underprounder.string是“underprounder.js的扩展”
(它不是“underprounder.js的子集”),但您可以将其用作一个好的下划线。