2011-09-23 5 views
7

Sto cercando un modo per convertire la prima lettera di una stringa in una lettera minuscola. Il codice che sto usando estrae una stringa casuale da una matrice, visualizza la stringa in una visualizzazione testuale e quindi la usa per visualizzare un'immagine. Tutte le stringhe dell'array hanno la prima lettera maiuscola, ma i file di immagine memorizzati nell'app non possono avere maiuscole, naturalmente.Android: Converti prima lettera di stringa in minuscolo

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

Grazie!

risposta

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

Semplice ed efficace! Grazie – cerealspiller

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 è una libreria Java con il lotto delle utenze e componenti riutilizzabili. Ciò richiede che la libreria guava-10.0.jar sia in classpath. L'esempio seguente mostra l'utilizzo di varie conversioni 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 
    } 

} 

output come:

STUDENT_NAME 
studentName 
StudentName 
student-name