This question already has answers here:
How to convert a String into an array of Strings containing one character each
(6 answers)
Closed 8 years ago.
I'm new to java.How to convert char array into string array.My char array holds the values from Resultset rs.The code for char array is
char[] time= rs.getString(logtime).toCharArray();
How to convert this time into String array.
The question is a bit unclear. Assuming that you want a string array with the same contents as the character array (one character per string), here's a very quick and dirty solution:
char[] time = rs.getString(logtime).toCharArray();
String[] s = (new String(time)).split("(?<=.)");
Or, if you don't really need to convert to a char array first, you could just simply do:
String[] s = rs.getString(logtime).split("(?<=.)");
The regular expression used for the split method is a zero-width positive lookbehind assertion to match any character.
A cleaner (and most likely more efficient) solution would look as follows:
char[] time = rs.getString(logtime).toCharArray();
String[] s = new String[time.length];
for (int i = 0; i < time.length; i++) {
s[i] = String.valueOf(time[i]);
}
Try this
char[] time= rs.getString(logtime).toCharArray();
String[] timeStr = new String[time.length];
for (int i=0,len=time.length; i<len; i++){
timeStr[i] = String.valueOf(time[i]);
}
Related
This question already has answers here:
Splitting String and put it on int array
(8 answers)
Closed 4 years ago.
If
String a="1,2,3,4,5"
I want to remove the commas in the string, and
change the string to an array, then
change every character of the array to integers, finally
apply any operation to the integers.
Is that possible?
I have this so far:
String input;
input = txtField.getText();
String[] a = new String[input.length()];
a = input.split(",");
for(int i=0;i==input.length();i++){
String c = a[i];
txtField.setText(String.valueOf(a));
}
it is, you could use a method which basically removes all commas(.split)so you get a String without commas, and later take that string and use a for loop to go through each char and parse to int.
This question already has answers here:
Replace a character at a specific index in a string?
(9 answers)
Closed 5 years ago.
The following code prints eleelde but I want it to print elcaalde.
How can I do it? Is there a function like replace char at index()??
I want to assign the character at i=0 the value of the char at i=6 and print the word elcaalde.
public class ReverseExperiments3 {
public static void main (String[] args) {
String s= "alcaalde";
s=s.replace('a','e');
System.out.println(s);
}
}
Change it in the char array:
char[] cs = s.toCharArray();
cs[0] = cs[6]; // For example.
s = new String(cs);
try this code: If you wanna get what you want, it is 7th element
String s= "alcaalde";
char[] cs = s.toCharArray();
cs[0] = cs[7];
s = new String(cs);
You could also use a regex replacement here:
String s = "alcaalde";
s = s.replaceFirst("(.)(.{6})(.)", "$3$2$1");
Demo
The answer given by #andy is probably the best one for this exact problem. But if the OP needed to move around strings, then a regex based approach would put us in a fairly good position.
This question already has answers here:
Java: parse int value from a char
(9 answers)
Closed 5 years ago.
This is just a part of my code that I have problem troubleshooting.
static int checkConcat(String number) {
TreeSet<Integer> a = new TreeSet<Integer>();
for(int i=0; i<number.length(); i++) {
a.add(Integer.parseInt(number.charAt(i)));
}
}
The error that I'm getting is :
error: incompatible types: char cannot be converted to String
What's wrong with the code, and how can I solve it in this context?
You can easily convert the char to String.
char aCharacter = 'c';
String aCharacterAsString = String.valueOf(aCharacter);
So in your case:
a.add(Integer.parseInt(String.valueOf(number.charAt(i))));
Integer.parseInt(String s) has the argument type String, not char as returned by number.charAt(i).
Convert to a String:
a.add(Integer.parseInt("" + number.charAt(i)));
A single character is char, not String. Although you could construct a single-character string from char using a method described below, parsing a number digit-by-digit should be done with a different API:
for(int i=0; i<number.length(); i++) {
a.add(Character.digit(number.charAt(i), 10));
}
To construct a single-character string from char in Java, use String.valueOf method:
String singleDigit = String.valueOf(number.charAt(i));
charAt returns one character of the string, therefore the return type is char not string to convert to string use the String.valueOf method:
String.valueOf(number.charAt(i))
Or don't use Integer.parse just subtract '0' from the character to convert it to the digit value. This would be a simpler more efficient way to convert a single digit value.
number.charAt(i) -'0'
This question already has answers here:
string to string array conversion in java
(15 answers)
Closed 7 years ago.
I was wondering how exactly one would convert a string of non-alphanumeric symbols into a string array. For example, what would be the code that takes the string
"#%$!#& (&$#^"
as an input, and converts it into a string array
{"#", "%", etc}
Create a String array of the length of string..
int size = theString.length();
String[] asStringArray = new String[size];
then step through that array/String adding chars from the String with charAt()
for (int i=0; i<size; i++){
asStringArray[i] = ""+theString.charAt(i);
}
Other option is substring()
asStringArray[i] = theString.substring(i,i+1);
This question already has answers here:
Split string into array of character strings
(12 answers)
Closed 7 years ago.
I'm just wondering if anyone could answer a question for me?
Would it be possible to take a string variable of 8 letters and then put each letter into a char array?
For example:
String str;
str = "ABCDEFGH";
Would you be able to take this string variable and split it into an array of 8 characters?
Update:
char[] charArray = str.toCharArray();
will do it
Exactly same question, answered here:
Split string into array of character strings
How about:
String str = "ABCDEFGH";
char[] charArray = str.toCharArray();
Read the documentation for the String class.
Yes you can, you can use toCharArray() method.
String str = "ABCDEFGH";
char[] array=str.toCharArray();
Please make sure that you read javadoc.
You already have a method in String class to do so.
String str = "ABCDEFGH";
char[] array=str.toCharArray();
char[] arr=new char[8];
for(int i=0;i<str.length();i++)
{
arr[i]=str.charAt(i);
}