【问题标题】:how to split string by reading text from file如何通过从文件中读取文本来拆分字符串
【发布时间】:2021-07-27 17:13:29
【问题描述】:

我正在尝试在 Linux 上读取 os-release 文件并尝试通过查找 VERSION_ID="12.3" 行来获取操作系统版本。我在下面写了一段代码,但最后在第一次拆分字符串后我无法更进一步。我从 VERSION_ID 拆分到“12.3”,在此之后我再次应用拆分函数并得到“java.lang.ArrayIndexOutOfBoundsException:索引 1 超出长度 0 的范围”错误。 , 需要您的帮助和宝贵的建议。

File fobj2 = new File("C:\\os-release");
if(fobj2.exists() && !fobj2.isDirectory())
{
Scanner sc = new Scanner(fobj2);
while(sc.hasNextLine())
    {
     String line = sc.nextLine();
     //VERSION_ID="12.3"
     if(line.contains("VERSION_ID="))
     {
     System.out.println(" VERSION_ID= " + line );
     String [] ver = line.split("=");
     if(ver.length > 1)
     {
         String [] ver_id = line.split("=");
         System.out.println(" ver_id.length " + ver_id.length );
         System.out.println(" ver_id[0] " + ver_id[0] );
         System.out.println(" ver_id[1] " + ver_id[1] );
         System.out.println(" ver_id[1].length() " + ver_id[1].length() );
                     
         String [] FinalVer1 = ver_id[1].split(".");

         System.out.println(" FinalVer1[1].length() " + FinalVer1[1].length() );
         System.out.println(" FinalVer1[1] " + FinalVer1[1] );
     }
     }                   
}

【问题讨论】:

  • 这相当混乱。我建议您尝试将问题缩小到只有一两行代码,即实际问题/异常。

标签: java arrays string exception split


【解决方案1】:

这是从字符串行收集主要和次要版本值的另一种方法:

String line = "VERSION_ID=\"12.3\"";
    
// Bring line to lowercase to eliminate possible letter case 
// variations (if any) then see if 'version-id' is in the line:
if (line.toLowerCase().contains("version_id")) {
    // Remove all whitespaces and double-quotes from line then
    // split the line regardless of whether or not there are
    // whitespaces before or after the '=' character.
    String version = line.replaceAll("[ \"]", "").split("\\s*=\\s*")[1];
    //Get version Major.
    String major = version.split("[.]")[0];
    // Get version Minor.
    String minor = version.split("[.]")[1];
    
    // Display contents of all variables
    System.out.println("Line:     --> " + line);
    System.out.println("Version:  --> " + version);
    System.out.println("Major:    --> " + major);
    System.out.println("Minor:    --> " + minor);
}

【讨论】:

    【解决方案2】:

    试试这个!

    File fobj2 = new File("C:\\os-release");
    if(fobj2.exists() && !fobj2.isDirectory())
    {
    Scanner sc = new Scanner(fobj2);
    while(sc.hasNextLine())
        {
         String line = sc.nextLine();
         //VERSION_ID="12.3"
         if(line.contains("VERSION_ID="))
         {
         System.out.println(" VERSION_ID= " + line );
         String [] ver = line.split("=");
         if(ver.length > 1)
         {
             String [] ver_id = line.split("=");
             System.out.println(" ver_id.length " + ver_id.length );
             System.out.println(" ver_id[0] " + ver_id[0] );
             System.out.println(" ver_id[1] " + ver_id[1] );
             System.out.println(" ver_id[1].length() " + ver_id[1].length() );
                 
             // Here        
             String [] FinalVer1 = ver_id[1].split("\\.");
    
             System.out.println(" FinalVer1[1].length() " + FinalVer1[1].length() );
             System.out.println(" FinalVer1[1] " + FinalVer1[1] );
         }
         }                   
    }
    

    只需要像这样转义点。

    【讨论】:

      【解决方案3】:

      我认为当您对“VERSION_ID="12.3"" 之类的字符串使用拆分方法时,输出将是:String arr = {"VERSION_ID", "12.3"}。那么,您为什么要在 ver_id 数组上使用带有“=”的下一个拆分。尝试使用“。”,据我了解您正在寻找点后的数字。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-19
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多