java 将备用字符转换为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28051983/
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-11-02 12:55:38  来源:igfitidea点击:

Convert alternate char to uppercase

javanetbeans

提问by sam007

I am new to java programming. I want to print a string with alternate characters in UpperCase.

我是java编程的新手。我想用大写的替代字符打印一个字符串。

String x=jTextField1.getText();
x=x.toLowerCase();
int y=x.length();
for(int i=1;i<=y;i++)
{}

I don't know how to proceed further. I want to do this question with the help of looping and continue function. Help would be appreciated. Thanks.

我不知道如何进一步。我想借助循环和继续功能来解决这个问题。帮助将不胜感激。谢谢。

回答by davesbrain

@Test
public void alternateUppercase(){
    String testString = "This is a !!!!! test - of the emergency? broadcast System.";

    char[] arr = testString.toLowerCase().toCharArray();

    boolean makeUppercase = true;
    for (int i=0; i<arr.length; i++) {
        if(makeUppercase && Character.isLetter(arr[i])) {
            arr[i] = Character.toUpperCase(arr[i]);
            makeUppercase = false;
        } else if (!makeUppercase && Character.isLetter(arr[i])) {
            makeUppercase = true;
        }
    }
    String convertedString = String.valueOf(arr);
    System.out.println(convertedString);
}

回答by Peter Lawrey

Strings start at index 0 and finish at index x.length()-1

字符串从索引 0 开始并在索引处结束 x.length()-1

To look up a String by index you can use String.charAt(i)

要按索引查找字符串,您可以使用 String.charAt(i)

To convert a character to upper case you can do Character.toUpperCase(ch);

要将字符转换为大写,您可以执行以下操作Character.toUpperCase(ch)

I suggest you build a StringBuilderfrom these characters which you can toString()when you are done.

我建议你StringBuilder从这些角色中构建一个,toString()当你完成时你可以。

回答by Elliott Frisch

First, java indexes start at 0(not 1). I think you are asking for something as simple as alternating calls to Character.toLowerCase(char)and Character.toUpperCase(char)on the result of modulo(remainder of division) 2.

首先,java 索引从0(not 1)开始。我认为您要求的只是对(除法的余数)2的结果进行交替调用Character.toLowerCase(char)Character.toUpperCase(char)对结果的调用一样简单。

String x = jTextField1.getText();
for (int i = 0, len = x.length(); i < len; i++) {
    char ch = x.charAt(i);
    if (i % 2 == 0) {
        System.out.print(Character.toLowerCase(ch));
    } else {
        System.out.print(Character.toUpperCase(ch));
    }
}
System.out.println();

回答by roeygol

you can make it using the 65 distnace of lower case and upper case ABCabc from the unicode table like:

您可以使用来自 unicode 表的小写和大写 ABCabc 的 65 距离来制作它,例如:

String str = "abbfFDdshFSDjdFDSsfsSdoi".toLowerCase();

char c;
boolean state = false; 
String newStr = "";
for (int i=0; i<str.length(); i++){
    c = str.charAt(o);

    if (state){
         newStr += c;
    }
    else {
         newStr += c + 65;
    }
    state = !state;
}

回答by bakoyaro

I'm sure there is a slicker way to do this, but this will work for a 2 minute-answer:

我确信有一种更巧妙的方法可以做到这一点,但这将在 2 分钟内有效:

public String homeWork(){
        String x = "Hello World";
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<=x.length();i++){
             char c = x.charAt(i);
             if(i%2==0){
                sb.append(String.valueOf(c).toUpperCase());
             } else {
                sb.append(String.valueOf(c).toLowerCase());
             }
        }
        return sb.toString();
    }

To explain i%2==0, if the remainder of i divided by 2 is equal to zero (even numbered) return true

解释 i%2==0,如果 i 除以 2 的余数等于 0(偶数),则返回 true

回答by java.manish.2015

public class PrintingStringInAlternativeCase {
    public static void main(String s[])
    {
        String testString = "TESTSTRING";
        String output = "";
        for (int i = 0; i < testString.length(); i++) {
            if(i%2 == 0)
            {
                output += Character.toUpperCase(testString.toCharArray()[i]);
            }else
            {
                output += Character.toLowerCase(testString.toCharArray()[i]);
            }
        }

        System.out.println("Newly generated string is as follow: "+ output);
    }
}

回答by Matt

Using as much of your code as I could, here's what I got. First I made a string called build that will help you build your resulting string. Also, I changed the index to go from [0,size-1] not [1,size]. Using modulo devision of 2 helps with the "every other" bit.

尽可能多地使用你的代码,这就是我得到的。首先,我创建了一个名为 build 的字符串,它将帮助您构建生成的字符串。另外,我将索引从 [0,size-1] 改为 [1,size]。使用 2 的模除法有助于“每隔一个”位。

String build =""
String x=jTextField1.getText();
x=x.toLowerCase();
int y=x.length();
for(int i=0;i<y;i++)
{
     if(i%2==0){
        build+=Character.toUpperCase(x.charAt(i));
     else{
        build+=x.charAt(i);
     }
}
x=build; //not necessary, you could just use build.

Happy oding! Leave a comment if you have any questions.

奥丁快乐!如果您有任何问题,请发表评论。

回答by Anne Lingesh

public static void main(String[] args)
{
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter Stirng");
    String str=sc.nextLine();
    for(int i=0;i<str.length();i++)
    {
        if(i%2==0)
        {
            System.out.print(Character.toLowerCase(str.charAt(i)));
        }
        else
        {
            System.out.print(Character.toUpperCase(str.charAt(i)));
        }
    }

    sc.close();
}

回答by DecipherX

Java 8 Solution:

Java 8 解决方案:

static String getMixedCase(String str) {
    char[] chars = str.toCharArray();
    return IntStream.range(0, str.length())
                    .mapToObj(i -> String.valueOf(i % 2 == 1 ? chars[i] : Character.toUpperCase(chars[i])))
                    .collect(Collectors.joining());
}