📄 stringseparator.java
字号:
package com.cownew.ctk.common.stringSep;
import java.util.ArrayList;
import java.util.List;
/**
* 字符串拼接解析类
*
* @author yangzk
*
*/
public class StringSeparator
{
/**
* 将List中的每一项调用toString方法得到的字符串拼接成一个字符串
*
* @param strList
* @return
*/
public static String pack(List strList)
{
StringBuffer sb = new StringBuffer();
for (int i = 0, n = strList.size(); i < n; i++)
{
String s = strList.get(i).toString();
s = s.replaceAll(">", ">>");
s = s.replaceAll(";", ">;");
sb.append(s).append(";");
}
return sb.toString();
}
/**
* 将strArray拼接成一个字符串
* @see public static String pack(List strList)
* @param strArray
* @return
*/
public static String pack(String[] strArray)
{
StringBuffer sb = new StringBuffer();
for (int i = 0, n = strArray.length; i < n; i++)
{
String s = strArray[i];
s = s.replaceAll(">", ">>");
s = s.replaceAll(";", ">;");
sb.append(s).append(";");
}
return sb.toString();
}
/**
* 将由pack方法返回的字符串重新解析成字符串,然后以String类型保存到List中返回
*
* @param value
* @return
* @throws ParserException
*/
public static List unPack(String value) throws ParserException
{
List list = new ArrayList();
Lexer lexer = new Lexer(value);
for (Token t = lexer.next(); t != null; t = lexer.next())
{
if (t instanceof WordToken)
{
list.add(((WordToken) t).getValue());
}
}
return list;
}
/**
* 将由pack方法返回的字符串重新解析成字符串,然后以String类型保存到数组中返回
* @param value
* @return
* @throws ParserException
*/
public static String[] unPackAsArray(String value) throws ParserException
{
List list = unPack(value);
String[] ret = new String[list.size()];
for(int i=0,n=list.size();i<n;i++)
{
ret[i] = (String) list.get(i);
}
return ret;
}
public static void main(String[] args)
{
try
{
String s1 = "n>;a>;;";
System.out.println(StringSeparator.unPack(s1));
} catch (Exception e)
{
e.printStackTrace();
}
if(true)return;
List inList = new ArrayList();
inList.add("ab哈哈d>3>;3");
inList.add("\ndde>;3");
inList.add("aa");
inList.add("c33>d;>>\n22d");
String t = pack(inList);
System.out.println("合并保存后的值:" + t);
System.out.println("解析结果");
try
{
List outList = unPack(t);
for (int i = 0, n = outList.size(); i < n; i++)
{
System.out.println(outList.get(i));
}
} catch (ParserException e)
{
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -