2

I have this line in my output:

current state of: "admin" 

I want to remove the double quotes around admin.

How can I do it using Java? I want to print only admin.

George G
  • 7,443
  • 12
  • 45
  • 59
user1795999
  • 301
  • 2
  • 5
  • 15
  • Would you like to share some code snippets with us to show what you are actually trying to achieve? – Kamran Ahmed Oct 21 '14 at 02:57
  • 1
    I think, knowing about what are the possible variations in the `output` is important before answering. Otherwise `System.out.print("admin");` is the perfect answer for this question, isn't it? – Kamran Ahmed Oct 21 '14 at 03:03
  • How variable is the prefix string? Could there be nested quotes? For simple cases, see [String.lastIndexOf](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#lastIndexOf(int)), for instance. – Ken Y-N Oct 21 '14 at 03:08

4 Answers4

2

You may try something like:

public class Main {
    public static void main(String[] args) {
        String outputLine = "current state of: \"admin\"";

        outputLine = outputLine.substring(19, outputLine.length() - 1);
        System.out.print(outputLine);
    }
}
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
  • 3
    Please try to post answers that actually compile. Remember that when you answer a Stack Overflow question, you're teaching someone how to do something. There are other mistakes here, not just the failure to compile. – Dawood ibn Kareem Oct 21 '14 at 03:02
  • OK, that looks a lot better now. Removing my downvote. But still wondering whether you meant `println` on the last substantive line. – Dawood ibn Kareem Oct 21 '14 at 03:16
  • Thanks, no I meant `print`. There is only one line in the output, I don't see any reason to put a `\n` at the end of it. – Kamran Ahmed Oct 21 '14 at 03:20
2

Assuming that your pattern is something like current state of: "nameYouWantToExtract". You can use a regular expression to extract what matches your pattern:

Pattern p = Pattern.compile("^current state of: \"([a-zA-Z]+)\"$");
Matcher m = p.matcher("current state of: \"nameYouWantToExtract\"");

if (m.find()) {
    System.out.println(m.group(1));
}

Parenthesis around [a-zA-Z]+ are creating a group, that's why you can extract the value being matched by [a-zA-Z]+.

You may change it to [a-zA-Z0-9]+ to be able to extract out numbers too, if applicable.

Know more about regular expressions

Federico Nafria
  • 1,397
  • 14
  • 39
1

This can be accomplished using regular expressions. The pattern you are interested in matching is:

current state of: \"([a-zA-Z0-9]*)\"

This pattern contains a group (the part surrounded by parenthesis), which we've defined as ([a-zA-Z0-9]*). This matches zero or more characters belonging to sets a-z, A-Z, or 0-9.

We want to remove all occurrences of this pattern from the string and replace them with the values matched by the group within the pattern. This can be done with a Matcher object by repeatedly calling find(), fetching the value matched by the group, and calling replaceFirst to replace the entire matched text with the value of the group.

Here is some example code:

Pattern pattern = Pattern.compile("current state of: \"([a-zA-Z0-9]*)\"");

String input = "the current state of: \"admin\" is OK\n" + 
               "the current state of: \"user1\" is OK\n" + 
               "the current state of: \"user2\" is OK\n" + 
               "the current state of: \"user3\" is OK\n";

String output = input;
Matcher matcher = pattern.matcher(output);
while (matcher.find())
{
    String group1 = matcher.group(1);
    output = matcher.replaceFirst(group1);
    matcher = pattern.matcher(output);      // re-init matcher with new output value
}

System.out.println(input);
System.out.println(output);

And this is what the output looks like:

the current state of: "admin" is OK
the current state of: "user1" is OK
the current state of: "user2" is OK
the current state of: "user3" is OK

the admin is OK
the user1 is OK
the user2 is OK
the user3 is OK
trooper
  • 4,444
  • 5
  • 32
  • 32
0

If there are no double quotes in either the prefix string or the value to extract, then the simplest way to do this is with split, something like this.

String[] inputSplit = theInput.split("\"");
String theOutput = inputSplit.length > 1 ? inputSplit[1] : null;
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110