I want to know how to divide a String and seperate it into two variables, one as char and other as integer
Example:
If "C 365" Is my string then char variable will be 'C' and Integer variable will be 365
The char can be extracted with charAt. For the int, just use substring to take the string from the third character (the first is the char and the second is a space), and then parse it:
String str = "C 365";
char ch = str.charAt(0);
int i = Integer.parseInt(str.substring(2));
Related
I have this:
char c = "\ud804\udef4".charAt(0);
char d = "\ud804\udef4".charAt(1);
How will I print c and d as hex Strings ?
I want d804 for c and def4 for d.
It doesn't matter whether the char is a surrogate pair or not. If you have a char, you can convert it to a hex string by Integer.toHexString(), since chars can be implicitly converted to int.
System.out.println(Integer.toHexString(c));
System.out.println(Integer.toHexString(d));
String message = "a";
char message1 = (char) message;
System.out.println(message1);
Gives me an output error,
This should be converted with ease because the string is one character "a"
I know I can do it explicitly sorry, why the two are incompatible to cast if they are storing the same (only one character)?
As you've seen, no, you cannot cast a single character String to a char. But you could extract it explicitly:
String message = "a";
char message1 = message.charAt(0);
No you cannot do that. You can cast a char to Character because the Character object type is the "boxed" version of the char base type.
Character charObject = (Character) 'c';
char charBase = (char) charObject;
actually, because of auto-boxing and auto-unboxing, you don't need the explicit cast:
Character charObject = 'c';
char charBase = charObject;
However, a String is an object type much like any other object type. That means you cannot cast it to char, you need to use the charAt(int index) method to retrieve characters from it.
Beware though that you may want to use codePointAt(int index) instead, since Unicode code points may well extend out of the 65536 code points that can be stored in the 16 bits that a char represents. So please make sure that no characters defined in the "supplementary planes" are present in your string when using charAt(int index).
As in Java any type can be converted to String, it is possibly to directly append characters to a string though, so "strin" + 'g' works fine. This is also because the + operator for String is syntactic sugar in Java (i.e. other objects cannot use + as operator, you would have to use a method such as append()). Do remember that it returns a new string rather than expanding the original "strin" string. Java strings are immutable after all.
You cannot cast a String to a char. Below is a snippet to always pick the first character from the String,
char c = message.charAt(0);
In case you want to convert the String to a character array, then it can be done as,
String g = "test";
char[] c_arr = g.toCharArray(); // returns a length 4 char array ['t','e','s','t']
A String with one char is more akin to a char[1]. Regardless, retrieve the character directly:
String ex = /* your string */;
if (!ex.isEmpty()) {
char first = ex.charAt(0);
}
I have a String s = "abcd" and I want to make a separate String c that is let's say the two first characters of String s. I use:
String s = "abcd";
int i = 0;
String c = s.charAt(i) + s.charAt(i+1);
System.out.println("New string is: " + c);
But that gives error: incompatible types. What should I do?
You should concatenate two Strings and not chars. See String#charAt, it returns a char. So your code is equivalent to:
String c = 97 + 98; //ASCII values for 'a' and 'b'
Why? See the JLS - 5.6.2. Binary Numeric Promotion.
You should do:
String c = String.valueOf(s.charAt(i)) + String.valueOf(s.charAt(i+1));
After you've understood your problem, a better solution would be:
String c = s.substring(0,2)
More reading:
ASCII table
Worth knowing - StringBuilder
String#substring
What you should do is
String c = s.substring(0, 2);
Now why doesn't your code work? Because you're adding two char values, and integer addition is used to do that. The result is thus an integer, which can't be assigned to a String variable.
String s = "abcd";
First two characters of the String s
String firstTwoCharacter = s.substring(0, 2);
or
char c[] = s.toCharArray();
//Note that this method simply returns a call to String.valueOf(char)
String firstTwoCharacter = Character.toString(c[0])+Character.toString(c[1]);
or
String firstTwoCharacter = String.valueOf(c[0])+ String.valueOf(c[1]);
I am working on a ShiftCipher program, and i'm looking to convert a string sentence (" This is an example") to chars, to i can shift the sentence over 2 letters.
Input: "THIS IS AN EXAMPLE"
output:"VJKU KU CP GZCORNG"
with the spaces intact. But i'm not sure how I can convert it to a char, shift the text and then convert it back into a char.
First you have to convert string into char array.
Do this:
String str = "Your input";
char[] charArray = str.toCharArray();
Then you will have to loop through every single char and shift it over by 2.
for(int i = 0; i < charArray.length; i++)
charArray[i] += 2;
And then convert the char array with shifted characters back to string.
String output = new Strin(charArray);
And there you have it.
I do advice you read up on String class but if you do not and simply copy my answer, then no one will cry for you because you fail the class by not putting effort into homework.
I looked for this about an hour now, but couldn't get any advise specific to my problem. What I'd like to do is take a string of 0's and 1's and manipulate a char that it fits the given String pattern. For example:
char c = 'b'
String s = "00000000 01100001";
Now I'd like to manipulate the bits in c, so that they match the bit pattern specified in s. As result c would be printed as 'a' (if I'm not completely wrong about it). Any help appreciated!
You can do
char a = (char) Integer.parseInt("0000000001100001", 2);
To do the conversion from binary string to Integer, use parseInt with the 2nd argument as 2.
int temp = Integer.parseInt("01100001", 2);
You can modify with binary operators (&,|,^), but if what you really want is to just assign a variable, you can do it with casts.
char c = 'c';
System.out.println((char)(c&temp));
System.out.println((char)temp);
How about:
String s = "00000000 01100001";
String[] w = s.split(" ");
char c = (char)(Integer.parseInt(w[0], 2) * 256 + Integer.parseInt(w[1], 2));
This allows for the leading zeroes of each byte to be omitted. If you know they're there, you can just replace the space out of the string and use a single parseInt() call:
char c = (char)Integer.parseInt(s.replace(" ", ""), 2);