Liferay 常用基础类 —— com.liferay.util.StringUtil

来源:互联网 发布:淘宝素材包 编辑:程序博客网 时间:2024/05/17 22:37
位置: com.liferay.util.StringUtil

功能:对字符串进行处理。

方法说明:

(1)组合有分隔符的字符串,可以为已有的字符串加入新的字符串。如"Liferay,JetSpeed,oPortal",加入字符串"eXo"之后为"Liferay,JetSpeed,oPortal,eXo"。

public static String add( String s, String add )
——为字符串 s 添加字符串 add,默认分隔符为","(逗号),默认不允许添加 s 中以后的字符串,若add  在 s 中已经存在则返回原字符串 s。

public static String add( String s, String add, String delimiter )
——为字符串 s 添加字符串 add,可自定义采用的分隔符,以字符串delimiter 输入,默认不允许添加 s 中以后的字符串,若add  在 s 中已经存在则返回原字符串 s。

String add( String s, String add, String delimiter, boolean allowDuplicates )
——为字符串 s 添加字符串 add,可自定义采用的分隔符,以字符串delimiter 输入,允许自定义是否允许加入已有的字符串。当传参 allowDuplicates 为true 时允许重复添加相同的字符串。


(2)将字符串转换为与其对应的十六禁止的字符串表示。
public static String bytesToHexString( byte[] bytes )


(3)判断用分隔符表示的字符串组中是否含有指定的字符串。
public static boolean contains( String s, String text )
——查看s 中是否包涵字符串 text,默认以 ”,“ (逗号)为分隔符。若包涵返回true ,否则返回false 。

public static boolean contains( String s, String text, String delimiter )
——查看s 中是否包涵字符串 text,可指定以字符串dilimiter 为分隔符。若包涵返回true ,否则返回false 。


(4)统计字符串数组中与指定字符串的匹配个数。
public static int count( String s, String text )
——返回字符串s 含有text 的个数。不考虑分隔符,分隔符作为字符串中的普通字符处理。


(5)判断首字符串与末尾字符与字符串。
public static boolean endsWith(String s, char end)
public static boolean endsWith(String s, String end)
——判断是否以字符或字符串end 结尾,是则返回true, 否则返回false。

public static boolean startsWith(String s, char begin)
public static boolean startsWith(String s, String begin)
——判断是否以字符或字符串begin 开始,是则返回true, 否则返回false。


(6)提取字符,数字,首个字符串以及末尾字符串
public static String extractChars( String s )
——提取字符串s 中的所有字符,返回仅包含字符的字符串。

public static String extractDigits( String s )
——提取字符串s 中的所有数字,返回仅包涵数字的字符串。

public static String extractFirst( String s, String delimiter )
——返回第一个字符串,以字符串delimiter 为分隔符。

public static String extractLast( String s, String delimiter )
——返回最后一个字符串,以字符串delimiter 为分隔符。


(7)高亮关键字。
public static String highlight( String s, String keywords )
——默认高亮方式为加粗字符串中s 中的关键字 keywords。

public static String highlight( String s, String keywords, String highlight1, String highlight2 )
——高亮字符串s 中的关键字keywords,并且可利用字符串highlight1 和highlight2 标识高亮方式。这两个字符串应为html 中的标签对。如<b>和</b>,<i>和</i>,分别为加粗和斜体的方式。


(8)将字符串中的字符全部转化为小写
public static String lowerCase(String s)
——将字符串s 全部转化为小写表示。


(9)将一组字符串组合成通过分隔符标识的长字符串,如将一组字符串"Liferay"、"JetSpeed”、 "oPortal"组合成 "Liferay,JetSpeed,oPortal"。
public static String merge(List list)
public static String merge(String[] array)
——初始的一组字符串存放于List 或者String[ ] 中,默认以 "," (逗号)为分隔符整理。

public static String merge( List list, String delimiter )
public static String merge( String[] array, String delimiter )
——可指定分隔符delimiter 整理List 和String[ ] 中的字符集合。


(10)是字符串里的字符随机排列。
public static String randomize(String s)


(11)读取流
public static String read( ClassLoader classLoader, String name )
——作为字符串输入则作为是单行流文件处理,即在末尾添加换行符 "/n" 再返回修改后的字符串。

public static String read( InputStream is )
——流文件每行添加换行符后作为整个字符串返回。


(12)删除字符串中的子串,效果类似字符串数组中删除一个字符串元组。
public static String remove( String s, String remove )
——删除字符串s 中的remove 子串,默认以 ”," (逗号)为分隔符。

public static String remove( String s, String remove, String delimiter )
——删除字符串s 中的remove 子串,可指定分隔符delimiter 。


(13)替换字符串中的子串或字符,效果类似字符串数组中替换一个字符串元组,或者是替换字符数组中的一个字符。
public static String replace(String s, char oldSub, char newSub)
public static String replace(String s, char oldSub, String newSub)
public static String replace(String s, char oldSub, String newSub)
public static String replace(String s, String[] oldSubs, String[] newSubs)
——替换和被替换的单元既可以是字符也可以是字符串,甚至是字符串数组都可以。

(14)倒置字符串中的字符。
public static String reverse(String s)


(15)省略全文,显式所写的提要。
public static String shorten(String s)
——默认省略为20个字符,末尾追加省略号"...",如字符串"The ShowTime portlet allows you to display time based on the selected time zone and allows you to compare time between two different timezones." 调用shorten() 后返回 "The ShowTime portlet all..."。

public static String shorten(String s, int length)
——指定省略后的字符数,末尾追加省略号"...",如字符串"The ShowTime portlet allows you to display time based on the selected time zone and allows you to compare time between two different timezones." 调用shorten(s, 30) 后返回 "The ShowTime portlet allows you..."。

public static String shorten(String s, String suffix)
——默认省略为20个字符,末尾追加省略号"...",如字符串"The ShowTime portlet allows you to display time based on the selected time zone and allows you to compare time between two different timezones." 调用shorten(s, ">>check more") 后返回 "The ShowTime portlet all >>checkmore"。

public static String shorten(String s, int length, String suffix)
——指定省略后的字符数以及追加的后缀,末尾追加省略号"...",如字符串"The ShowTime portlet allows you to display time based on the selected time zone and allows you to compare time between two different timezones." 调用shorten(s, 30, ">>check more") 后返回 "The ShowTime portlet allows you >>check more"。


原创粉丝点击