java - 我在 for 循环中大写了太多元素,但仅在特定索引上大写

标签 java arrays string for-loop uppercase

我不知道为什么,但在这个方法中,第 5 个和第 10 个元素每次都会被大写。 我找不到原因。 例如:传递“ZpglnRxqenU”作为参数应返回:“Z-Pp-Ggg-Llll-Nnnnn-Rrrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnnn-Uuuuuuuuuuu”,但返回:“Z-Pp-Ggg-Llll-Nnnnn -RRRRRR-Xxxxxxx-Qqqqqqq-Eeeeeeeee-Nnnnnnnnnn-UUUUUUUUUUU”

请注意,与其他字符不同,R 和 U 均为大写。

/**
 * The purpose of this method is to receive a string, deconstruct it by characters
 * and save it in array, then iterate through this array creating a new string
 * where each character will be represented the number of times equivalent to its
 * position in the array + 1 while upperCasing every 1st occurrence of the character
 * and separating every set of character repetitions with "-".
 * @param string
 * @return String
 */
public String builder (String string){

    /*
    Here we split the string we receive by characters and save these characters in
    a new array called strArr.
     */
    String[] strArr = string.split("");
    String finalString = "";

    /*
    We iterate through the array of characters adding "-" each time we start to
    represent a new character.
    We add the characters N times where N is the order of appearance in the string
    we receive as param.
     */
    for (int i = 0; i < strArr.length; i++) {
        if(i > 0){
            finalString += "-";
        }
        for (int j = 0; j < i + 1; j++) {
            if(j == 0){
                /*
                this is upperCasing every 5th and 10th character for some reason
                 */
                // TODO: 26/12/2019 stop upperCasing 5th and 10th element. 
                finalString += strArr[i].toUpperCase();
                continue;
            }
            finalString += strArr[i];
        }
    }
    return finalString;

最佳答案

你必须首先添加这一行: 字符串 = string.toLowerCase();

公共(public)字符串构建器(字符串字符串){

 string = string.toLowerCase();
/*
Here we split the string we receive by characters and save these characters in
a new array called strArr.
 */
String[] strArr = string.split("");
String finalString = "";

/*
We iterate through the array of characters adding "-" each time we start to
represent a new character.
We add the characters N times where N is the order of appearance in the string
we receive as param.
 */
for (int i = 0; i < strArr.length; i++) {
    if(i > 0){
        finalString += "-";
    }
    for (int j = 0; j < i + 1; j++) {
        if(j == 0){
            /*
            this is upperCasing every 5th and 10th character for some reason
             */
            // TODO: 26/12/2019 stop upperCasing 5th and 10th element. 
            finalString += strArr[i].toUpperCase();
            continue;
        }
        finalString += strArr[i];
    }
}
return finalString;

}

关于java - 我在 for 循环中大写了太多元素,但仅在特定索引上大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59493830/

相关文章:

java - LinkedHashSet 迭代器无缘无故地进入无限循环

arrays - 如何快速将数据附加到 JSON 格式数组中?

javascript - 对对象数组进行分组的最有效方法

python - 将文件转换为静态 C 字符串声明

mysql - 在 MySQL 中去除列的最后两个字符

c - 标记多个字符串 C

java - 如何将库导入到 Intellij 中?

java - 访问修饰符在封装中有什么作用吗

java - 连续数据流的排序算法

C 语言 - 根据方阵边的平方数读取方阵的通用算法?