java newbie
java newbie

Reputation: 31

how to delete empty line and rest of the character in java

I want to delete empty line and rest of the character from my string, I would like to parse particular value alone from the string.

I want this value alone 23243232 from my string, after product price I've have empty line space and again I've some character so I'm using that empty line as delimiter and trying to get product price alone. But I'm getting other values also along with 23243232. Can someone help me to get only 23243232 from this string

String actualResponse = "--sGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArwzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exsGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArw\r\n"
                + "Product-Discription: form-name; productName=\"iPhone\"\r\n" + "Product-Type: Mobile\r\n"
                + "Product-Price: 23243232\r\n" + "\r\n" + "%dsafdfw32.323efaeed\r\n" + "#$#@#@#";

        String productPrice = actualResponse.substring(actualResponse.lastIndexOf("Product-Price:") + 15);
        System.out.println("Printing product price ..." + productPrice);
        String finalString = productPrice.replaceAll(" .*", "");

This is the output I'm getting:

Printing product price ...23243232

%dsafdfw32.323efaeed
#$#@#@#

But I want only 23243232 - this value alone.

Upvotes: 2

Views: 59

Answers (4)

Aditya Singh
Aditya Singh

Reputation: 2453

This is because you are printing the entire sub-string right from index: actualResponse.lastIndexOf("Product-Price:") + 15 to the end of the string.

You need to provide the end index too as a second parameter in substring method.

You need to use this:

int start = actualResponse.lastIndexOf("Product-Price:") + 15;
int end   = actualResponse.indexOf("\r\n", start); // The first "\r\n" from the index `start`
String productPrice = actualResponse.substring(start, end); 

Upvotes: 1

Nghia Do
Nghia Do

Reputation: 2658

Apply Regular Expression for more flexibility.

    String content = "--sGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArwzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exsGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArw\r\n"
                    + "Product-Discription: form-name; productName=\"iPhone\"\r\n" + "Product-Type: Mobile\r\n"
                    + "Product-Price: 23243232\r\n" + "\r\n" + "%dsafdfw32.323efaeed\r\n" + "#$#@#@#";

            String re1 = "\\bProduct-Price:\\s";    // Word 1
            String re2 = "(\\d+)";    // Integer Number 1

            Pattern p = Pattern.compile(re1 + re2, Pattern.DOTALL);
            Matcher m = p.matcher(content);

            while (m.find()) {
                for (int i = 0; i <= m.groupCount(); i++) {
System.out.println(String.format("Group=%d | Value=%s",i, m.group(i)));
                }
            }

It will print out:

Group=0 | Value=Product-Price: 23243232

Group=1 | Value=23243232

Upvotes: 4

Rohit
Rohit

Reputation: 89

first solution came in my mind. its not the best but will solve your problem.

StringBuilder finalString =new StringBuilder();
        for (Character c : productPrice.toCharArray()) {
            if(Character.isDigit(c)){
                finalString.append(c);
            }else{
                break;
            }
        }

Upvotes: 1

paresh
paresh

Reputation: 351

This will give  your final ans...
 String actualResponse ="--sGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArwzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exzoF_y         DmmW5DfupJDtuHlh20LL2SAbWZb8a3exsGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArw\r\n"
                + "Product-Discription: form-name; productName=\"iPhone\"\r\n" + "Product-Type: Mobile\r\n"
                + "Product-Price: 23243232\r\n" + "\r\n" + "%dsafdfw32.323efaeed\r\n" + "#$#@#@#";

String productPrice = actualResponse.substring(actualResponse.lastIndexOf("Product-Price:") + 15);
System.out.println("Printing content lenght..." + productPrice.split("\r\n")[0]);

Upvotes: 0

Related Questions