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.

3
  • Would you like to share some code snippets with us to show what you are actually trying to achieve? Commented Oct 21, 2014 at 2: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? Commented Oct 21, 2014 at 3:03
  • How variable is the prefix string? Could there be nested quotes? For simple cases, see String.lastIndexOf, for instance. Commented Oct 21, 2014 at 3:08

4 Answers 4

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
OK, that looks a lot better now. Removing my downvote. But still wondering whether you meant println on the last substantive line.
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.
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

Comments

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

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.