我想检查字符串的空白,并希望将空格后面的第一个字符打印到大写字母

时间:2014-04-04 19:21:40

标签: java

我想检查一个字符串的空白,并希望将空格后面的第一个字符打印成大写字母。

E:克 - >输入 - I am in trouble

输出应该是这样的 - > I Am In Trouble

  1. 我能够在字符串中捕捉到空格(无论它们有多少)但是无法将空格后的第一个字符更改为大写字母。

  2. 以及如何在原始 str 的空格(现在是大写字母)之后容纳更改后的第一个字符。因此,当我打印时,我会找到上面提到的输出。

  3. 我已经分享了我尝试过的任何东西,请看看并引导我完成。

    public class CatchSpace {
    
        public static void main(String[] args) {
            String str="I am in trouble";
    
            char[] arr=str.toCharArray();
    
            for(int i=0;i<arr.length;i++){
    
                if(arr[i]==' '){
    
                    //str.charAt(i+1);
                    //Character.toUpperCase(i+1);
                    char c=arr[i+1];    
                }
            }
        }
    }
    

4 个答案:

答案 0 :(得分:1)

你几乎就在那里,但你需要将新的大写字符存储回你的数组:

    String str = "I am in trouble";
    char[] arr = str.toCharArray();
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == ' ') {
            //str.charAt(i+1);
            //Character.toUpperCase(i+1);
            char c = arr[i + 1];
            arr[i+1] = Character.toUpperCase(c); // like this;
        }
    }

答案 1 :(得分:1)

您可以使用Apache Commons Lang代替直接Java API。

简单地用空格分割字符串,然后对每个标记使用StringUtils.capitalize方法,然后连接(即使用Guava集合)。

我附上以下示例(使用Guava和Commons Lang的极端版本​​):

import java.util.List;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import static com.google.common.collect.Lists.*;
import static org.apache.commons.lang.StringUtils.*;

public class TestCapitalize {

    private Joiner joiner = Joiner.on(" "); 

    private Function<String, String> fCapitalize = new Function<String, String>() {
        public String apply(String s) {
            return capitalize(s);
        }
    };

    public String capitalizeAll() {        
        String str = "I am in trouble";
        List<String> tokens = newArrayList(split(str, " "));
        List<String> capitalized = transform(tokens, fCapitalize);
        return joiner.join(capitalized);
    }
}

希望有所帮助。

答案 2 :(得分:1)

String str = "testy testy testy";
        char arr[] = str.toCharArray();
        for (int i = 0; i < str.length()-1; i++){
            //we go until length-1 so we don't get an out of bounds exception if the last character of the string is a whitespace
            if(Character.isWhitespace(arr[i])){
                arr[i+1] = Character.toUpperCase(arr[i+1]);
            }
        }
        str = arr.toString();

这样做。如果数组的第一个字母尚未大写,那么唯一不做的就是大写数组的第一个字母。如果你想要,只需要一个简单的修改。

String str = "testy testy testy";
        char arr[] = str.toCharArray();
        arr[0] = Character.toUpperCase(arr[0]);
        for (int i = 0; i < str.length()-1; i++){
            //we go until length-1 so we don't get an out of bounds exception if the last character of the string is a whitespace
            if(Character.isWhitespace(arr[i])){
                arr[i+1] = Character.toUpperCase(arr[i+1]);
            }
        }
        str = arr.toString();

答案 3 :(得分:1)

您可以尝试使用正则表达式:

String str = "testy testy testy";
char arr[] = str.toCharArray();
// set first char to UpperCase
arr[0] = Character.toUpperCase(arr[0]);
// pattern is set to match any space char followed by lowercase char
Pattern p = Pattern.compile(" [a-z]");
Matcher m = p.matcher(str);
StringBuffer sb = new StringBuffer();
while (m.find()) {
     arr[i+1] =  Character.toUpperCase(arr[m.start()+1]);
}
// Turn the array back to a string
str = arr.toString();

它类似于Lighthat的答案,但是这样,它只会对小写字符应用toUpperCase,并且你不会冒任何IndexOutOfBoundException