当前位置:首页 » 编程语言 » java字符串解析

java字符串解析

发布时间: 2022-09-03 00:13:54

‘壹’ java字符串解析

String input = ...;
String[] split = input.split("\n");// "\n" 是正解, "/n" 是得不到你想要的结果的!
for(String sp:split)System.out.println(sp.trim());

‘贰’ java 解析-字符串

import java.text.DecimalFormat;

public class StrTest {

public static String[] SpiltString(String str){
char[] allChars = str.toCharArray();
for(int i = 0 ; i<allChars.length ; i++){
if(!( (allChars[i]>='0' && allChars[i]<='9') || allChars[i]=='-' || allChars[i]==',') ){
System.out.println("ERROR");
throw new RuntimeException();
}
}
StringBuffer strArray = new StringBuffer();
String[] firstStr = str.split(",");
for(int i = 0 ; i<firstStr.length ; i++){
if(firstStr[i].indexOf("-") == -1){
strArray.append(firstStr[i]+" ");
}else{
String[] secondStr = firstStr[i].split("-");
try{
int min =Integer.valueOf( secondStr[0] );
int max =Integer.valueOf( secondStr[secondStr.length-1] );
int len = secondStr[0].length();
String newStr = null;
String formatStr = "";
for(int j = min;j<=max;j++){
DecimalFormat df = new DecimalFormat(formatStr.substring(0,len));
newStr =df.format(j);
strArray.append(newStr+" ");
}
}catch(NumberFormatException e){
continue;
}
}
}
return strArray.toString().split(" ");
}

public static void main(String[] args) {
String [] str = SpiltString("001,002-005,007");
for(int i = 0 ; i < str.length;i++){
System.out.print(str[i]+" ");
}
}
}

‘叁’ java 拼接字符和解析字符串

importjava.util.ArrayList;
importjava.util.List;

/**
*CreatedbyAdministratoron2015/6/14.
*/
publicclassTest{
privatestaticList<String>infor;

static{
infor=newArrayList<String>();
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");
}

publicstaticvoidmain(Stringargs[]){
/**
*开始拼接字符串
*/
StringinforString=infor.get(0);
for(inti=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);

Useruser;
String[]userString;
for(inti=0,k=usersInfor.length;i<k;i++){
userString=usersInfor[i].split("\^");
user=newUser();
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);
}


}


staticclassUser{
privateStringid;
privateStringgathering;
privateStringname;
privateStringpayment;
privateStringremark;

publicUser(){

}

publicStringgetId(){
returnid;
}

publicvoidsetId(Stringid){
this.id=id;
}

publicStringgetGathering(){
returngathering;
}

publicvoidsetGathering(Stringgathering){
this.gathering=gathering;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicStringgetPayment(){
returnpayment;
}

publicvoidsetPayment(Stringpayment){
this.payment=payment;
}

publicStringgetRemark(){
returnremark;
}

publicvoidsetRemark(Stringremark){
this.remark=remark;
}

@Override
publicStringtoString(){
return"[id:"+id+",gathering:"+gathering+",name:"+name+",payment:"+payment+",remark:"+remark+"]";
}
}
}

‘肆’ java字符串如何解析成能运行的java代码

java字符串如何解析成运行的java代码
有些情况下,不得不动态运行Java代码,以便提供更加灵活的方式,以下代码可参考(在JDK 1.5+平台上运行通过):

public static void main(String[] args) {
int i = 10;
String code = "System.out.println(\"Hello World!\"+(13+2*5/3));";
code += "for(int i=0;i<" + i + ";i++){";
code += " System.out.println(Math.pow(i,2));";
code += "}";
try {
run(code);
} catch (Exception e) {
e.printStackTrace();
}
}

private synchronized static File compile(String code) throws Exception {
File file = File.createTempFile("JavaRuntime", ".java", new File(System.getProperty("user.dir")));
file.deleteOnExit();
// 获得类名
String classname = getBaseFileName(file);
// 将代码输出到文件
PrintWriter out = new PrintWriter(new FileOutputStream(file));
out.println(getClassCode(code, classname));
out.close();
// 编译生成的java文件
String[] cpargs = new String[] { "-d",
System.getProperty("user.dir") + "\\WebRoot\\WEB-INF\\classes",
file.getName() };
int status = Main.compile(cpargs);

if (status != 0) {
throw new Exception("语法错误!");
}
return file;

}

private static synchronized void run(String code) throws Exception {
String classname = getBaseFileName(compile(code));
new File(System.getProperty("user.dir")
+ "\\WebRoot\\WEB-INF\\classes\\" + classname + ".class")
.deleteOnExit();
try {
Class cls = Class.forName(classname);
Method main = cls.getMethod("method", null);
main.invoke(cls, null);
} catch (Exception se) {
se.printStackTrace();
}
}

private static String getClassCode(String code, String className) {
StringBuffer text = new StringBuffer();
text.append("public class " + className + "{\n");
text.append(" public static void method(){\n");
text.append(" " + code + "\n");
text.append(" }\n");
text.append("}");
return text.toString();
}

private static String getBaseFileName(File file) {
String fileName = file.getName();
int index = fileName.indexOf(".");
String result = "";
if (index != -1) {
result = fileName.substring(0, index);
} else {
result = fileName;
}
return result;
}

‘伍’ Java 字符串解析,任意组合。

publicclassTest{
publicstaticvoidmain(String[]argv){
Stringstr="A,B,C,D,E";
String[]arr=str.split(",");
Stringresult="";
for(inti=0;i<arr.length;i++){
for(intj=i+1;j<arr.length;j++){
if(i<arr.length-2){
result+=arr[i]+arr[j]+",";
}else{
result+=arr[i]+arr[j];
}
}
}
System.out.println(result);
}
}

//输出结果:

AB,AC,AD,AE,BC,BD,BE,CD,CE,DE

‘陆’ java中怎么把字符串解析成10进制数

java.lang.Integer中有一个方法:valueOf(String s,int radix),其中s为传入的参数,radix为要将这个字符串中每一个数将被识别成什么进制的数,不过无论字符串中的每一位被是识别成什么都不重要,结果是它都转换成了10进制数。比如:
定义一个字符串val s:String = "924"
再定义一个变量val z = Integer.valueOf(s,10),即可把字符串s中每一位数识别为10进制数,即‘9’就是十进制中的9,‘2’就是十进制中的2,‘4’就是10进制中的4,所以最后转换成10进制数依旧为924
但是如果将变量z定义成val z = Integer.valueOf(s,16),即可把字符串s中每一位数识别为16进制数,即‘9’就是16进制中的9,‘2’就是16进制中的2,‘4’就是16进制中的4,所以转换成10进制数为2340

‘柒’ Java解析字符串

importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;publicclassTest{ publicstaticvoidmain(String[]args)throwsException{ Stringstr="{"id":["fdsfadf","hgfhhgjghj","gfhfghfghgf"]}"; JSONObjectjb=JSONObject.fromObject(str); JSONArrayja=jb.getJSONArray("id"); for(inti=0;i

‘捌’ JAVA字符串解析

给代码,把文本存在input.txt里

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.*;


publicclassdemo{

publicstaticvoidmain(String[]args)throwsFileNotFoundException{
Map<String,Map<String,List<String>>>map=newHashMap<String,Map<String,List<String>>>();
Scannersc=newScanner(newFile("input.txt"));
StringouterKey=newString();
while(sc.hasNext()){
Stringline=sc.nextLine();
if(line.startsWith("POS:")){
outerKey=line.replace(":","");
if(!map.containsKey(outerKey)){
map.put(outerKey,newHashMap<String,List<String>>());
}
}elseif(line.startsWith("MAC")){
String[]lines=line.split(":");
Map<String,List<String>>innerMap=map.get(outerKey);
if(innerMap.containsKey(lines[0])){
innerMap.get(lines[0]).add(lines[1]);
}else{
List<String>list=newArrayList<String>();
list.add(lines[1]);
innerMap.put(lines[0],list);
}
}
}
sc.close();
StringBuildersb=newStringBuilder();
for(Stringouterkey:map.keySet()){
sb.append(outerkey+" ");
Map<String,List<String>>innerMap=map.get(outerkey);
for(Stringinnerkey:innerMap.keySet()){
sb.append(innerkey+":");
for(Stringvalue:innerMap.get(innerkey)){
sb.append(value+"");
}
sb.append(" ");
}
}
System.out.println(sb.toString());
}

}

运行结果:

POS1

MAC2:A2 A22

MAC1:A1 A11

MAC3:A3 A33

POS2

MAC2:B2 B22

MAC1:B1 B11

MAC3:B3 B33

热点内容
我的世界电脑版进pe服务器 发布:2025-01-15 21:33:57 浏览:294
网页游戏吃什么配置 发布:2025-01-15 21:27:58 浏览:65
安卓怎么转移数据华为 发布:2025-01-15 21:03:02 浏览:141
软件打印反馈单脚本错误 发布:2025-01-15 21:01:24 浏览:178
如何进cs里的练枪服务器 发布:2025-01-15 21:00:07 浏览:979
苹果手机存储芯片 发布:2025-01-15 20:52:02 浏览:163
盲人读屏软件安卓哪个好 发布:2025-01-15 20:47:13 浏览:729
炸图脚本 发布:2025-01-15 19:56:07 浏览:429
八字源码 发布:2025-01-15 19:54:47 浏览:372
服务器可以变电脑使用吗 发布:2025-01-15 19:40:29 浏览:202