[英]How to capitalize every string in Java?
I'm working on this java method trying to capitalize the nth word in a string and got stuck on not being able to return a value for retVal variable 我正在尝试将此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;
}
}
concat()
returns a value, it doesn't modify the string on which you're calling the method. concat()
返回一个值,它不会修改您在其上调用方法的字符串。 You need to use it as retVal = retVal.concat(...)
or simply retVal += ...
您需要将其用作
retVal = retVal.concat(...)
或只是retVal += ...
Java's String
class is immutable. Java的
String
类是不可变的。 String.concat()
will return the concatenation as a new String
object. String.concat()
将作为新的String
对象返回串联。
You can either use retVal = retVal.concat(...)
, or use a StringBuilder
. 您可以使用
retVal = retVal.concat(...)
,也可以使用StringBuilder
。
The following works: 以下作品:
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;
}
}
A more efficient approach would be something like: 一种更有效的方法是:
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();
}
This second approach only allocates one buffer which is then modified in-place to capitalize words. 第二种方法仅分配一个缓冲区,然后就地对其进行修改以大写单词。 The previous approach allocates new
String
objects with every call to +=
(this can occasionally be optimized away by compilers but it's not guaranteed, as far as I know). 以前的方法在每次对
+=
调用中都会分配新的String
对象(据我所知,这有时可以由编译器进行优化,但是不能保证)。
使用toUpperCase()
方法并使用返回值 :
String retVal = retVal.concat(...).toUpperCase();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.