Dev Tamil
Dev Tamil

Reputation: 649

How to remove first line and last line in a String in android

How do i remove first line and last line in this string

String key = "-----BEGIN PUBLIC KEY-----\n" +
                        "MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgE0JOa5WUcifbDnnQWB2WKOOODwq\n" +
                        "JUxu/7fG2BaynwVRSifljrzGjqpS24R0ss3cZZSKfD2GszG0aVd5T1Yvh4kSOzsx\n" +
                        "arj8QUkfW/EL5ClhDv8LVtkErbTU42QLUUTl5izyKZXaHFdBnJZ8jqXk4AlK22mp\n" +
                        "LcMadrpv7SzQJq1HAgMBAAE=\n" +
                        "-----END PUBLIC KEY-----";


I want a output string like this

String key ="MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgE0JOa5WUcifbDnnQWB2WKOOODwq
    JUxu/7fG2BaynwVRSifljrzGjqpS24R0ss3cZZSKfD2GszG0aVd5T1Yvh4kSOzsx
    arj8QUkfW/EL5ClhDv8LVtkErbTU42QLUUTl5izyKZXaHFdBnJZ8jqXk4AlK22mp
    LcMadrpv7SzQJq1HAgMBAAE="

Upvotes: 1

Views: 6658

Answers (3)

Leon
Leon

Reputation: 198

as this "-----BEGIN PUBLIC KEY-----" and this "-----END PUBLIC KEY-----" are alwayes have constant length you can use substring.

String key = "-----BEGIN PUBLIC KEY-----" +
            "MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgE0JOa5WUcifbDnnQWB2WKOOODwq" +
            "JUxu/7fG2BaynwVRSifljrzGjqpS24R0ss3cZZSKfD2GszG0aVd5T1Yvh4kSOzsx" +
            "arj8QUkfW/EL5ClhDv8LVtkErbTU42QLUUTl5izyKZXaHFdBnJZ8jqXk4AlK22mp" +
            "LcMadrpv7SzQJq1HAgMBAAE=" +
            "-----END PUBLIC KEY-----";
    String result;
    result = key.substring(26,key.length()-24);

Upvotes: 0

AwaisMajeed
AwaisMajeed

Reputation: 2314

If it is stored in a String you can simply call

String [] strArr = str.split("\\n");

now ignore strArr[0] and strArr[strArr.length-1], and get the rest.

Upvotes: 5

Darkpingouin
Darkpingouin

Reputation: 196

If its static you could do this :

key = key.replace("-----BEGIN PUBLIC KEY-----", "");
key = key.replace("-----END PUBLIC KEY-----", "");

if its not you could use the split()method on \nmaybe and then just keep the center string

Upvotes: 1

Related Questions