当我输入时:
char[] chars=Texto.toCharArray();
String D= new String(chars);
当我输入“hello”时,字符会得到第一个字母“h”,但字符串会得到“hello”,我只需要“h”将其置于if的条件下,我可以输入什么字符串只得到一个字母。
最佳答案
你为什么不这样做(就像评论中建议的那样):
String d = Textto.substring(0, 1);
// d = "h"
我在这里遗漏了什么吗?
编辑
根据您的评论,您想检查字符串是否包含某个字符,这是一种方法:
if(d.contains("o")) {
d = d + "fo";
}
编辑2
嗯,这与您原来的问题相去甚远,但是通过您的评论,您希望用以下模式替换每个出现的元音 -> [vowel]f[vowel]
。
这是一个(未经测试的)应用程序:
public static void main (String[] args) throws java.lang.Exception {
String d = "hello";
for(char c : d.toCharArray()) {
if(isVowel(c)) {
d = replaceVowel(d, c);
}
}
System.out.println(d);
}
private static boolean isVowel(char c) {
return "AEIOUaeiou".indexOf(c) != -1;
}
private static String replaceVowel(String original, char c) {
String vowel = Character.toString(c);
return original.replace(vowel, String.format("%1$sf%1$s",vowel));
}
看看这里(已编辑): http://ideone.com/uWpjbb
关于java - 是否有任何形式可以将字符数组放在字符串上并再次更改它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19898855/