How to convert/parse from String to char in java?
If your string contains exactly one character the simplest way to convert it to a character is probably to call the charAt
method:
char c = s.charAt(0);
Converting String to Character array in Java
Use this:
String str = "testString";
char[] charArray = str.toCharArray();
Character[] charObjectArray = ArrayUtils.toObject(charArray);
How to Convert String to Char and Integer in Java ?
A simple way to achieve that (if you don't want to use Regex) is to do something like this:
String temp ="";
// read every char in the input String
for(char c: input.toCharArray()){
// if it's a digit
if(Character.isDigit(c)){
temp +=c; // append it
}
else{ // at the end parse the temp String
data = Integer.parseInt(temp);
opr = c;
break;
}
}
//test
System.out.println("Input: " + input
+ "\t Data: " + data
+ "\t Opr: " + opr);
Test
Input: 5* Data: 5 Opr: *
Input: 123* Data: 123 Opr: *
How to convert string = \t to char
if ("\\t".equals(sTerminatedBy)) {
separator = '\t';
} else if (null == sTerminatedBy || "".equals(sTerminatedBy)) {
separator = ' ';
} else {
separator = sTerminatedBy.charAt(0);
}
Converting String to char when String is a valid char
If you strip off the 0x
prefix for your hex String
representation, you can use Integer.parseInt
and cast to char
.
See edit below for an alternative (possibly more elegant solution).
String s = "0x7D";
// | casting to char
// | | parsing integer ...
// | | | on stripped off String ...
// | | | | with hex radix
System.out.println((char)Integer.parseInt(s.substring(2), 16));
Output
}
Edit
As njzk2 points out:
System.out.println((char)(int)Integer.decode(s));
... will work as well.
How do I parse a char from a String?
This will give you last char in String whatever the length of String is because we are getting char at position String.legth() - 1
.
String input = "1000 c";
char type = input.charAt(input.length()-1);
System.out.println(type);
How to convert char to String in tokenizer in Java
Try
char party = st.nextToken().charAt(0);
Convert character to ASCII numeric value in java
Very simple. Just cast your char
as an int
.
char character = 'a';
int ascii = (int) character;
In your case, you need to get the specific Character from the String first and then cast it.
char character = name.charAt(0); // This gives the character 'a'
int ascii = (int) character; // ascii is now 97.
Though cast is not required explicitly, but its improves readability.
int ascii = character; // Even this will do the trick.
Related Topics
How Does Java Store Strings and How Does Substring Work Internally
Java: Object to Byte[] and Byte[] to Object Converter (For Tokyo Cabinet)
How to Bind an Object List with Thymeleaf
"File Not Found" When Running New Libgdx Project
Does Java Support Inner/Local/Sub Methods
How to Test If My Font Is Rendered Correctly in PDF
Use of '? Extends ' and '? Super ' in Collection Generics
Unexpected Type Resulting from the Ternary Operator
How to Clear or Empty a Stringbuilder
Multiple Packages in Context:Component-Scan, Spring Config
How to Get Current Working Directory in Java
What's the Default Value of Char
Improvednamingstrategy No Longer Working in Hibernate 5
Finding Number of Cores in Java
Generic Wildcard Types Should Not Be Used in Return Parameters