2011-09-23 14 views
7

Szukam sposobu na konwersję pierwszej litery ciągu na małą literę. Kod, którego używam, pobiera ciąg losowy z tablicy, wyświetla ciąg w widoku tekstowym, a następnie używa go do wyświetlania obrazu. Wszystkie ciągi w tablicy mają wielką literę, ale pliki obrazów zapisane w aplikacji nie mogą oczywiście zawierać wielkich liter.Android: Konwertuj pierwszą literę łańcucha na małą literę

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

Dzięki!

Odpowiedz

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

Proste i skuteczne! Dzięki – 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 jest biblioteka Java z wielu narzędzi i komponentów wielokrotnego użytku. To wymaga, aby biblioteka guava-10.0.jar była w ścieżce klas. Poniższy przykład pokazuje różne konwersje 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 
    } 

} 

Wyjście odczuwalna:

STUDENT_NAME 
studentName 
StudentName 
student-name 
Powiązane problemy