首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我需要使用Regex表达式将我的常规字符串拆分为以下字符串[暂停]

我需要使用Regex表达式将我的常规字符串拆分为以下字符串[暂停]
EN

Stack Overflow用户
提问于 2019-05-13 07:12:39
回答 1查看 0关注 0票数 0

我的表情是这样的:

Elephanttree Tech Pvt. Ltd, 12th Main Road, 3rd Block, , , ,Rajajinagar, Bengaluru, Karnataka

我需要使用正则表达式输出这样的输出我的字符串必须拆分。

Elephanttree Tech Pvt Ltd  
12th Main Road, 3rd Block  
[empty]   
[empty]   
[empty]   
Rajajinagar  
Bengaluru  
Karnataka  

我试过这样的

{
    line = "Elephanttree Tech Pvt. Ltd, 12th Main Road, 3rd Block, , , ,Rajajinagar, Bengaluru, Karnataka"
    String[] line1 = line.split(",")
    String address= line1[0];
    String cross= line1[1];
    String block= line1[2];
    so on...
}

但我没有得到适当的输出。

任何人都可以建议步骤,逻辑来实现这一目标吗?

EN

回答 1

Stack Overflow用户

发布于 2019-05-13 16:25:49

您可以创建一个方法来返回所需数据的String Array。然而,我将假设输入字符串包含地址1和地址2,这就是您希望将两个分隔的字符串组件附加在一起的原因。以下是您可以完成手头任务的方法。代码评论很好,所以你知道发生了什么:

public static String[] splitStringData(String inputString) {
    // No string is supplied then return a null String Array
    if (inputString == null || inputString.equals("")) {
        return null;
    }

    /* Split the input string based on either ",\\s+" (comma 
       followed by ANY number of spaces) or "," (comma with
       no spaces before or after it).     */
    String[] stringParts = inputString.split(",\\s+|,");

    /* Declare and initialize our returnable String Array.
       We subtract 1 from the declared length because two
       of the split elements will be appended together for
       the Address.   */
    String[] data = new String[stringParts.length -1]; 

    /* Index incremetor for the above array because this
       data array will be one less than the split array. */
    int indexCounter = 0; 

    // Iterate through the split (stringParts) array to acquire data
    for (int i = 0; i < stringParts.length; i++) {
        /* Upon each iteration, place each split element into
           the variable s. If the split element contains nothing 
           (is null string) then place the string "[empty]" into
           the variable otherwise place the actual split element 
           string there. Terary Operator is used for this.   */
        String s = stringParts[i].equals("") ? "[empty]" : stringParts[i];

        /* If the current index is 2 then we want to specially process
           this split element. We want to append Address 2 to Address 1.  */
        if (i == 2) {
            // We want to add this string element to the previous 
            // data array element so we subtract 1 from the index
            // and we don't increment the indexCounter variable.
            data[indexCounter - 1]+= ", " + s;
        }
        else {
            // Add the parts element to the data Array.
            data[indexCounter] = s;
            indexCounter++; // Increment the index counter for the data array
        }
    }
    // Return the created String Array.
    return data;
}

要使用上述方法,您可以这样做:

// The string to create array data from.
String strg = "Elephanttree Tech Pvt. Ltd, 12th Main Road, 3rd Block, , , ,Rajajinagar, Bengaluru, Karnataka";
String[] data = splitStringData(strg);

// Display our data Array
for (int j = 0; j < data.length; j++) {
    System.out.println(data[j]);
}

当然,控制台窗口中显示的输出将是:

Elephanttree Tech Pvt. Ltd
12th Main Road, 3rd Block
[empty]
[empty]
[empty]
Rajajinagar
Bengaluru
Karnataka
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档