dgelinas21
dgelinas21

Reputation: 641

Trying to pull multiple String parses into 1 String

I am trying to extract 2 specific pieces of data from one long string. Both of the values are after an "=", but the issue is I can't solely just split data on every "=" because some of the data I need contains them as well:

example: "{col1=RND393JKDN, col2=DJW//39ndo==8}
desired format: "RND393JKDN, DJW//39ndo==8" 

Below is the code I have written to fix do so, but the other piece is that I am unsure of how many columns will be inputted, so I have to be able to handle an unknown amount of columns like : "{col1=value1, col2=value2, col3=value3}"

for(int i = 0; i < finalResult.size(); i+=modval) { 
    //below forces the result to get stored in below variable as a String type
    String resulttemp = finalResult.get(i).toString();

    //below is only for
    for(int z = 0; z < columnHeaders.size(); z++) {
        String newStr=finalResult.get(i).replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\{", "").replaceAll("\\}", "");

        String newStr2;
        String newStr3;

        String find = "=";

        newStr = (newStr.substring(newStr.indexOf(find)+find.length()));
        newStr2 = (newStr.substring(newStr.indexOf(find)+find.length()));
        newStr3 = (newStr2.substring(newStr.indexOf(find)+find.length()));

        pw.printf("%s\n", newStr, newStr3);
    }
}

Upvotes: 0

Views: 66

Answers (4)

Tim M.
Tim M.

Reputation: 608

Regexes are a wonderful thing. Here's an example of how you can use Java's built-in Matcher and Pattern classes to do exactly what you need in a simple manner.

public static void main(String[] args)
{
    String input = "{col1=RND393JKDN, col2=DJW//39ndo==8}";
    //Remove the {}
    str = str.substring(1, str.length() - 1);
    //Match "col", then any number, then an = sign,
    //then capture everything from the = to the , as "content"
    Pattern p = Pattern.compile("col\\d+=(?<content>[^,]+)");
    Matcher m = p.matcher(str);
    //The target string.
    String out = "";
    //While a match for the pattern exists, i.e. do for all matches of the pattern.
    while (m.find())
    {
        //String together desired parts into the output string, separated by ", "
        out += m.group("content") + ", ";
    }
    //Remove trailing ", "
    out = out.substring(0, out.length() - 2);
}

Upvotes: 1

cen
cen

Reputation: 2943

If possible, consider changing the input format. If not, here is a naive and simple approach.

import java.util.List;
import java.util.ArrayList;



public class HelloWorld
{
  public static void main(String[] args)
  {
    String s = "{col1=RND393JKDN, col2=DJW//39ndo==8}";
        s=s.substring(1, s.length()-1); //remove {}
        s.replaceAll(" ", "");
        String[] explode = s.split(",");
        List<String> result = new ArrayList<String>();
        for (int i=0; i<explode.length; i++) {
            String keyAndValue = explode[i];
            int at = keyAndValue.indexOf("=");
            String value = keyAndValue.substring(at+1);
            result.add(value);
        }
        String finalResult = "";
        for (String val : result) {
            finalResult+=val+", ";
        }
        finalResult=finalResult.substring(0, finalResult.length()-2);
        System.out.println(finalResult);
  }
}

Basically, break into key+value tokens, find first = char and get the value from there. Simple enough. Unless there are some other unexpected format rules. I expect space and comma to not appear in key and value.

Upvotes: 1

baao
baao

Reputation: 73251

I'd split the original string by comma, then replace what's in front of the =:

public static void main(String[] args) {
    String f = "{col1=RND393JKDN, col2=DJW//39ndo==8}";
    String[] fs = f.replaceAll("^\\{|}$", "").split(",");
    String[] res = new String[fs.length];

    for (int i = 0; i < fs.length; i++) {
        res[i] = fs[i].replaceAll("\\s?col\\d+=", "");
    }
    System.out.println(String.join(", ", res)); // RND393JKDN, DJW//39ndo==8
}

Upvotes: 0

kleopi
kleopi

Reputation: 460

Build yourself a list of the = 's positions. Depending on how many you need, build substrings (you already did this).

Upvotes: 0

Related Questions