cekayyy
cekayyy

Reputation: 3

Cut a specific part of String in loop

I have a String like this:

String content = "{"begin"bf3b178a70.jpg","end",....},{"id":,"f06190e8938.jpg","end",....}"

and i want to cut out the image ID like this:

bf3b178a70.jpg
f06190e893.png

and after that, i want compose the image ID with a new url like this:

url.com/image/bf3b178a70.jpg
url.com/image/f06190e893.png

I begin with substring() to cut the first part and with content.split(""id":,"); but i have problems with a string array and normal string. I used the string array with a for-loop, because the real string is very long.

Will someone please help me?

Upvotes: 0

Views: 152

Answers (2)

Jeffrey Hantin
Jeffrey Hantin

Reputation: 36504

At first blush, it looks like your string is formatted as JSON. If that's the case you can use the JSON.org Java parser or one of the many other parsers listed on the JSON.org site to break it down, or just follow the syntax diagrams they give; simple string-chopping is inadvisable since JSON is not a regular language.

I'm going to assume for the moment that you're receiving a JSON array-of-objects (square brackets around, comma separated), and that you are either reading from a file or a Web service, either of which provides an InputStream. If you have something else, you can pass a Reader or a plain String to the JSONTokener constructor, or if you have a byte array you can wrap it in a ByteArrayInputStream and pass that in.

I don't have a JDK handy to even check if this compiles :-) but here goes.

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import org.json.*;

public class ImageListProcessor
{
    public static ArrayList<URL> processList(InputStream toProcess, URL baseURL)
        throws JSONException, MalformedURLException
    {
        JSONTokener toProcessTokener = new JSONTokener(toProcess);
        JSONObject toProcessResponse = new JSONObject(toProcess);
        if (!toProcessResponse.isNull("error")) {
            // it's an error response, probably a good idea to get out of here
            throw new JSONException("Response contains error: " + toProcessResponse.get("error"));
        }
        JSONArray toProcessArray = toProcessResponse.getJSONArray("items");
        int len = toProcessArray.length();
        ArrayList<URL> result = new ArrayList<URL>(len);
        for(int i = 0; i < len; i++) {
            JSONObject imageRecord = toProcessArray.getJSONObject(i);
            String imagePath = imageRecord.getString("image");
            // if you want to remove the date portion of the path:
            imagePath = imagePath.substring(1 + imagePath.lastIndexOf('/'));
            URL combinedURL = new URL(baseURL, imagePath);
            result.add(combinedURL);
        }
        return result;
    }
}

Upvotes: 1

TAR515
TAR515

Reputation: 369

Try something like this:

import java.util.regex.*;

public class ReplaceDemo {
    public static void main(String[] args) {
    String input = 
              "User clientId=23421. Some more text clientId=33432. This clientNum=100";

    Pattern p = Pattern.compile("(clientId=)(\\d+)");
    Matcher m = p.matcher(input);

    StringBuffer result = new StringBuffer();
    while (m.find()) {
        System.out.println("Masking: " + m.group(2));
        m.appendReplacement(result, m.group(1) + "***masked***");
    }
    m.appendTail(result);
    System.out.println(result);
}
}

Upvotes: 0

Related Questions