-5
public static String char2Str(char c) {
    return Integer.toString(c);
}

EDIT: For example, char2Str('ɮ') returns 622.

How do I implement the opposite method of the above (str2Char())?

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
  • You want to reduce a string to a single char? That's not really how it works. – Ben May 23 '18 at 12:17
  • 4
    What are you expecting to pass into `char2Str` ? because you are taking in a `char` and treating it like an integer. – khelwood May 23 '18 at 12:18
  • 5
    You should probably use `Character.toString(c)` rather than `Integer` – Michael May 23 '18 at 12:18
  • 1
    A `String` contains more information than a `char`. A `char` consists of a **single** character. A `String` may contain **multiple** character. Do you want to extract only the first character? Or rather get a whole array of characters, a `char[]`? Note that your question was asked already, simply search for "*java convert string to char*", first hit on Google. Note that a `char` is not the same as an `int`. Instead of converting using utility methods for `int` from `Integer` you should use utilities for `char` from `Character`. – Zabuzard May 23 '18 at 12:26
  • 1
    At least provide some examples to demonstrate input and desired output. Otherwise your question may be **unclear**. Which it is at the moment, it is not clear if you want a `char[]` or only extract the first `char`. – Zabuzard May 23 '18 at 12:29

4 Answers4

2

You should have something like that:

Char to String

public static String char2Str(char c) {
    return Character.toString(c);
}

String to Char

When converting String to char, your output will be an array of characters as bellow:

public static char[] str2Char(String s) {
    return s.toCharArray();
}
Tiago Mussi
  • 801
  • 3
  • 13
  • 20
0

If you know String is of single length (just a single char), Use

char c = s.charAt(0);

Your method

public static char str2Char(String s) {
    return s.charAt(0);
}
Hemant Patel
  • 3,160
  • 1
  • 20
  • 29
  • 2
    His above sample appears to be putting the integer value in a string, this may be a case for `Integer.parseInt()` – cyroxis May 23 '18 at 12:20
0

you have to be sure that your string contains only 1 char, then use

s.charAt(0)

if not, you can get the array of chars and then return the one you need or the whole array

char[] arr = s.toCharArray();
secario
  • 456
  • 4
  • 14
0

Well you can find the answer right here : https://stackoverflow.com/a/7853536/9623806

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

  • Instead of linking like that you can just flag as duplicate... that needs 15 reputation. Before that consider answering other questions instead. (however, this answer is sufficiently self-contained, no problem) – user202729 May 23 '18 at 13:33
  • Another suggestion: You can [format code blocks](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks). – user202729 May 23 '18 at 13:34
  • thanks for the information – François Bourrée May 23 '18 at 13:56