logo

Camel case v Javi

Java za poimenovanje razredov, vmesnikov, metod in spremenljivk sledi sintaksi kamelje male črke. Če je ime združeno z dvema besedama, se bo druga beseda vedno začela z veliko črko, na primer maxMarks(), lastName, ClassTest, pri čemer bodo odstranjeni vsi presledki.

Zadevo Camel lahko uporabljate na dva načina:

  1. Male velike črke, kjer je prvi znak prve besede z malimi črkami. Ta konvencija se običajno upošteva pri poimenovanju metod in spremenljivk. Primer, FirstName, LastName, actionEvent, printArray() itd.
  2. Velike črke, znane tudi kot naslovne začetnice, kjer je prvi znak prve besede napisan z velikimi črkami. Ta konvencija se običajno upošteva pri poimenovanju razredov in vmesnikov. Na primer Employee, Printable itd.

Pretvarjanje navadne vrvice v kamelo črto

Niz je mogoče pretvoriti v male ali velike navadne črke samo tako, da iz niza odstranite presledke.

Primer malih črk Camel:

Vnos: JavaTpoint je najboljše spletno mesto z vadnicami za programske jezike.

Izhod: javaTpointIsTheBestTutorialSiteForProgrammingLanguages.

Primer velikih črk Camel:

Vnos: to je vadnica za java

Izhod: To je vadnica za Java

Algoritem:

  1. Prečkajte niz znakov znak za znakom, dokler ne doseže konca.
  2. Prva črka niza pri indeksu = 0 se pretvori v male črke (če sledi malim črkam) ali v velike črke (če sledi velikim črkam).
  3. V matriki se preverijo presledki in črka, ki sledi presledku, se pretvori v velike črke.
  4. Če naletite na znak, ki ni presledek, se kopira v nastalo matriko.

Implementirajmo algoritem v program Java.

A. Pretvarjanje niza v male črke

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. Pretvarjanje niza v velike črke

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