在Java中拆分String的有效方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Java中拆分String的有效方法相关的知识,希望对你有一定的参考价值。

我有一些像这种模式的输入:

org.apache.felix.dm;版本= “[4.1,5)”,org.osgi.framework;版本= “[1.5,2)”,org.osgi.service.cm;版本=“[1.3,2) ”,org.osgi.service.deploymentadmin;版本= “[1.0,2)”,org.osgi.service.deploymentadmin.spi;版本= “[1.0,2)”,org.osgi.service.event;版本= “[1.2,2)”,org.osgi.service.log;版本= “[1.3,2)”,org.osgi.service.metatype;版本= “[1.3,2)”,org.osgi.util。跟踪器;版本= “[1.4,2)”

我需要拆分并将其存储在对象或列表中,如下所示:

  • Object1: String name = "org.apache.felix.dm"; String[] versions = {4.1.0, 5.0.0}
  • 对象2: String name = "org.osgi.framework"; String[] versions = {1.5.0, 2.0.0}
答案

我会分三步解决这个问题

步骤1:

将文本分成名称和版本对。所以你最终会得到

org.apache.felix.dm;version="[4.1,5)"
org.osgi.framework;version="[1.5,2)"
org.osgi.service.cm;version="[1.3,2)"
org.osgi.service.deploymentadmin;version="[1.0,2)"
org.osgi.service.deploymentadmin.spi;version="[1.0,2)"
org.osgi.service.event;version="[1.2,2)"
org.osgi.service.log;version="[1.3,2)"
org.osgi.service.metatype;version="[1.3,2)"
org.osgi.util.tracker;version="[1.4,2)"

为此,您可以执行以下操作:

String text = "org.apache.felix.dm;version="[4.1,5)",org.osgi.framework;version="[1.5,2)",org.osgi.service.cm;version="[1.3,2)",org.osgi.service.deploymentadmin;version="[1.0,2)",org.osgi.service.deploymentadmin.spi;version="[1.0,2)",org.osgi.service.event;version="[1.2,2)",org.osgi.service.log;version="[1.3,2)",org.osgi.service.metatype;version="[1.3,2)",org.osgi.util.tracker;version="[1.4,2)"";
    String[] strArray = text.split(",(?=([^"]*"[^"]*")*[^"]*$)");

strArray现在包含对。

for (String string : strArray) {
    System.out.println(string);
}

版画

org.apache.felix.dm;version="[4.1,5)"
org.osgi.framework;version="[1.5,2)"
org.osgi.service.cm;version="[1.3,2)"
org.osgi.service.deploymentadmin;version="[1.0,2)"
org.osgi.service.deploymentadmin.spi;version="[1.0,2)"
org.osgi.service.event;version="[1.2,2)"
org.osgi.service.log;version="[1.3,2)"
org.osgi.service.metatype;version="[1.3,2)"
org.osgi.util.tracker;version="[1.4,2)"

第2步:

为strArray中的每个条目创建一个对象,并将它们存储在一个数组或列表中(这次我选择ArrayList,你可以选择你想要的任何东西):

    String text = "org.apache.felix.dm;version="[4.1,5)",org.osgi.framework;version="[1.5,2)",org.osgi.service.cm;version="[1.3,2)",org.osgi.service.deploymentadmin;version="[1.0,2)",org.osgi.service.deploymentadmin.spi;version="[1.0,2)",org.osgi.service.event;version="[1.2,2)",org.osgi.service.log;version="[1.3,2)",org.osgi.service.metatype;version="[1.3,2)",org.osgi.util.tracker;version="[1.4,2)"";
    String[] strArray = text.split(",(?=([^"]*"[^"]*")*[^"]*$)");  
    ArrayList<MyObject> list = new ArrayList<>();
    for (String string : strArray) {
        String currentPair[] = string.split(";");
        list.add(new MyObject(currentPair[0], currentPair[1]
                .replaceAll(""", "")
                .replaceAll("version=", "")
                .replaceAll("\[", "")
                .replaceAll("\)", "").split(",")));
    }

    for (MyObject myObject : list) {
        System.out.print("Name: " + myObject.name + " - Versions:");
        for (String version : myObject.versions) {
            System.out.print(version + " ");
        }
        System.out.println();
    }

MyObject是你对象的类。

打印以下内容:

Name: org.apache.felix.dm - Versions:4.1 5 
Name: org.osgi.framework - Versions:1.5 2 
Name: org.osgi.service.cm - Versions:1.3 2 
Name: org.osgi.service.deploymentadmin - Versions:1.0 2 
Name: org.osgi.service.deploymentadmin.spi - Versions:1.0 2 
Name: org.osgi.service.event - Versions:1.2 2 
Name: org.osgi.service.log - Versions:1.3 2 
Name: org.osgi.service.metatype - Versions:1.3 2 
Name: org.osgi.util.tracker - Versions:1.4 2 

现在这几乎就是你想要的东西,只剩下将版本号格式化为3点的东西。

第3步:格式化版本字符串有3个点:

因为我写了一个方法并将currentpair [1] ...作为参数

public static void main(String[] args) {
    String text = "org.apache.felix.dm;version="[4.1,5)",org.osgi.framework;version="[1.5,2)",org.osgi.service.cm;version="[1.3,2)",org.osgi.service.deploymentadmin;version="[1.0,2)",org.osgi.service.deploymentadmin.spi;version="[1.0,2)",org.osgi.service.event;version="[1.2,2)",org.osgi.service.log;version="[1.3,2)",org.osgi.service.metatype;version="[1.3,2)",org.osgi.util.tracker;version="[1.4,2)"";
    String[] strArray = text.split(",(?=([^"]*"[^"]*")*[^"]*$)");
    ArrayList<MyObject> list = new ArrayList<>();
    for (String string : strArray) {
        String currentPair[] = string.split(";");
        list.add(new MyObject(currentPair[0], addDots(currentPair[1]
                .replaceAll(""", "")
                .replaceAll("version=", "")
                .replaceAll("\[", "")
                .replaceAll("\)", "").split(","), 3)));
    }

    for (MyObject myObject : list) {
        System.out.print("Name: " + myObject.name + " - Versions:");
        for (String version : myObject.versions) {
            System.out.print(version + " ");
        }
        System.out.println();
    }
}

public static String[] addDots(String toAdd[], int dotcount) {
    String[] formattedVersions = new String[toAdd.length];
    for (int i = 0; i < toAdd.length; i++) {
        ArrayList<String> digits = new ArrayList<>();
        digits.add(toAdd[i]);
        if(toAdd[i].contains(".")) {
            digits = new ArrayList<>(Arrays.asList(toAdd[i].split("\.")));
        }
        for(int x = digits.size(); x < dotcount; x++) {
            digits.add("0");
        }
        String curretnFormattedVersion = "";
        for (int j = 0; j < digits.size() - 1; j++) {
            curretnFormattedVersion += digits.get(j) + ".";
        }
        curretnFormattedVersion += digits.get(dotcount - 1);
        formattedVersions[i] = curretnFormattedVersion;
    }
    return formattedVersions;
}

现在打印以下内容:

Name: org.apache.felix.dm - Versions: 4.1.0 5.0.0 
Name: org.osgi.framework - Versions: 1.5.0 2.0.0 
Name: org.osgi.service.cm - Versions: 1.3.0 2.0.0 
Name: org.osgi.service.deploymentadmin - Versions: 1.0.0 2.0.0 
Name: org.osgi.service.deploymentadmin.spi - Versions: 1.0.0 2.0.0 
Name: org.osgi.service.event - Versions: 1.2.0 2.0.0 
Name: org.osgi.service.log - Versions: 1.3.0 2.0.0 
Name: org.osgi.service.metatype - Versions: 1.3.0 2.0.0 
Name: org.osgi.util.tracker - Versions: 1.4.0 2.0.0 

这应该是你需要的。

我不知道这是否有效,但从代码的长度我会说不,但嘿,它的工作:D

另一答案

只需使用此代码,此代码取决于问题中的输入字符串

package com.java;

public class Test {

public static void main( String[] args ) {

    String inputString = Test.getString();

    String[] firstSplit = inputString.split("",");
    int counter = 1;

    for (String firstString : firstSplit) {

        String[] secondSplit = firstString.split(";"); 

        for(String secondString : secondSplit) {                

            if(secondString.substring(0,1).equals("o")) {
                System.out.println("Object" + counter++);
                String name = secondString;
                System.out.println("name = " + name);
            }
            if(secondString.substring(0,7).equals("version")) {
                String[] versions = new String[2];
                versions[0] = secondString.substring(10,13) + ".0";
                versions[1] = secondString.substring(14,15) + ".0.0";
                System.out.println("Version = " + versions[0] + ", " + versions[1]);
            }

        }


    }
}


static String getString() {
    return "org.apache.felix.dm;version="[4.1,5)",org.osgi.framework;version="[1.5,2)",org.osgi.service.cm;version="[1.3,2)",org.osgi.service.deploymentadmin;version="[1.0,2)",org.osgi.service.deploymentadmin.spi;version="[1.0,2)",org.osgi.service.event;version="[1.2,2)",org.osgi.service.log;version="[1.3,2)",org.osgi.service.metatype;version="[1.3,2)",org.osgi.util.tracker;version="[1.4,2)"";
}

}

结果将是

Object1
name = org.apache.felix.dm
Version = 4.1.0, 5.0.0
Object2
name = org.osgi.framework
Version = 1.5.0, 2.0.0
Object3
name = org.osgi.service.cm
Version = 1.3.0, 2.0.0
Object4
name = org.osgi.service.deploymentadmin
Version = 1.0.0, 2.0.0
Object5
name = org.osgi.service.deploymentadmin.spi
Version = 1.0.0, 2.0.0
Object6
name = org.osgi.service.event
Version = 1.2.0, 2.0.0
Object7

以上是关于在Java中拆分String的有效方法的主要内容,如果未能解决你的问题,请参考以下文章

需要一种有效的方法来避免使用 Laravel 5 重复代码片段

编写代码片段的更简洁的方法

为啥单管道字符会混淆java拆分方法? [复制]

113Java中String类之字符串文本复杂二次拆分

在 Powershell 中,按记录类型拆分大型文本文件的最有效方法是啥?

在 R 中拆分大型数据集的有效方法