如何在Java中大写每个字符串?

如何在Java中大写每个字符串?,java,Java,我正在研究这个java方法,试图将字符串中的第n个单词大写,但由于无法为retVal变量返回值而陷入困境 class MyTesting { public static void main (String[] args) throws java.lang.Exception { capitalizeEveryNthWord("this is a String", 3, 3); } // Take a single-spaced <sentence>, and capital

我正在研究这个java方法,试图将字符串中的第n个单词大写,但由于无法为retVal变量返回值而陷入困境

class MyTesting
{
public static void main (String[] args) throws java.lang.Exception
{
    capitalizeEveryNthWord("this is a String", 3, 3);
}

// Take a single-spaced <sentence>, and capitalize every <n> word starting   with <offset>.

public static String capitalizeEveryNthWord(String sentence, Integer offset,     Integer n) {
String[] parts = sentence.split(" ");
String retVal = "";
for (int idx = 0; idx < offset; idx++)
{
    retVal.concat(parts[idx] + " ");
}
for (int idx = offset; idx < parts.length; idx++)
{
    if (idx - offset % n == 0)
    {
        retVal.concat(parts[idx] + "-");
    }
    else
    {
        retVal.concat(parts[idx] + " ");
    }
}
System.out.println(retVal);
return retVal;
}
}
classmytesting
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
大写Everynthword(“这是一个字符串”,3,3);
}
//取一个空格,每个单词以开头大写。
公共静态字符串大写Everynthword(字符串语句,整数偏移量,整数n){
字符串[]部分=句子。拆分(“”);
字符串retVal=“”;
对于(int idx=0;idx
concat()
返回一个值,它不会修改调用该方法的字符串。您需要将其用作
retVal=retVal.concat(…)
或简单地使用
retVal+=…
Java的
String
类是不可变的。将以新的
字符串
对象的形式返回连接

您可以使用
retVal=retVal.concat(…)
,也可以使用
StringBuilder

以下工作:

class MyTesting
{
  public static void main (String[] args) throws java.lang.Exception
  {
    capitalizeEveryNthWord("this is a sentence that is being tested", 3, 3);
  }

  // Take a single-spaced <sentence>, and capitalize every <n> word starting   with <offset>.
  public static String capitalizeEveryNthWord(String sentence, Integer offset, Integer n) {
    String[] parts = sentence.split(" ");
    String retVal = "";
    for (int idx = 0; idx < offset; idx++)
    {
        retVal += parts[idx] + " ";
    }
    for (int idx = offset; idx < parts.length; idx++)
    {
        if ((idx - offset) % n == 0) // added parantheses
        {
            retVal += Character.toUpperCase(parts[idx].charAt(0)) + parts[idx].substring(1) + " "; // make the first character uppercase.
        }
        else
        {
            retVal += parts[idx] + " ";
        }
    }
    System.out.println(retVal);
    return retVal;
  }
}
classmytesting
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
大写Everynthword(“这是一个正在测试的句子”,3,3);
}
//取一个空格,每个单词以开头大写。
公共静态字符串大写Everynthword(字符串语句,整数偏移量,整数n){
字符串[]部分=句子。拆分(“”);
字符串retVal=“”;
对于(int idx=0;idx
更有效的方法是:

public static String capitalizeEveryNthWord(String sentence, Integer offset, Integer n) {
  StringBuilder sb = new StringBuilder(sentence);
  int wordIdx = 0;
  boolean newWord = true;
  for (int i = 0; i < sb.length(); i++) {
    char c = sb.charAt(i);
    if (c == ' ') {
      wordIdx++; // assumes single space between words.
      newWord = true;
    } else if (newWord) {
      if (wordIdx >= offset && (wordIdx - offset) % n == 0) {
        sb.setCharAt(i, Character.toUpperCase(c));
      }
      newWord = false;
    }
  }
  return sb.toString();
}
公共静态字符串大写Everynthword(字符串语句,整数偏移量,整数n){
StringBuilder sb=新的StringBuilder(句子);
int-wordIdx=0;
布尔新词=真;
for(int i=0;i=offset&&(wordIdx-offset)%n==0){
sb.setCharAt(i,Character.toUpperCase(c));
}
newWord=false;
}
}
使某人返回字符串();
}
第二种方法只分配一个缓冲区,然后对其进行适当的修改以使单词大写。前面的方法在每次调用
+=
时都会分配新的
字符串
对象(编译器偶尔会对此进行优化,但据我所知,这并不能保证)。

使用
toUpperCase()
方法并使用返回值

String retVal = retVal.concat(...).toUpperCase();

是我,还是一开始就没有大写?@progx你是指
String.toUpperCase()
还是想转换成TitleCase?但它们必须大写,条件是..看看:capitalizeEveryNthWord(“这是一个正在测试的句子”,3,3);