gpt4 book ai didi

Java本身改变字符的大小

转载 作者:行者123 更新时间:2023-12-01 16:44:48 25 4
gpt4 key购买 nike

我是java初学者做任务的时候我遇到了问题代码本身将单词 Riverrrrr rsmall 更改为 Rlarge

import java.lang.StringBuilder;
import java.util.Arrays;

class Solution {
static String toCamelCase(String s) {
if (s.contains("_")) {
s = s.replace("_", "-");
}
String[] list = s.split("-");
String nowa = "";
String result = "";
String result1 = "";
System.out.println(Arrays.toString(list));
for (int i = 0; i < list.length; i++) {
if (Character.isUpperCase(list[0].charAt(0))) {
nowa = list[i].replace(list[i].charAt(0), list[i].toUpperCase().charAt(0));
result += nowa;
System.out.println(nowa);
result1 = result;
}
if (Character.isLowerCase(list[0].charAt(0))) {
nowa = list[i].replace(list[i].charAt(0), list[i].toUpperCase().charAt(0));
result += nowa;
result1 = result.replace(result.charAt(0), result.toLowerCase().charAt(0));
}
}
return result1;
}
}

为什么要在 RiverRRRRR 上更改 Riverrrrr ?我只想要 Riverrrrr,为什么 rrrrr 是大写的?

enter image description here

最佳答案

public String Replace(char oldChar,char newChar) 它将替换所有匹配的字符

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

使用replaceFirst()

public String replaceFirst(String regex, String replacement)

Replaces the first substring of this string that matches the given regular expression with the given replacement.

在您的代码中将其更改为

nowa = list[i].replace(list[i].charAt(0), list[i].toUpperCase().charAt(0));

这个

nowa = list[i].replaceFirst(String.valueOf(list[i].charAt(0)), String.valueOf(list[i].toUpperCase().charAt(0)));

输入: “lake_riverrrr”

输出: lakeRiverrrrr

关于Java本身改变字符的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53998064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com