假设目前有下面这样一个字符串,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

解决方案 »

  1.   

    public class Test {
        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]);
         }
         }
        }
    }
      

  2.   

    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.";
    String ss = s.split("NodeB Name=")[1];
    String sss = ss.split(",")[0];

    自己写的比较笨的方法。
      

  3.   

    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.";
    System.out.println(str.replaceFirst(".*NodeB Name=([^,]*).*", "$1"));
      

  4.   

    用正则表达式:
    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);
    }
    }
      

  5.   

    public class Test{
    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;
             }
            }
    }
    }
      

  6.   

    取出来全放到map集合里 以便使用其他值的时候直接调用
    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"));
      

  7.   

    另类方法。但是如果你把这些放在一个属性文件中保存,那么我的这个方法就不另类了。 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."; 
    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();
    }