English 中文(简体)
如何在不使用上下调法的情况下将体力转换为上层?
原标题:How to convert a string to uppercase without using the toUpperCase method?
  • 时间:2016-12-14 23:37:24
  •  标签:
  • java
  • string

I m astartner at java and can ted this Code to work. 我必须做的是,在不使用上下调方法的情况下,将任何经投入扼杀物转换成上层。 这是我拥有的:

public String toUpperCase(String str)
{
    for(int i = 0; i < str.length(); i++)
    {
        char a = str.charAt(i);
        a = Character.toUpperCase(a);
        str += Character.toString(a);
    }
return str;
}
问题回答

You are using str as input, and output (so your String has infinite length, as you keep adding characters). And you can use static, because you aren t using instance state. And, you might use a for-each loop. Finally, add another String, or better a StringBuilder like

public static String toUpperCase(String str) {
    StringBuilder sb = new StringBuilder();
    for (char ch : str.toCharArray()) {
        sb.append(Character.toUpperCase(ch));
    }
    return sb.toString();
}

我知道,你的学校可能不允许你使用<代码>StringBuilder,如果你也能使用阵列。 这是贵国学校可能接受的另一个原始做法:

public static String toUpperCase(String s){
    String str = "";
    for(int x=0; x<s.length(); x++){
        char ch = s.charAt(x);
        if(ch >=  a  && ch <=  z )
            str += "" + (char)(ch - 32);
        else
            str += "" + ch; 
    }
    return str;
}

<>试验:

System.out.println(toUpperCase("aAbBcC"));

<<>Output>:

AABBCC

由于你可以使用<代码>toUpperCase()方法,你可使用ASCII table,通过分拨32,从低案信到上案信。

a = 97, A = 65
b = 98, B = 66
...
z = 122, Z = 90

public static int DIFF =  a  -  A ; // 32

public static String toUpperCase(String str) {
    StringBuilder sb = new StringBuilder();
    for (char c : str.toCharArray()) {
        if (Character.isLowerCase(c)) {
            sb.append(String.valueOf((char)(c - DIFF)));
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}

审判:

public static String toUpperCase(String str) {
        String result = "";
        for (int i = 0; i < str.length(); i++) {
            int v = str.charAt(i);
            if (v > 96 && v < 123) {
                v -= 32;
            }
            result+=(char)v;

        }
        return result;
    }

C mon guys, Java 8, 多年来已经消失!

/**
 * Converts an all-lowercase String to
 * uppercase. Retains only spaces, any
 * other characters will be lost.
 */
public static String toUpperCase(String s) {
    int diff =  a  -  A ; // 32

    return s.chars()
            .filter(c -> c >=  a  && c <=  z  || c ==    )
            .mapToObj(c -> String.valueOf((char) (c - (diff))))
            .collect(Collectors.joining());
}
public String toUpper(String str){

String strUp = "";
for(int i = 0; i<str.length(); i++) {
    char a = str.charAt(i);
    strUp += Character.toUpperCase(a);
iii
return strUp;

iii





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签