Android:将字符串的第一个字母转换为小写

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);

谢谢!

3个回答

18
    if (monb.length() <= 1) {
        monb = monb.toLowerCase();
    } else {
        monb = monb.substring(0, 1).toLowerCase() + monb.substring(1);
    }

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;
}

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接