Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在ucwords中使用类正则表达式制作首字母大写_Java_Php_Regex - Fatal编程技术网

Java 在ucwords中使用类正则表达式制作首字母大写

Java 在ucwords中使用类正则表达式制作首字母大写,java,php,regex,Java,Php,Regex,我需要将字符串值转换为大写(每个单词的第一个字母转换为大写)。 这可以通过使用ucwords()方法在php中完成 例: 感谢使用hi5和regex,这将很困难。尝试以下简单代码: String str="hello world"; String[] words=str.split(" "); for (String word : words) { char upCase=Character.toUpperCase(word.charAt(0));

我需要将字符串值转换为大写(每个单词的第一个字母转换为大写)。 这可以通过使用ucwords()方法在php中完成

例:


感谢使用hi5和
regex
,这将很困难。尝试以下简单代码:

String str="hello world";
String[] words=str.split(" ");
for (String word : words) {
   char upCase=Character.toUpperCase(word.charAt(0));
   System.out.print(new StringBuilder(word.substring(1)).insert(0, upCase));
}
输出:
Hello World

您可以出于同样的目的使用apache的WordUtils

WordUtils.capitalizeFully(Input String);

下面提到的将在您的所有情况下都非常有效

If you need to get first letter of all words capital ..
-----------------------------------------------------



 public String toTheUpperCase(String givenString) {
            String[] arr = givenString.split(" ");
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < arr.length; i++) {
                sb.append(Character.toUpperCase(arr[i].charAt(0)))
                        .append(arr[i].substring(1)).append(" ");
            }
            return sb.toString().trim();
        }



When you need first letter of first word to be capitalized 
-------------------------------------------------------------


public String toTheUpperCaseSingle(String givenString) {
                String example = givenString;

                example = example.substring(0, 1).toUpperCase()
                        + example.substring(1, example.length());

                System.out.println(example);
                return example;
            }
如果你需要更多的细节,请告诉我

享受


干杯

这里是toUpperCase方法的简化版本

将句子中的所有首字母改为大写

public static String ucwords(String sentence) {
    StringBuffer sb = new StringBuffer();

    for (CharSequence word: sentence.split(" "))
        sb.append(Character.toUpperCase(word.charAt(0))).append(word.subSequence(1, word.length())).append(" ");

    return sb.toString().trim();
}
仅将第一个单词改为大写。(漂亮的一行)

String stringToSearch=“每个单词的第一个字母必须大写”

System.out.println(ucWord(“代码比单词好!!”);//主要方法
专用静态字符串ucWord(字符串字){
word=word.toLowerCase();
char[]c=word.toCharArray();
c[0]=字符.toUpperCase(c[0]);
int len=c.长度;
对于(int i=1;i
您想用JAVA或Javascript实现这一点吗?这是一个很大的区别。我想要Java版本。不是在JS中。谢谢对不起,先生。我不明白你的意思。我只想用java和regex将上面的myString转换为每个单词的第一个字符大写。我应该在replaceAll()方法中使用什么?还是我应该用另一种方式?但是我想通过使用正则表达式来实现这一点。
regex
只是一个“正则表达式”。它用于查找/匹配特定的字符、字符串等。它本身不进行替换,因此需要“匹配并替换”。在本例中,“匹配每个单词的第一个字符”和“替换为大写版本”。正则表达式本身不“做”替换。因此,你的问题措词不当。这有点像“我想要一把螺丝刀来钉这颗钉子”,而你真正想要的是“把这颗钉子钉进木头里”,解决办法是“锤子”。谢谢你的剪切。这太棒了。。可以在任何地方使用。明白了,谢谢。创建一个全局实用程序类应该是以上方法的答案。明白了。在某些情况下,使用第三方API是可以的。但是我更喜欢用纯Java来做。谢谢你,伙计!这很有用。你可以写一个实用方法,可以手动格式化字符串。明白了。静态实用程序类可以用作应用程序的全局字符串hanler。以上api也是很有价值的。。是 啊我试过了,但让我恶心。为此使用正则表达式有点困难。字符串处理就是答案。谢谢你,伙计!
your_text_view.setText(cuc.toTheUpperCase(user_name)); // for all words 

your_text_view.setText(cuc.toTheUpperCaseSingle(user_name)); // for only first word ...
public static String ucwords(String sentence) {
    StringBuffer sb = new StringBuffer();

    for (CharSequence word: sentence.split(" "))
        sb.append(Character.toUpperCase(word.charAt(0))).append(word.subSequence(1, word.length())).append(" ");

    return sb.toString().trim();
}
public static String ucFirstWord(String sentence) {
    return String.valueOf(Character.toUpperCase(word.charAt(0))).concat(word.substring(1));
}
    // First letter upper case using regex
Pattern firstLetterPtn = Pattern.compile("(\\b[a-z]{1})+");
Matcher m = firstLetterPtn.matcher(stringToSearch);
StringBuffer sb = new StringBuffer();
while(m.find()){
    m.appendReplacement(sb,m.group().toUpperCase()); 
}
m.appendTail(sb);
stringToSearch = sb.toString();
sb.setLength(0);

System.out.println(stringToSearch);

output:

This String Is Needed To Be First Letter Uppercased For Each Word
System.out.println(ucWord("the codes are better than words !!"));// in main method

private static String ucWord(String word) {
    word = word.toLowerCase();
    char[] c = word.toCharArray();
    c[0] = Character.toUpperCase(c[0]);
    int len = c.length;
    for (int i = 1; i < len; i++) {
        if (c[i - 1] == ' ') {
            c[i] = Character.toUpperCase(c[i]);
        }
    }
    word = String.valueOf(c);
    return word;
}