novayhulk14
novayhulk14

Reputation: 113

How can I turn "'\n'" (String) into '\n' (char) in java

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?

Upvotes: 0

Views: 634

Answers (1)

Aniket Sahrawat
Aniket Sahrawat

Reputation: 12937

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);

Upvotes: 3

Related Questions