如何拆分NAME和ADDRESS,并单独打印

时间:2015-04-08 04:41:03

标签: java

我从这个字符串开始:

"NAME-RAHUL KUMAR CHOUDHARY ADDRESS-RAJDHANWAR DISTRICT-GIRIDIH STATE-JHARKHAND PIN CODE-825412"

我想拆分名称和地址,并按照以下方式打印:

NAME:RAHUL KUMAR CHOUDHARY , DDRESS-RAJDHANWAR DISTRICT-GIRIDIH STATE-JHARKHAND PIN CODE-825412. like this 

这是我到目前为止所做的:

String str_colArow3 = colArow3.getContents();
//Display the cell contents

System.out.println("Contents of cell Col A Row 3: \""+str_colArow3 + "\"");

if(str_colArow3.contains("NAME"))
{

}
else if(str_colArow3.contains("ADDRESS"))
{

}

1 个答案:

答案 0 :(得分:0)

String string = "NAME-RAHUL KUMAR CHOUDHARY ADDRESS-RAJDHANWAR DISTRICT-GIRIDIH STATE-JHARKHAND PIN CODE-825412";
String[] parts = string.split("-");

string = "Name: " + parts[1].substring(0, parts[1].length() - 7) 
       + "\nAdress: " + parts[2] + " - " + parts[3]
       + "\nPin Code: " + parts[5];

像这样的东西。查看字符串的split()方法,但是你的字符串格式有点使用它。你必须根据自己的需要进行调整。

编辑:使用不同的字符串输入更好地执行此操作。

String string = "RAHUL KUMAR CHOUDHARY:RAJDHANWAR:GIRIDIH:JHARKHAND:825412";
String[] parts = string.split(":");

string = "Name: " + parts[0] + "\n" 
       + "Address: " + parts[1] + "\n"
       + "District: " + parts[2] + "\n"
       + "State: " + parts[3] + "\n"
       + "Pin Code: " + parts[4] + "\n";