73 votes

Conversion des majuscules et des minuscules en Java

Je veux convertir le premier caractère d'une chaîne en majuscule et le reste des caractères en minuscule. Comment puis-je le faire ?

Exemple :

String inputval="ABCb" OR "a123BC_DET" or "aBcd"
String outputval="Abcb" or "A123bc_det" or "Abcd"

112voto

paxdiablo Points 341644

Essayez-le pour la taille :

String properCase (String inputVal) {
    // Empty strings should be returned as-is.

    if (inputVal.length() == 0) return "";

    // Strings with only one character uppercased.

    if (inputVal.length() == 1) return inputVal.toUpperCase();

    // Otherwise uppercase first letter, lowercase the rest.

    return inputVal.substring(0,1).toUpperCase()
        + inputVal.substring(1).toLowerCase();
}

10voto

Bozho Points 273663

WordUtils.capitalizeFully(str) de apache commons-lang a la sémantique exacte requise.

8voto

Strawberry Points 3323
String inputval="ABCb";
String result = inputval.substring(0,1).toUpperCase() + inputval.substring(1).toLowerCase();

Je changerais "ABCb" en "Abcb".

-1voto

Isabella Engineer Points 341
import java.util.Scanner;

class demo

{   String accNo,name,fatherName,motherName;

    int age;
        static double rate=0.25;

    static double balance=1000;
    Scanner scanString=new Scanner(System.in);
    Scanner scanNum=new Scanner(System.in);

    void input()
    {

    System.out.print("Account Number:");
    accNo=scanString.nextLine();
    System.out.print("Name:");
    name=scanString.nextLine();
    System.out.print("Father's Name:");
    fatherName=scanString.nextLine();
    System.out.print("Mother's Name:");
    motherName=scanString.nextLine();
    System.out.print("Age:");
    age=scanNum.nextInt();
    System.out.println();
    }
    void withdraw()
    {System.out.print("How Much:");
    double withdraw=scanNum.nextDouble();
    balance=balance-withdraw;
    if(balance<1000)
    {
        System.out.println("Invalid Data Entry\n Balance below Rs 1000 not allowed");
        System.exit(0);
    }

    }
    void deposit()
    {
        System.out.print("How Much:");
        double deposit=scanNum.nextDouble();
        balance=balance+deposit;
    }
    void display()
    {
        System.out.println("Your  Balnce:Rs "+balance);
    }

    void oneYear()
    {   System.out.println("After one year:");
        balance+=balance*rate*0.01;
    }

    public static void main(String args[])
    {
        demo d1=new demo();
        d1.input();
        d1.display();
    while(true)
    {//Withdraw/Deposit
        System.out.println("Withdraw/Deposit Press W/D:");
        String reply1= ((d1.scanString.nextLine()).toLowerCase()).trim();

      /*Here we used d1.scanString.nextLine()  because we Cannot make a static reference to the non-static field scanString*/

        char reply=reply1.charAt(0);
        if(reply=='w')
        {
            d1.withdraw();
        }
        else if(reply=='d')
        {
            d1.deposit();
        }
        else
        {
            System.out.println("Invalid Entry");
        }

           //More Manipulation 

               System.out.println("Want More Manipulations: Y/N:");
        String manipulation1= ((d1.scanString.nextLine()).toLowerCase()).trim();
        char manipulation=manipulation1.charAt(0);
        System.out.println(manipulation);

        if(manipulation=='y')
        {

        }
        else if(manipulation=='n')
        {
            break;
        }
        else
        {
            System.out.println("Invalid Entry");
            break;
        }
    }   

    d1.oneYear();
    d1.display();

    }

}

/*Note Only way to convert String to char is using charAt()

for e.g  String stringAns="hello";

char charAns=stringAns.charAt(0);//Gives You 'h'

char charAns=stringAns.charAt(1);//Gives You 'e'

char charAns=stringAns.charAt(2);//Gives You 'l'

char charAns=stringAns.charAt(3);//Gives You 'l'

char charAns=stringAns.charAt(4);//Gives You 'o'

char charAns=stringAns.charAt(5);//Gives You:: Exception in thread "main"

java.lang.StringIndexOutOfBoundsException: String index out of range: 5

Above Program Is demonstration of bank Account :))))   */

-5voto

Satvant Singh Points 1
/* This code is just for convert a single uppercase character to lowercase 
character & vice versa.................*/

/* This code is made without java library function, and also uses run time input...*/

import java.util.Scanner;

class CaseConvert {
char c;
void input(){
//@SuppressWarnings("resource")  //only eclipse users..
Scanner in =new Scanner(System.in);  //for Run time input
System.out.print("\n Enter Any Character :");
c=in.next().charAt(0);     // input a single character
}
void convert(){
if(c>=65 && c<=90){
    c=(char) (c+32);
    System.out.print("Converted to Lowercase :"+c);
}
else if(c>=97&&c<=122){
        c=(char) (c-32);
        System.out.print("Converted to Uppercase :"+c);
}
else
    System.out.println("invalid Character Entered  :" +c);

}

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    CaseConvert obj=new CaseConvert();
    obj.input();
    obj.convert();
    }

}

/*OUTPUT..Enter Any Character :A Converted to Lowercase :a 
Enter Any Character :a Converted to Uppercase :A
Enter Any Character :+invalid Character Entered  :+*/

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X