簡體   English   中英

將字符串中每組字母的首字母大寫

[英]Capitalize first letter of each group of letters in a string

我有以下字符串s

String s = "abbcccdddd";

我需要將每組字母的首字母大寫,例如:

ABbCccDddd

我怎樣才能做到這一點?

public static String capFirst(String s){
    
    //String we want to return
    String result = "";
    
    //The first letter will always be capitalized; concatenate it to our result
    result += s.substring(0, 1).toUpperCase();
    
    //Iterate across original string s
    for (int i = 1; i < s.length(); i++){
        
        //If the current letter is equal to the previous letter
        if (s.substring(i,i+1).equalsIgnoreCase(s.substring(i-1,i))){
            
            //Concatenate to our return string as is (lower case)
            result += s.substring(i,i+1);
            
        } else {
            
            //Otherwise, concatenate to our return string as upper case
            result += s.substring(i,i+1).toUpperCase();
            
        }
    }
    
    //Return final result
    return result;
}

例子

public static void main(String[] args) {
    String s = "abbcccdddd";
    System.out.println(capFirst(s));
}

Output

ABbCccDddd

使用 Java 8 分組方式:

您可以使用 Java8 groupingBy功能實現相同的功能,您可以在其中對相同的元素進行分組並找到每個元素的頻率並將其添加到列表中,如下所示:

代碼:

public class Convert {
    public static void main(String[] args) {
        String s = "abbcccdddd";
        
        Map<Character,Long> countMap = s.chars().mapToObj(x -> (char)x)
            .collect(Collectors.groupingBy(Function.identity(), 
                    LinkedHashMap::new,Collectors.counting()));

        StringBuilder sb = new StringBuilder();
        
        countMap.forEach((k,v) -> {
            for(int i = 0; i < v; i++){
                if(i==0){
                    sb.append(k.toString().toUpperCase());
                } else{
                    sb.append(k);
                }
            }
        });
        System.out.println(sb);
    }
}

Output:

ABbCccDddd

只是另一種方式。 閱讀代碼中的注釋:

String s = "abbcccdddd";
    
String curLetter = "";   // Holds the current letter being processed in string.
String prevLetter = "";  // Holds the current letter AFTER its been processed.
StringBuilder sb = new StringBuilder("");  // Used to build the string.
    
// Iterate through each character of the string `s`...
for (int i = 0; i < s.length(); i++) {

    // Get the current letter to process.
    curLetter = String.valueOf(s.charAt(i));

    // Is it the same s the previous letter procesed?
    if (!curLetter.equalsIgnoreCase(prevLetter)) {
        // No, then make the letter Upper Case
        curLetter = curLetter.toUpperCase();
    }
    // Add the current letter to the String Builder
    sb.append(curLetter);
        
    /* Make the prevLetter hold the curLetter for 
       next iteration comparison.      */
    prevLetter = curLetter; 
}
    
// Apply what is in the StringBuilder into a newString variable.
String newString = sb.toString();
    
// Display newString in Console Window.
System.out.println(newString);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM