(StringUtil包函数(用法))

时间:2025-02-15 08:01:30

1.空字符串检查
使用函数: (testString)
函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False
例程:
    String test = "";
    String test2 = "/n/n/t";
    String test3 = null;
    String test4 = "Test";
    ( "test blank? " + ( test ) );
    ( "test2 blank? " + ( test2 ) );
    ( "test3 blank? " + ( test3 ) );
    ( "test4 blank? " + ( test4 ) );
输出如下:
test blank? true
test2 blank? true
test3 blank? true
test4 blank? False
函数(testString)的功能与(testString)相反.


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

    ( "test1 trimToNull: " + ( test1 ) );
    ( "test2 trimToNull: " + ( test2 ) );
    ( "test3 trimToNull: " + ( test3 ) );

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

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


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

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

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

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


    ( ( array1 ) );
    ( ( array2 ) );
输出如下:
{A,b,c,d,e}
{Pharmacy,basketball funky}

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


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


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

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


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

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

例程:
    String state = "Virginia";
    ( "Is state number? " + (state ) );
    ( "Is state alpha? " + ( state ));
    ( "Is state alphanumeric? " +( state ) );
    ( "Is state alphaspace? " + ( state ) );
输出如下:
    Is state number? false
    Is state alpha? true
    Is state alphanumeric? true
    Is state alphaspace? true

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

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

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