使用ucwords中的正则表达式创建首字母大写字母

时间:2013-11-07 04:38:07

标签: java php regex

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

前:

String myString = “HI GUYS”;
myString = myString. toLowerCase().replaceAll(“Regex”, “Some Charactor”)

感谢hi5

6 个答案:

答案 0 :(得分:3)

使用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

答案 1 :(得分:2)

在所有情况下,下面提到的都会很好用

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;
            }

如何使用::尝试在超类中定义此代码(最佳代码实践)

现在当你需要使用这个方法时......只需传递你需要变换的String。 对于Ex ::让我们假设我们的超类为CommanUtilityClass.java ......

现在您需要在某些活动中使用此方法,例如“MainActivity.java”

现在创建超类的对象:: [CommanUtilityClass cuc; ]

最终任务 - 使用如下所述的方法:

your_text_view.setText(cuc.toTheUpperCase(user_name)); // for all words 

your_text_view.setText(cuc.toTheUpperCaseSingle(user_name)); // for only first word ...

如果您需要更多详细信息,请告诉我..

享受

干杯!

答案 2 :(得分:1)

import { combineReducers } from 'redux'

import {
    phonereducer
} from '../scenes'

export default combineReducers({
    phonepage: phonereducer
})

答案 3 :(得分:0)

您可以将apache中的WordUtils用于同样的目的,

WordUtils.capitalizeFully(Input String);

答案 4 :(得分:0)

以下是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();
}

仅将第一个单词更改为大写。 (漂亮的单行)

public static String ucFirstWord(String sentence) {
    return String.valueOf(Character.toUpperCase(word.charAt(0))).concat(word.substring(1));
}

答案 5 :(得分:0)

String stringToSearch =“此字符串需要为每个单词的首字母大写”;

    // 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