大写或小写单词“Java”中的特定字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34538000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:45:33  来源:igfitidea点击:

Uppercase or Lowercase a specific character in a word "Java"

javastringuppercaselowercase

提问by Shah91n

I have look into most of questions but I couldn't find how to uppercase or lowercase specific character inside a word.

我研究了大部分问题,但找不到如何在单词中大写或小写特定字符。

Example:

例子:

String name = "Robert"

What if I would like to make "b" Uppercase and rest lowercase also how to make first letter Uppercase and rest lowercase?

如果我想制作“b”大写和其余小写怎么办?如何制作第一个字母大写和其余小写?

Like "john" >> Output >> "John"...

像“约翰”>>输出>>“约翰”...

I have toUppercase()and toLowercase(). They convert the whole text.

我有toUppercase()toLowercase()。他们转换整个文本。

Also I tried to include charAtbut never worked with me.

我也试图包括charAt但从未与我合作过。

回答by bhooks

You will need to take your string, take a substring of the specific character or characters you want to capitalize or lowercase, and then build a new string off of it.

您需要获取字符串,获取要大写或小写的特定字符的子字符串,然后从中构建一个新字符串。

Example

例子

String test = "JoHn"; //make the H lowercase
test = test.substring(0,2) + test.substring(2,3).toLowercase() + test.substring(3);

The first substring gets all characters before the desired point, the second gets the desired character and lowercases it, and the final substring gets the rest of the string

第一个子字符串获取所需点之前的所有字符,第二个获取所需字符并将其小写,最后一个子字符串获取字符串的其余部分

回答by Lew Bloch

I wouldn't use 'test.substring(2, 3).toLowerCase()' necessarily. 'Character.valueOf(test.charAt(2)).toUpperCase()' works. Also, the 'test.substring(0, 3)' is wrong; it should be 'test.substring(0, 2)'.

test.substring(2, 3).toLowerCase()不一定会使用 ' '。' Character.valueOf(test.charAt(2)).toUpperCase()' 工作。此外,“ test.substring(0, 3)”是错误的;它应该是“ test.substring(0, 2)”。

回答by Anthony Raymond

A function that capitalize the first letter

首字母大写的函数

private String capitalize(String str) {
    return Character.toUpperCase(str.charAt(0)) + str.substring(1);
}


A function that capitalize an arbitrary letter


将任意字母大写的函数

private String replaceCharWithUpperCase(char letterToCapitalize, String str)
{
    return str.replaceAll(letterToCapitalize, Character.toUpperCase(letterToCapitalize));
}

Then you can use the previous functions like that :

然后你可以像这样使用以前的功能:

String a = "JOHN";
a = capitalize(a.toLowerCase());
// now a = John.

String b = "ROBERT";
a = replaceCharWithUpperCase('b', a.toLowerCase());
// now a = roBert.

回答by Abdelhak

You can use toCharArray()to capitalize the first letter like this:

您可以toCharArray()像这样将第一个字母大写:

String name = "robert";

// Convert String to char array.
char[] arr = name.toCharArray();

// Modify first element in array.
arr[0] = Character.toUpperCase(arr[0]);
String str = new String(arr);
System.out.println(str);

Output:

输出:

Robert

And you want to make "b" Uppercase and rest lowercase like this:

并且你想让 "b" 大写,其余小写如下:

// Convert String to char array.
char[] arr2 = name.toCharArray();

// Modify the third element in array.
arr2[2] = Character.toUpperCase(arr2[2]);
String str2 = new String(arr2);
System.out.println(str2);

Output:

输出:

roBert

回答by Pralhad

//Try this...

String str = "Robert";

for (int i = 0; i < str.length(); i++) {
    int aChar = str.charAt(i);

    // you can directly use character instead of ascii codes
    if (aChar == 'b') {
        aChar = aChar - 32;
    } else if (aChar >= 'A' && aChar <= 'Z') {
        aChar += 32 ;
    }

    System.out.print((char) aChar);
}

/*
Output will be- roBert

/*
输出将是-roBert

*/

*/