I want to know that how to recognize and print next character in ASCII sequence if input is a non- string value like "space" or "!".
I know that for string value we can convert it into ASCII value by using
char character = 'a';
int ascii = (int) character;
Then adding 1 to it and converting it back to char , we can get next value in the sequence .
You can use:
char character = 'a';
int ascii = (char)((int)character+1);
It should work. But I have haven`t tested it.
(in Java)
I am trying to take a value out of my char array
char[] abc = {'a' , 'b' , 'c' , 'd' , 'e' , 'f'};
and assign it to a single char
char currentChar = abc.[0];
and then ideally, I would replace 0 with and i for a for loop. Eclipse says
"The type of the expression must be an array type but it resolved to Class"
How would I get variable currentChar to be equal to the character a ?
It has to be
char currentChar = abc[0];
it should be
char currentChar = abc[0];
when you are doing abc., it means its expecting class or object because of dot
Your syntax is just wrong - there doesn't have to a dot.
Right would be:
char currentChar = abc[0];
I am using the toCharArray method to convert a string to an array of type char but every time i try to print the array, it is printing numbers instead of the characters stored in the string. When i print the string the characters are printed just fine.
char[] nArray = capitalizedSentence.toCharArray();
for (int i = 0; i < nArray.length; i++)
{
System.out.println(nArray[i] + '\n');
}
EXAMPLE:
If my capitalizedSentence string has the value "Saad", when i convert it to a character array and print it, it prints the following:
93
75
75
78
can someone please help me so that it prints the individual characters stored in the capitalizedSentence string?
nArray[i] is a char; so is the '\n' constant. Character is an unsigned integral type, so characters are added together in the same way as all integers - numerically. When an addition happens, you end up with an int, not a char, so calling println on it produces a numeric result.
Removing + '\n' will fix the problem. You would get a newline character from println, so all characters would appear on a new line.
Demo.
Replace '\n' with "\n"
Why? Because the compiler sees you adding 2 characters together, to the compiler char and int primitive data types are considered to interchangeable and so the + is actually adding the 2 int values together. Using double quotes will tell the compiler that you are adding a string to a character.
I think this will help you. Use for-each loop
char[] nArray = capitalizedSentence.toCharArray();
for(Character c: nArray ){
System.out.println(c);
}
For example:
String word = "schnucks";
word[1] = 'x'; // would this access the C and turn it to an x?
If the above code is not correct, is there a way, besides converting it from a string to a char array to access the individual indices?
Strings in Java are immutable. You can read a char from a specific index with charAt(int index) but you can not modify it. For that you would need to convert to a char array as you suggested and then build a new string from the array.
You can try replace():
String word = "schnucks";
word = word.replace("c", "x");//<-- "sxhnucks", only first occurrence
Also there is replaceAll():
String word = "schnucks";
word = word.replaceAll("c", "x");//<-- "sxhnuxks", all occurrences
To access the elements of a String by index, first convert to an array of chars.
String word = "schnucks";
char[] array = word.toCharArray();
Then you are free to change any letter as you wish. e.g.
array[4] = 'a';
To retrieve the modified String, simply use
word = new String(array);
which returns a String containing the word schnacks.
well you can use charAt(int index) method to access character at your specified index.
But for changing characters of the string you can use StringBuilder class and use .setCharAt(int index, char character) method.
You can't change characters in a String because Strings are immutable in Java.
As mentioned in the Documentation:
Strings are constant; their values cannot be changed after they are created.
To read a character from a String, use charAt
Returns the char value at the specified index. An index ranges from 0 to length() - 1.
To get a String with only a certain character changed, you can do as follows:
String word = "geography";
int indexToChange = 3;
char newCharacter = 'x';
String newword = word.substring(0, indexToChange - 1) + newCharacter + word.substring(indexToChange, word.length());
System.out.println(newword);
I want to represent an empty character in Java as "" in String...
Like that char ch = an empty character;
Actually I want to replace a character without leaving space.
I think it might be sufficient to understand what this means: no character not even space.
You may assign '\u0000' (or 0).
For this purpose, use Character.MIN_VALUE.
Character ch = Character.MIN_VALUE;
char means exactly one character. You can't assign zero characters to this type.
That means that there is no char value for which String.replace(char, char) would return a string with a diffrent length.
As Character is a class deriving from Object, you can assign null as "instance":
Character myChar = null;
Problem solved ;)
An empty String is a wrapper on a char[] with no elements. You can have an empty char[]. But you cannot have an "empty" char. Like other primitives, a char has to have a value.
You say you want to "replace a character without leaving a space".
If you are dealing with a char[], then you would create a new char[] with that element removed.
If you are dealing with a String, then you would create a new String (String is immutable) with the character removed.
Here are some samples of how you could remove a char:
public static void main(String[] args) throws Exception {
String s = "abcdefg";
int index = s.indexOf('d');
// delete a char from a char[]
char[] array = s.toCharArray();
char[] tmp = new char[array.length-1];
System.arraycopy(array, 0, tmp, 0, index);
System.arraycopy(array, index+1, tmp, index, tmp.length-index);
System.err.println(new String(tmp));
// delete a char from a String using replace
String s1 = s.replace("d", "");
System.err.println(s1);
// delete a char from a String using StringBuilder
StringBuilder sb = new StringBuilder(s);
sb.deleteCharAt(index);
s1 = sb.toString();
System.err.println(s1);
}
As chars can be represented as Integers (ASCII-Codes), you can simply write:
char c = 0;
The 0 in ASCII-Code is null.
If you want to replace a character in a String without leaving any empty space then you can achieve this by using StringBuilder. String is immutable object in java,you can not modify it.
String str = "Hello";
StringBuilder sb = new StringBuilder(str);
sb.deleteCharAt(1); // to replace e character
I was looking for this. Simply set the char c = 0; and it works perfectly. Try it.
For example, if you are trying to remove duplicate characters from a String , one way would be to convert the string to char array and store in a hashset of characters which would automatically prevent duplicates.
Another way, however, will be to convert the string to a char array, use two for-loops and compare each character with the rest of the string/char array (a Big O on N^2 activity), then for each duplicate found just set that char to 0..
...and use new String(char[]) to convert the resulting char array to string and then sysout to print (this is all java btw). you will observe all chars set to zero are simply not there and all duplicates are gone. long post, but just wanted to give you an example.
so yes set char c = 0; or if for char array, set cArray[i]=0 for that specific duplicate character and you will have removed it.
You can't. "" is the literal for a string, which contains no characters. It does not contain the "empty character" (whatever you mean by that).
In java there is nothing as empty character literal, in other words, '' has no meaning unlike "" which means a empty String literal
The closest you can go about representing empty character literal is through zero length char[], something like:
char[] cArr = {}; // cArr is a zero length array
char[] cArr = new char[0] // this does the same
If you refer to String class its default constructor creates a empty character sequence using new char[0]
Also, using Character.MIN_VALUE is not correct because it is not really empty character rather smallest value of type character.
I also don't like Character c = null; as a solution mainly because jvm will throw NPE if it tries to un-box it. Secondly, null is basically a reference to nothing w.r.t reference type and here we are dealing with primitive type which don't accept null as a possible value.
Assuming that in the string, say str, OP wants to replace all occurrences of a character, say 'x', with empty character '', then try using:
str.replace("x", "");
char ch = Character.MIN_VALUE;
The code above will initialize the variable ch with the minimum value that a char can have (i.e. \u0000).
this is how I do it.
char[] myEmptyCharArray = "".toCharArray();
You can do something like this:
mystring.replace(""+ch, "");
String before = EMPTY_SPACE+TAB+"word"+TAB+EMPTY_SPACE
Where
EMPTY_SPACE = " " (this is String)
TAB = '\t' (this is Character)
String after = before.replaceAll(" ", "").replace('\t', '\0')
means
after = "word"
You can only re-use an existing character. e.g. \0 If you put this in a String, you will have a String with one character in it.
Say you want a char such that when you do
String s =
char ch = ?
String s2 = s + ch; // there is not char which does this.
assert s.equals(s2);
what you have to do instead is
String s =
char ch = MY_NULL_CHAR;
String s2 = ch == MY_NULL_CHAR ? s : s + ch;
assert s.equals(s2);
Use the \b operator (the backspace escape operator) in the second parameter
String test= "Anna Banana";
System.out.println(test); //returns Anna Banana<br><br>
System.out.println(test.replaceAll(" ","\b")); //returns AnnaBanana removing all the spaces in the string