StringUtils的各项用法(转载)

news/2024/6/3 20:35:05 标签: 字符串, python, 正则表达式, java, javascript

StringUtils的各项用法

Commons之字符串操作
要利用Jakarta Commons来进行字符串操作,首先需要加载需要用到的包:
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;

以下是StringUtils的各项用法1.空字符串检查
使用函数: StringUtils.isBlank(testString)
函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False
例程:
    String test = "";
    String test2 = "\n\n\t";
    String test3 = null;
    String test4 = "Test";

    System.out.println( "test blank? " + StringUtils.isBlank( test ) );
    System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );
    System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );
    System.out.println( "test4 blank? " + StringUtils.isBlank( test4 ) );
输出如下:
test blank? true
test2 blank? true
test3 blank? true
test4 blank? False
函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反.

2.清除空白字符使用函数: StringUtils.trimToNull(testString)
函数介绍:清除掉testString首尾的空白字符,如果仅testString全由空白字符
(whitespace)组成则返回null
例程:
    String test1 = "\t";
    String test2 = "  A  Test  ";
    String test3 = null;

    System.out.println( "test1 trimToNull: " + StringUtils.trimToNull( test1 ) );
    System.out.println( "test2 trimToNull: " + StringUtils.trimToNull( test2 ) );
    System.out.println( "test3 trimToNull: " + StringUtils.trimToNull( test3 ) );

输出如下:
test1 trimToNull: null
test2 trimToNull: A  Test
test3 trimToNull: null

注意:函数StringUtils.trim(testString)与
StringUtils.trimToNull(testString)功能类似,但testString由空白字符
(whitespace)组成时返回零长度字符串

3.取得字符串的缩写
使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,width)
函数介绍:在给定的width内取得testString的缩写,当testString的长度小于width则返回原字符串.
例程:
    String test = "This is a test of the abbreviation.";
    String test2 = "Test";

    System.out.println( StringUtils.abbreviate( test, 15 ) );
    System.out.println( StringUtils.abbreviate( test, 5,15 ) );
    System.out.println( StringUtils.abbreviate( test2, 10 ) );
输出如下:
This is a te...
...is a test...
Test

4.劈分字符串
使用函数: StringUtils.split(testString,splitChars,arrayLength)
函数介绍:splitChars中可以包含一系列的字符串来劈分testString,并可以设定得
到数组的长度.注意设定长度arrayLength和劈分字符串间有抵触关系,建议一般情况下
不要设定长度.
例程:
    String input = "A b,c.d|e";
    String input2 = "Pharmacy, basketball funky";

    String[] array1 = StringUtils.split( input, " ,.|");
    String[] array2 = StringUtils.split( input2, " ,", 2 );


    System.out.println( ArrayUtils.toString( array1 ) );
    System.out.println( ArrayUtils.toString( array2 ) );
输出如下:
{A,b,c,d,e}
{Pharmacy,basketball funky}

5.查找嵌套字符串
使用函数:StringUtils.substringBetween(testString,header,tail)
函数介绍:在testString中取得header和tail之间的字符串。不存在则返回空
例程:
    String htmlContent = "ABC1234ABC4567";
    System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));
    System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567"));
输出如下:
    ABC
    null

6.去除尾部换行符
使用函数:StringUtils.chomp(testString)
函数介绍:去除testString尾部的换行符
例程:
    String input = "Hello\n";
    System.out.println( StringUtils.chomp( input ));
    String input2 = "Another test\r\n";
    System.out.println( StringUtils.chomp( input2 ));
输出如下:
    Hello
    Another test

7.重复字符串
使用函数:StringUtils.repeat(repeatString,count)
函数介绍:得到将repeatString重复count次后的字符串
例程:
    System.out.println( StringUtils.repeat( "*", 10));
    System.out.println( StringUtils.repeat( "China ", 5));
输出如下:
    **********
    China China China China China

其他函数:StringUtils.center( testString, count,repeatString );
函数介绍:把testString插入将repeatString重复多次后的字符串中间,得到字符串
的总长为count
例程:
    System.out.println( StringUtils.center( "China", 11,"*"));
输出如下:
    ***China***

8.颠倒字符串
使用函数:StringUtils.reverse(testString)
函数介绍:得到testString中字符颠倒后的字符串
例程:
    System.out.println( StringUtils.reverse("ABCDE"));
输出如下:
    EDCBA

9.判断字符串内容的类型函数介绍:
StringUtils.isNumeric( testString ) :如果testString全由数字组成返回True
StringUtils.isAlpha( testString ) :如果testString全由字母组成返回True
StringUtils.isAlphanumeric( testString ) :如果testString全由数字或数字组
成返回True
StringUtils.isAlphaspace( testString )  :如果testString全由字母或空格组
成返回True

例程:
    String state = "Virginia";
    System.out.println( "Is state number? " + StringUtils.isNumeric(
state ) );
    System.out.println( "Is state alpha? " + StringUtils.isAlpha( state )
);
    System.out.println( "Is state alphanumeric? " +StringUtils.isAlphanumeric( state ) );
    System.out.println( "Is state alphaspace? " + StringUtils.isAlphaSpace( state ) );
输出如下:
    Is state number? false
    Is state alpha? true
    Is state alphanumeric? true
    Is state alphaspace? true

10.取得某字符串在另一字符串中出现的次数使用函数:StringUtils.countMatches(testString,seqString)
函数介绍:取得seqString在testString中出现的次数,未发现则返回零
例程:
    System.out.println(StringUtils.countMatches( "Chinese People", "e"
));
输出:
    4

11.部分截取字符串
使用函数:
StringUtils.substringBetween(testString,fromString,toString ):取得两字符
之间的字符串
StringUtils.substringAfter( ):取得指定字符串后的字符串
StringUtils.substringBefore( ):取得指定字符串之前的字符串
StringUtils.substringBeforeLast( ):取得最后一个指定字符串之前的字符串
StringUtils.substringAfterLast( ):取得最后一个指定字符串之后的字符串

函数介绍:上面应该都讲明白了吧。
例程:
    String formatted = " 25 * (30,40) [50,60] | 30";
    System.out.print("N0: " + StringUtils.substringBeforeLast( formatted, "*" ) );
    System.out.print(", N1: " + StringUtils.substringBetween( formatted, "(", "," ) );
    System.out.print(", N2: " + StringUtils.substringBetween( formatted, ",", ")" ) );
    System.out.print(", N3: " + StringUtils.substringBetween( formatted, "[", "," ) );
    System.out.print(", N4: " + StringUtils.substringBetween( formatted, ",", "]" ) );
    System.out.print(", N5: " + StringUtils.substringAfterLast( formatted, "|" ) );
输出如下:
    N0:  25 , N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5:  30


http://www.niftyadmin.cn/n/1547741.html

相关文章

安卓获取手机网络强度_手机信号强度常见几种测试方法

今天给大家介绍几种常用的手机信号强度测试方法,以助于大家在安装手机信号屏蔽器或者手机信号放大器的时候能轻松判断是否可以安装或者能准确知道安装手机信号屏蔽器后会达到什么样的效果。第一种,使用常用的信号测试软件,可以轻松的知道使用…

java 软引用 使用_java – 使用软引用的“实际后果”是什么?

我认为它们所暗示的是,如果你使用Soft参考映射,你应该为最大内存使用做准备,并且可能有更多gc活动,因为引用只是gc’d,因为需要释放内存.如果您知道只需要缓存中的最后n个值,则使用LRU缓存是一种更精简的方法,对于正在运行的应用程序具有更可预测的资源使用.此外,根据this,似乎…

类的继承python事例_Python 类的继承

说明Python 教程正在编写中,欢迎大家加微信 sinbam 提供意见、建议、纠错、催更。类的继承也是一个非常有用的设计,我们在新定义类时,如果它属于之前定义过类的一部分,则可以继承父类的特性。继承方法基本方法如下:cla…

Windows2003环境下安装VS2008,.NetFramework3.5组件安装失败的解决

由于经历过不少挫折,所以安装之前,找了不少资料先参考一下。之前安装VS2008时,发现与Office2007冲突,在搜索了网络以后并请教一些朋友,发现其顺序是先VS2008然后Office2007,并且最好在vs2008之前安装SQL200…

The softwares are used in Flex developing

The softwares are used in Flex developing 解决安装 Adobe Flash Player ActiveX 控件失败的方法 1、下载安装微软subinacl.msi (不知道是什么,安装后有人说有Windows Resource Kits)(我的没有) http://download.microsoft.com/download/1/7/d/17d82b72-bc6a-4dc8…

java压缩工具_java 压缩解压缩 工具类 | 学步园

package com.test.zip;import java.util.*;import java.util.zip.*;import java.io.*;public class ZipTool {/*** 压缩文件** param zipFileName* 保存的压缩包文件路径* param inputFile* 需要压缩的文件夹或者文件路径* throws Exception*/public voi…

python中的超类_Python中的抽象超类

标签:1 #-*- coding:utf-8 -*-2 classSuper(object):34 deftest(self):5 self.action()67 classSub(Super):89 defaction(self):10 print "sub action"1112 objSub()13 obj.test()代码中,超类Super中定义了一个函数test。调用了自身的action函…

__doPostBack原理

__doPostBack是一个纯粹并且是非常简单的javascript函数,大部分的页面PostBack都是由它触发的。注意,这里是“大部分”,因为只有两个Web Server Control 会自己触发页面的PostBack,其它的所以控件都是通过__doPostBack函数触发页面的PostB…