8

文字列の最初の文字を小文字に変換する方法を探しています。私が使用しているコードは、配列からランダムな文字列を取得し、文字列をテキスト ビューに表示してから、それを使用して画像を表示します。配列内のすべての文字列は最初の文字が大文字になっていますが、アプリに保存されている画像ファイルはもちろん大文字にすることはできません。

String source = "drawable/"
//monb is randomly selected from an array, not hardcoded as it is here
String monb = "Picture";

//I need code here that will take monb and convert it from "Picture" to "picture"

String uri = source + monb;
    int imageResource = getResources().getIdentifier(uri, null, getPackageName());
    ImageView imageView = (ImageView) findViewById(R.id.monpic);
    Drawable image = getResources().getDrawable(imageResource);
    imageView.setImageDrawable(image);

ありがとう!

4

3 に答える 3

18
    if (monb.length() <= 1) {
        monb = monb.toLowerCase();
    } else {
        monb = monb.substring(0, 1).toLowerCase() + monb.substring(1);
    }
于 2011-09-23T22:56:48.087 に答える
8
public static String uncapitalize(String s) {
    if (s!=null && s.length() > 0) {
        return s.substring(0, 1).toLowerCase() + s.substring(1);
    }
    else
       return s;
}
于 2011-09-23T22:57:11.283 に答える
2

Google Guava は、多くのユーティリティと再利用可能なコンポーネントを備えた Java ライブラリです。これには、ライブラリguava-10.0.jarがクラスパスにある必要があります。次の例は、さまざまなCaseFormat変換の使用を示しています。

import com.google.common.base.CaseFormat;

public class CaseFormatTest {

    /**
    * @param args
    */
    public static void main(String[] args) {

    String str = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, "studentName");
    System.out.println(str);  //STUDENT_NAME

    str = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "STUDENT_NAME");
    System.out.println(str);  //studentName


    str = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, "student-name");
    System.out.println(str);  //StudentName

    str = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "StudentName");
    System.out.println(str);  //student-name
  }

}

次のような出力:

STUDENT_NAME
studentName
StudentName
student-name
于 2013-07-24T11:06:23.403 に答える