假设目前有下面这样一个字符串,String =“Subrack No.=1, Slot No.=5, Subsystem No.=1, RNC ID=1003, Cell ID=54221, NodeB ID=437, NodeB Name=U_COL_DEH199, Cause=NCP/CCP failure.”
我怎样写程序才能获得NodeB Name=之后的内容,即U_COL_DEH199
我怎样写程序才能获得NodeB Name=之后的内容,即U_COL_DEH199
调试欢乐多
public static void main(String args[]) {
String str ="Subrack No.=1, Slot No.=5, Subsystem No.=1, RNC ID=1003, Cell ID=54221, NodeB ID=437, NodeB Name=U_COL_DEH199, Cause=NCP/CCP failure.";
String[] strs = str.split(",");
for(int i = 0; i < strs.length; i++)
{
String temp = strs[i];
if(temp.startsWith(" NodeB Name="))
{
String[] strs1 = temp.split("=");
System.out.println(strs1[1]);
}
}
}
}
String ss = s.split("NodeB Name=")[1];
String sss = ss.split(",")[0];
自己写的比较笨的方法。
System.out.println(str.replaceFirst(".*NodeB Name=([^,]*).*", "$1"));
import java.util.regex.*;
public class MyRegex2 { public static void main(String[] args) {
String s="Subrack No.=1, Slot No.=5, Subsystem No.=1, RNC ID=1003, Cell ID=54221, NodeB ID=437, NodeB Name=U_COL_DEH199, Cause=NCP/CCP failure.";
Matcher m=Pattern.compile("(?<=(NodeB Name=)).*(?=\\,)").matcher(s);
String s1 = null;
if(m.find())
s1=m.group(); System.out.println(s1);
}
}
public static void main(String[]args){
String str ="Subrack No.=1, Slot No.=5, Subsystem No.=1, RNC ID=1003, Cell ID=54221, NodeB ID=437, NodeB Name=U_COL_DEH199, Cause=NCP/CCP failure.";
String[] temp = str.split(",");
String flag=" NodeB Name=";
for(int i= 0;i < temp.length;i++){
if(temp[i].startsWith(flag)){
System.out.println(temp[i]);
break;
}
}
}
}
String str ="Subrack No.=1, Slot No.=5, Subsystem No.=1, RNC ID=1003, Cell ID=54221, NodeB ID=437, NodeB Name=U_COL_DEH199, Cause=NCP/CCP failure.";
Map map = new HashMap();
String[] strs = str.split(",");
for(int i = 0; i < strs.length; i++)
{
map.put(strs[i].split("=")[0],strs[i].split("=")[1]);
}
System.out.println(map.get("NodeB Name"));
str = str.replace(", ", "\n");
str = str.replace(" ", "_");
try {
Properties prop = new Properties();
prop.load(new ByteArrayInputStream(str.getBytes()));
System.out.println(prop.getProperty("NodeB_Name"));
} catch (Exception e) {
e.printStackTrace();
}