本文目录一览:
java 拼接字符和解析字符串
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2015/6/14.
*/
public class Test {
private static ListString infor;
static {
infor = new ArrayListString();
infor.add("流水号1");
infor.add("收款方帐号1");
infor.add("真实姓名");
infor.add("付款金额1");
infor.add("备注说明1");
infor.add("流水号2");
infor.add("收款方帐号2");
infor.add("真实姓名");
infor.add("付款金额2");
infor.add("备注说明2");
}
public static void main(String args[]) {
/**
* 开始拼接字符串
*/
String inforString = infor.get(0);
for (int i = 1, k = infor.size(); i k; i++) {
if (i % 5 == 0) {
inforString += "|";
} else {
inforString += "^";
}
inforString += infor.get(i);
}
System.out.println("拼接后的字符串是:" + inforString);
/**
* 开始解释字符串
*/
String[] usersInfor = inforString.split("\\|");
System.out.println(usersInfor.length);
User user;
String[] userString;
for (int i = 0, k = usersInfor.length; i k; i++) {
userString = usersInfor[i].split("\\^");
user = new User();
user.setId(userString[0]);
user.setGathering(userString[1]);
user.setName(userString[2]);
user.setPayment(userString[3]);
user.setRemark(userString[4]);
System.out.println("User" + i + "=" + user);
}
}
static class User {
private String id;
private String gathering;
private String name;
private String payment;
private String remark;
public User() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGathering() {
return gathering;
}
public void setGathering(String gathering) {
this.gathering = gathering;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPayment() {
return payment;
}
public void setPayment(String payment) {
this.payment = payment;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "[id:" + id + ",gathering:" + gathering + ",name:" + name + ",payment:" + payment + ",remark:" + remark + "]";
}
}
}
java解析字符串 算术表达式求值
Java 有一个jar包 叫 groovy
groovy可以实现动态执行 String格式的算数表达式
public static void main(String[] args) throws Exception{
String str1 = "1 + 2 * 3"; //表达式1 固定表达式
GroovyShell groovyShell = new GroovyShell();
Object value = groovyShell.evaluate(str1);
System.out.println(value);
String str2 = "A + B * C"; //表达式2 动态表达式
Binding binding = new Binding();
binding.setVariable("A",1); //表达式中 所有的A替换为1
binding.setVariable("B",2);//表达式中 所有的B替换为2
binding.setVariable("C",3);//表达式中 所有的C替换为3
GroovyShell groovyShell2 = new GroovyShell(binding);
Object value2 = groovyShell2.evaluate(str2); //str2 实际表达式为 1 + 2 * 3
System.out.println(value2);
}
java解析字符串
package com;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Yuma
{
private ListMap analysisConfig(String config)
{
String[] arr = config.split(";");
String reg = "([^\\(]+)(\\([^\\)]*\\))";
ListMap list = new LinkedListMap();
for(int i = 0; i arr.length; i++)
{
String tmp = arr[i];
MapString, String map = new HashMapString, String();
map.put(tmp.replaceAll(reg, "$1"), tmp.replaceAll(reg, "$2"));
list.add(map);
}
return list;
}
public static void main(String[] args)
{
String ruleConfig = "债券(0000,1200,1100);非标资产(0100);质押式回购(0123@质押式正回购,0123@质押式逆回购)";
Yuma yuma = new Yuma();
ListMap list = yuma.analysisConfig(ruleConfig);
System.out.println(list);
}
}
java 解析json字符串
你好:
后台拆分json
private String interactPrizeAll; //json 使用字符串来接收
方法中的代码:
Gson gson = new Gson();
InteractPrize interactPrize =new InteractPrize();
//gson用泛型 转List数组 多个对象
ListInteractPrize interactPrizeList = gson.fromJson(interactPrizeAll, new TypeTokenListInteractPrize(){}.getType()); //TypeToken,它是gson提供的数据类型转换器,可以支持各种数据集合类型转换
for(int i = 0; i interactPrizeList.size(); i++)
{
interactPrize = interactPrizeList.get(i);//获取每一个对象
}
这一种方法是转单个对象时使用的
//gson 转对象 单个对象
//interactPrize = gson.fromJso(interactPrizeAll,InteractPrize.class);
这个方法是我后台拼的json往前台传的方法
jsonStrAll.append("{"+ "\""+"catid"+"\""+":"+"\""+c.getCatid()+"\""+","+"\""+"catname"+"\""+":"+"\""+c.getCatname()+"\""+","+ "\""+"catdesc"+"\""+":"+"\""+c.getCatdesc()+"\""+","+"\""+"showinnav"+"\""+":"+"\""+c.getShowinnav()+"\""+","+"\""+"sortorder"+"\""+":"+"\""+c.getSortorder()+"\""+","+"level:"+"\""+"0"+"\""+",parent:"+"\""+"0"+"\""+",isLeaf:true,expanded:false,"+"loaded:true},");
你自己挑着用吧!