String sample = "Java";
How do I change "Java" into, for example, "Jeva"?
change the 2nd char to 'e'?
changing char's of a String
Started by zee_ola05, Jul 12 2009 01:10 AM
8 replies to this topic
#1
Posted 12 July 2009 - 01:10 AM
|
|
|
#2
Posted 12 July 2009 - 02:21 AM
i would convert the string into a char array
then it's easy to change 1 char from the char array
note: i don't have java on this computer. Did this without testing, might contain an error or 2
String text = "Java"; char[] letters = text.toCharArray();
then it's easy to change 1 char from the char array
letters[1]='e';now just put the char array into the string
String temp="";
for(int i=0; i<letters.length(); i++){
temp+=letters[i];
}
text = temp;
note: i don't have java on this computer. Did this without testing, might contain an error or 2
Edited by wim DC, 12 July 2009 - 02:22 AM.
forgot crucial code ;)
#3
Posted 12 July 2009 - 06:42 AM
Thanks oxano! it worked. I will send you a private message when I got other questions with JAVA. I hope that's ok. :D
#4
Posted 12 July 2009 - 07:09 AM
no problem :)
#5
Posted 12 July 2009 - 07:15 AM
Another question.
How about the reverse, array of char to String?
How about the reverse, array of char to String?
#6
Posted 12 July 2009 - 08:02 AM
isn't that the 3th part of my 1st post?
#7
Posted 12 July 2009 - 08:23 AM
ow. sorry. I didn't look at that before. Thanks!
#8
Posted 12 July 2009 - 08:28 AM
this might be another interesting way:
never did it that way, don't know if it works.
/**
* Main.java
*
* @author www.javadb.com
*/
public class Main {
/**
* Converts an array of characters to a String
*/
public void convertCharArrayToString() {
char[] charArray = new char[] {'a', 'b', 'c'};
String str = new String(charArray);
System.out.println(str);
}
/**
* Starts the program
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertCharArrayToString();
}
}
from Character array to String conversion - A Java Code Examplenever did it that way, don't know if it works.
#9
Posted 12 July 2009 - 09:34 AM
That is better!


Sign In
Create Account


Back to top









