logo

Kamelenkoffer op Java

Java volgt de syntaxis van camel-case voor het benoemen van de klassen, interfaces, methoden en variabelen. Als de naam wordt gecombineerd met twee woorden, begint het tweede woord altijd met een hoofdletter, zoals maxMarks( ), achternaam, ClassTest, waarbij alle spaties worden verwijderd.

Er zijn twee manieren om Camel Case te gebruiken:

  1. Kleine kameelletter waarbij het eerste teken van het eerste woord in kleine letters staat. Deze conventie wordt meestal gevolgd bij het benoemen van de methoden en variabelen. Bijvoorbeeld voornaam, achternaam, actionEvent, printArray( ), enz.
  2. De bovenste kameelkast, ook wel de titelkast genoemd, waarbij het eerste teken van het eerste woord in hoofdletters staat. Deze conventie wordt meestal gevolgd bij het benoemen van de klassen en interfaces. Bijvoorbeeld Werknemer, Afdrukbaar, etc.

Een normale snaar ombouwen tot camelcase

Een string kan worden omgezet in de onderste of bovenste camel case-conventie door simpelweg de spaties uit de string te verwijderen.

Voorbeeld van een onderste camelbehuizing:

Invoer: JavaTpoint is de beste tutorialsite voor programmeertalen.

Uitgang: javaTpointIsTheBestTutorialSiteForProgrammingLanguages.

Voorbeeld van een bovenste kameelkast:

Invoer: dit is de Java-tutorial

Uitgang: Dit is de Java-tutorial

Algoritme:

  1. Doorloop de karakterarray karakter voor karakter totdat het einde is bereikt.
  2. De eerste letter van de string bij index = 0 wordt geconverteerd naar kleine letters (bij het volgen van kleine kameelletters) of naar hoofdletters (bij het volgen van hoofdletters van kameel).
  3. De array wordt gecontroleerd op spaties en de letter die onmiddellijk volgt op de spatie wordt omgezet in hoofdletters.
  4. Als het niet-spatieteken wordt aangetroffen, wordt dit naar de resulterende array gekopieerd.

Laten we het algoritme in een Java-programma implementeren.

A. String omzetten in lagere camelbehuizing

LowerCamel.java

 public class LowerCamel { // function to convert the string into lower camel case static String convertString( String s ) { // to keep track of spaces int ctr = 0 ; // variable to hold the length of the string int n = s.length( ) ; // converting the string expression to character array char ch[ ] = s.toCharArray( ) ; // keep track of indices of ch[ ] array int c = 0 ; // traversing through each character of the array for ( int i = 0; i <n; 0 1 i++ ) { the first position of array i.e., letter must be converted to lower case as we are following camel in this program if( i="=" converting using tolowercase( in-built function ch[ ]="Character.toLowerCase(" ; need remove all spaces between, check for empty if ( ' incrementing space counter by ctr++ immediately after upper + continue loop } is not encountered simply copy character else c++ size new string will reduced have been removed thus, returning with return string.valueof( ch, 0, n - ctr driver code public static void main( args[ passing name a method str="Max Marks()" system.out.println( convertstring( variable str1="Last name" str2="JavaTpoint is the best tutorial site for programming languages." < pre> <p> <strong>Output:</strong> </p> <pre> maxMarks() lastName javaTpointIsTheBestTutorialSiteForProgrammingLanguages. </pre> <h3>B. Converting String to Upper Camel Case</h3> <p> <strong>UpperCamel.java</strong> </p> <pre> public class UpperCamel { // function to convert the string into upper camel case static String convertString( String s ) { // to keep track of spaces int ctr = 0 ; // variable to hold the length of the string int n = s.length( ) ; // converting the string expression to character array char ch[ ] = s.toCharArray( ) ; // // keep track of indices of ch[ ] array int c = 0 ; // traversing through each character of the array for ( int i = 0; i <n; 0 1 i++ ) { the first position of array i.e., letter must be converted to upper case. we checked this before second if statement as that is executed only when it encounters space and, there no a string. if( i="=" converting case using touppercase( in-built function ch[ ]="Character.toUpperCase(" ; need remove all spaces in between, check for empty ( ' incrementing counter by ctr++ immediately after + 1] continue loop } not encountered simply copy character else c++ new string will reduced have been removed thus returning with size return string.valueof( ch, 0, n - ctr driver code public static void main( args[ passing name method str="class test" system.out.println( convertstring( variable str1="employee" str2="this is the java tutorial" system.out.println(convertstring( < pre> <p> <strong>Output:</strong> </p> <pre> ClassTest Employee ThisIsTheJavaTutorial </pre> <hr></n;></pre></n;>

B. Tekenreeks omzetten in hoofdletter Kameel

UpperCamel.java

 public class UpperCamel { // function to convert the string into upper camel case static String convertString( String s ) { // to keep track of spaces int ctr = 0 ; // variable to hold the length of the string int n = s.length( ) ; // converting the string expression to character array char ch[ ] = s.toCharArray( ) ; // // keep track of indices of ch[ ] array int c = 0 ; // traversing through each character of the array for ( int i = 0; i <n; 0 1 i++ ) { the first position of array i.e., letter must be converted to upper case. we checked this before second if statement as that is executed only when it encounters space and, there no a string. if( i="=" converting case using touppercase( in-built function ch[ ]="Character.toUpperCase(" ; need remove all spaces in between, check for empty ( \' incrementing counter by ctr++ immediately after + 1] continue loop } not encountered simply copy character else c++ new string will reduced have been removed thus returning with size return string.valueof( ch, 0, n - ctr driver code public static void main( args[ passing name method str="class test" system.out.println( convertstring( variable str1="employee" str2="this is the java tutorial" system.out.println(convertstring( < pre> <p> <strong>Output:</strong> </p> <pre> ClassTest Employee ThisIsTheJavaTutorial </pre> <hr></n;>