日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SplitConcatWithAMP----Array转换为String,连接;String转换为Array,切割

發布時間:2024/7/23 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SplitConcatWithAMP----Array转换为String,连接;String转换为Array,切割 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

SplitConcatWithAMP

功能描述:

1、將字符串數組連接為整個字符串,'&' 為連接符

特例:如果 array 為 null 或 empty,拋出異常。因為這時無法轉換!

public static String arrayToStringWithAMP( String[] array ) throws Exception


2、字符串切割為字符數組,'&'為切割符

特例:如果 str 為 null,拋出異常。這時也無法轉換!

public static String[] stringToArrayWithAMP( String str ) throws Exception


3、將 "&" 轉換為 "&" (因為 "&" 為特殊字符)。注意:"&" 的 "&" 不做處理

public static String encode( String str )


4、將 "&" 還原為 "&"

public static String decode( String str )

public class SplitConcatWithAMP {//Array to string concat with "&"//If array items contain "&" replace it to "&amp;"public static String arrayToStringWithAMP( String[] array ) throws Exception{if( array == null || array.length == 0 ){throw new Exception("Array is null or empty, can not convert to string!");}StringBuilder sb = new StringBuilder();for( String s : array ){sb.append( encode(s) );sb.append( "&" );}return removeLastAMP( sb.toString() );}//Remove last "&" in stringprivate static String removeLastAMP( String str ){return str.substring( 0, str.length()-1 );}//String to array split with "&"//But if is "&amp;" ,do not splitpublic static String[] stringToArrayWithAMP( String str ) throws Exception{if( str == null ){throw new Exception("String is null, Can not convert to array!");}String[] result = str.split( "&(?!amp;)" );for( int i=0 ; i<result.length ; i++ ){result[i] = decode( result[i] );}return result;}//Encode "&" in String to "&amp;"//But if is "&amp;" ,do not change.public static String encode( String str ){if( str == null){return null;}return str.replaceAll( "&(?!amp;)", "&amp;" );}//Decode "&amp;" in string to "&"public static String decode( String str ){if( str == null ){return null;}return str.replaceAll( "&amp;", "&" );}} 演示:

public static void main(String[] args) throws Exception {String[] array = new String[]{"超人", "abc", "真的&可以區分", "懷&amp;疑", "gg"};//RightSystem.out.println( "Right Demo!" );System.out.println( Arrays.toString(array) );String temp = arrayToStringWithAMP(array);System.out.println( temp );array = stringToArrayWithAMP(temp);System.out.println( Arrays.toString( array ) );//ErrorSystem.out.println("Error Demo!");errorDemo();}static void errorDemo(){String string = "超人&abc&真的&可以區分&懷&疑&gg";String[] arr = string.split( "&" );System.out.println( Arrays.toString(arr) );}輸出結果:

Right Demo!
[超人, abc, 真的&可以區分, 懷&amp;疑, gg]
超人&abc&真的&amp;可以區分&&amp;&gg
[超人, abc, 真的&可以區分, 懷&疑, gg]
Error Demo!
[超人, abc, 真的, 可以區分, 懷, amp;疑, gg]


從上面的Right Demo結果可以看出:數組 -> 字符串 -> 數組

如果數組項中包含 "&",可以正確還原。

如果數組項中包含 "&amp;",會還原為 "&"。(但其表達的意義是一致的)


總結

以上是生活随笔為你收集整理的SplitConcatWithAMP----Array转换为String,连接;String转换为Array,切割的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。