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,切割相关推荐

  1. php 字符串放到数组中,在PHP中将字符串转换为数组(Converting string into array in php)...

    在PHP中将字符串转换为数组(Converting string into array in php) 我有像下面的字符串 ["Day1"]["Morning" ...

  2. Javascript 对象二(Number、String、Boolean、Array、Date、Math、RegExp)

    Javascript 对象二(Number.String.Boolean.Array.Date.Math.RegExp) Number 数字 对象 String 字符串 对象 Boolean 布尔 对 ...

  3. EXT核心API详解(二)-Array/Date/Function/Number/String

    EXT核心API详解(二)-Array/Date/Function/Number/String Array类 indexOf( Object o )  Number object是否在数组中,找不到返 ...

  4. php simplexmlelement array,php中SimpleXMLElement 对象转换为数组

    PHP 提供了 simplexml_load_string 方法用来解析 XML 格式的字符串,并返回 SimpleXMLElement 对象,不过一般数组是更为适用的,所以也会有转换为普通数组的需求 ...

  5. Java char转换为String,String转换为char数组

    Today we will look into java char to String program. We will also learn how to convert String to a c ...

  6. ES10:Object.fromEntries()、String.prototype.trim()、Array.prototype.flat()、Array.prototype.flatMap()

    Object.fromEntries(iterable) 把键值对列表(二维数组)转换为一个对象,这个方法和 Object.entries() 相对 Object.fromEntries([['foo ...

  7. 字符串html在线互转,将string 的字符串转换为HTML的两种方法

    将string 的字符串转换为HTML的两种方法 采用jquery对象转换 采用原生javascript中DOMParser的parseFromString方法实现 1.采用jquery实现 var ...

  8. array,arraylist,string的总结

    import java.util.ArrayList;/* * 这个程序是对array,arraylist,string的总结 * 1.创建 *(1)数组:分为动态初始化和静态初始化.(动态初始化时其 ...

  9. array.tolist_在Python中使用array.tolist()将数组转换为列表

    array.tolist Given an array with some elements and we have to convert them to the list using array.t ...

  10. java map 转string_java-将Map String,Object转换为Map String,String

    java-将Map 转换为Map 如何将Map转换为Map? 这不起作用: Map map = new HashMap(); //Object is containing String Map new ...

最新文章

  1. 网络营销——网站在网络营销优化中不收录了怎么办呢?
  2. Web-Lesson07-JS的函数及作用域
  3. SAP UI5 sap-ui-core.js的加载逻辑
  4. 前端学习(2532):vuex mutation
  5. mysql 创建表check如何使用_MySQL怎么使用check约束
  6. Mysql数据库引擎快速指南
  7. matlab pdist如何实现,在MATLAB版本7中相当于pdist2
  8. 位操作符:与,或,异或 狼羊菜
  9. c语言聊天程序代码,今天打的代码。基于TCP的聊天程序。
  10. JsonCpp的使用
  11. Windows域策略 统一设置本地管理员密码 【全域策略生效】
  12. 洛谷 P2420 让我们异或吧 解题报告
  13. 数据库~Mysql里的Explain说明
  14. LoRaWAN网络协议与LoRa私有协议相比有哪些优势
  15. vxe-table checkbox range 实现复选框鼠标快速选择
  16. Flying-Saucer使用HTML或者FTL(Freemarker模板)生成PDF
  17. 天翼云 Ubuntu 22.04.1 LTS apt换源 (阿里云)
  18. PyQt5制作一个爬虫小工具,爬取雪球网上市公司的财务数据
  19. windows XP无法使用suspend to RAM (s3)功能的解决过程
  20. 扎好篱笆桩:三大运营商守住网络安全“命门”

热门文章

  1. 爬取热门网站的热榜,集中展示
  2. 平衡二叉树、二叉排序树-数据结构
  3. ActiveMQ反序列化漏洞(CVE-2015-5254)复现
  4. 双RocketMq集群的搭建
  5. SET ARITHABORT ON 对UI的影响
  6. 在连续发布版本的情况下 如何做好测试?
  7. string.Format格式化输出
  8. 在sharepoint中添加视频播放
  9. linux-01-概述
  10. LeetCode 670 最大交换 (暴力+贪心、Python)