0

Its a simple question, I have a String like this:

String s = "'\n'";  

I want to turn that into a character like this:

char c = '\n';

What can I do?

3
  • 1
    Possible duplicate of How to convert/parse from String to char in java? Commented Feb 13, 2018 at 15:42
  • Is there always a string with one char ("\n" or "a") in it wrapped by two single quotes? Commented Feb 13, 2018 at 15:48
  • 1
    If the string is always like that, why not just use char c = '\n'; instead of picking a single char out of a string? I can't really see any use case for this.. Commented Feb 13, 2018 at 15:56

1 Answer 1

3

There are three characters, first is ', second is \n and third is '. You can get the second one using .charAt(1) since it is zero based indexing:

String s = "'\n'";
char ch = s.charAt(1);
Sign up to request clarification or add additional context in comments.

1 Comment

Or char ch = (char) (1 << 3 | 1 << 1);, what's the point? ;) I guess there are even more confusing ways of assigning a newline to a char

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.