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 "&"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 "&" ,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 "&"//But if is "&" ,do not change.public static String encode( String str ){if( str == null){return null;}return str.replaceAll( "&(?!amp;)", "&" );}//Decode "&" in string to "&"public static String decode( String str ){if( str == null ){return null;}return str.replaceAll( "&", "&" );}}
演示:
public static void main(String[] args) throws Exception {String[] array = new String[]{"超人", "abc", "真的&可以区分", "怀&疑", "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, 真的&可以区分, 怀&疑, gg]
超人&abc&真的&可以区分&怀&疑&gg
[超人, abc, 真的&可以区分, 怀&疑, gg]
Error Demo!
[超人, abc, 真的, 可以区分, 怀, amp;疑, gg]
从上面的Right Demo结果可以看出:数组 -> 字符串 -> 数组
如果数组项中包含 "&",可以正确还原。
如果数组项中包含 "&",会还原为 "&"。(但其表达的意义是一致的)
SplitConcatWithAMP----Array转换为String,连接;String转换为Array,切割相关推荐
- php 字符串放到数组中,在PHP中将字符串转换为数组(Converting string into array in php)...
在PHP中将字符串转换为数组(Converting string into array in php) 我有像下面的字符串 ["Day1"]["Morning" ...
- Javascript 对象二(Number、String、Boolean、Array、Date、Math、RegExp)
Javascript 对象二(Number.String.Boolean.Array.Date.Math.RegExp) Number 数字 对象 String 字符串 对象 Boolean 布尔 对 ...
- EXT核心API详解(二)-Array/Date/Function/Number/String
EXT核心API详解(二)-Array/Date/Function/Number/String Array类 indexOf( Object o ) Number object是否在数组中,找不到返 ...
- php simplexmlelement array,php中SimpleXMLElement 对象转换为数组
PHP 提供了 simplexml_load_string 方法用来解析 XML 格式的字符串,并返回 SimpleXMLElement 对象,不过一般数组是更为适用的,所以也会有转换为普通数组的需求 ...
- 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 ...
- ES10:Object.fromEntries()、String.prototype.trim()、Array.prototype.flat()、Array.prototype.flatMap()
Object.fromEntries(iterable) 把键值对列表(二维数组)转换为一个对象,这个方法和 Object.entries() 相对 Object.fromEntries([['foo ...
- 字符串html在线互转,将string 的字符串转换为HTML的两种方法
将string 的字符串转换为HTML的两种方法 采用jquery对象转换 采用原生javascript中DOMParser的parseFromString方法实现 1.采用jquery实现 var ...
- array,arraylist,string的总结
import java.util.ArrayList;/* * 这个程序是对array,arraylist,string的总结 * 1.创建 *(1)数组:分为动态初始化和静态初始化.(动态初始化时其 ...
- 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 ...
- 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 ...
最新文章
- 网络营销——网站在网络营销优化中不收录了怎么办呢?
- Web-Lesson07-JS的函数及作用域
- SAP UI5 sap-ui-core.js的加载逻辑
- 前端学习(2532):vuex mutation
- mysql 创建表check如何使用_MySQL怎么使用check约束
- Mysql数据库引擎快速指南
- matlab pdist如何实现,在MATLAB版本7中相当于pdist2
- 位操作符:与,或,异或 狼羊菜
- c语言聊天程序代码,今天打的代码。基于TCP的聊天程序。
- JsonCpp的使用
- Windows域策略 统一设置本地管理员密码 【全域策略生效】
- 洛谷 P2420 让我们异或吧 解题报告
- 数据库~Mysql里的Explain说明
- LoRaWAN网络协议与LoRa私有协议相比有哪些优势
- vxe-table checkbox range 实现复选框鼠标快速选择
- Flying-Saucer使用HTML或者FTL(Freemarker模板)生成PDF
- 天翼云 Ubuntu 22.04.1 LTS apt换源 (阿里云)
- PyQt5制作一个爬虫小工具,爬取雪球网上市公司的财务数据
- windows XP无法使用suspend to RAM (s3)功能的解决过程
- 扎好篱笆桩:三大运营商守住网络安全“命门”