Sunday, November 8, 2009

Free Sheet Music To Mountains By Biffy Clyro

String Class

java_programmers_brain_screensaver_preview The String class represents a string of characters. All the string literals such as "abc" are implemented as instances of this class.
The class String includes methods for examining individual characters of a string to compare, locate and remove them as sub chains to create copies of a string converting all characters to uppercase or lowercase.
Java provides special support for the concatenation (joining) chain operator (+). and to convert other objects cadenas.La String concatenation is implemented through the StringBuffer class and its append () method. The conversions are implemented through the method toString () defined in the Object class which is inherited by all classes in Java.

I. - TO CONSIDER:
  1. The String class belongs to the package java.lang.
  2. There are different ways to create String objects as:
    • String str = "abc";
    • char str [] = {'a', 'b', 'c'}; / / array of characters
    • String str = new String ("abc"); / / Object with initial value "abc"

  3. The language Java provides operator "+" to concatenate (join) String objects.

  4. A String represents a string-coded, therefore, an operation as converting to uppercase or lowercase does not modify the original object but returns a new object with the string resulting from this operation which should be stored in another string.
II .- CLASS HIERARCHY
image
III .- most important methods
  1. String (String value) .- The Used when you want to create a string:
    • String str = "Hello"
    • String str = new String ("Hello") / / Constructor for the String class


  2. cadena.concat (string txt) .- It returns a new String object concatenation of two strings:

  3.  String str = "Hello" 
    cadena.concat result = String ("World ");// The Hello world result will

  4. cadena.compareTo (String another string) .- lexicographically Compare the string with another string, this character by character comparison is given that the capital letters are larger than tiny - this is according to the ASCII or UNICODE - as it returns:


    • \u0026lt;0: if string \u0026lt;another string
    • = 0: if another string str =
    • > 0: if string> another string
    • Example: JUAN
    • > juan
    • Juan> Carlos
    • JUAN> JOHN
    • abcde> abcdefg

  5. cadena.toIgnoreCase (String another string) .- compareTo does the same thing but with only difference does not distinguish between uppercase and lowercase letters, ie: John = john
  6. cadena.length () .- Returns the length or number of characters in the string:

    String str = "Hello World";
    cad.length ();// int qty = value cannot be 10
  7. cadena.toLowerCase () .- Converts uppercase to lowercase
  8. chain
  9. cadena.toUpperCase () .- Converts uppercase to lowercase chain.


     String str1 = "This is a test"; 
    cad1.toUpperCase ();// String str2 = str2 will be: THIS IS A TEST.

  10. cadena.trim () .- Removes blank spaces to be at the beginning or end of the chain.


     String str1 = "This is a test"; 
    cad1.toLowerCase ();// String str2 = str2 = This is a test.

IV .- METHODS

Char boolean int
BUILDERS
Constructor
Description
String ()
Creates a new empty String no characters.
String (byte [] bytes)
Create a new String object initialized with the characters equivalent to each number stored in the array of bytes.
String (char [] value)
Create a new String object initialized with the characters in the array given as parameter.
String (String string)
Create a new String object initialized with the given value as a parameter.
METHODS
Return Type
Method
Description

charAt (int index)
Returns the character at index given as parameter.
int
compareTo (String otraCadena)
Returns as integer the result of the comparison between the chain calls him otraCadena. If the return parameter is "0" means that the two strings are equal, making the comparison character with differing uppercase character to lowercase.
int
compareToIgnoreCase (String otraCadena)
Returns as integer the result of comparing the string with otraCadena calls. If this integer is 0 means that the strings match, being the character comparison with character but ignoring the differences between upper and lower case.
String
concat (String otraCadena)
Returns the result of the concatenation of the string given as parameter otraCadena.

equals (Object anotherObject)
Returns true if the string that is equal to the object name submitted as a parameter.
boolean
equalsignoreCase (String otraCadena)
Returns true if the string is equal to call him Dad as a parameter string ignoring differences between upper and lower case.
int
indexOf (String str)
Return the position of first occurrence of the given string found as a parameter in the string that calls the method.
int
indexOf (String str, int ini)
Returns the position of the first found occurrence of the string given as parameter but starting the search from the initial position given as parameter.
int
lastIndexOf (String str)
Return the position of last occurrence of the string given as parameter.

lastIndexOf (String str, int ini)
Returns the position of last occurrence of the string given as parameter searching backward from the position given as a parameter.
int
length ()
Returns the number of characters in the string.
String
replace (char car1, char car2)
Returns a new string resulting from replacing all occurrences of the character "car1" by the character "car2" given as parameters.
String
replaceAll (String str1, String str2)
Returns a new string resulting from replacing all occurrences of the string sub for sub str1 str2 string given as parameter .
boolean
startsWith (String str)
Returns true if the string that calls the method starts with the substring cad given as parameter.

String substring (int posIni)
Returns a substring whose first character starts at position posIni given as parameter.
String
substring (int posIni, int posFin)
Returns a substring whose first character posIni position begins and ends with the character of posFin position.
String
toLowerCase ()
Returns a new string by converting all characters a minúsculas.
String
toUpperCase ( )
Retorna una nueva cadena como resultado de convertir todos los caracteres a mayúsculas.
String
trim ( )
Retorna una nueva cadena como resultado de omitir los espacios en blanco al inicio y al final de la cadena.

V.- EJEMPLOS

  1. Design an application that can manipulate the characters in a substring by the methods seen.

    image

    • create a new project which will place it in a package, in this new package we image add a JFrame to which we will distribution type GridLayout with 2 columns, done this in the first column put a JPanel (JPANEL1) and give a distribution of BorderLayout (in this panel placed in the north a text box in the CENTER a ScrollPane and over a txtArea ... finally in the south place a label), the second column put another JPanel (JPanel2) with GridLayout distribution on 2 columns and 7 rows and simply drag buttons to the panel and see which will be ordered as shown in the picture above. Are reminded to visit this entry to get more information about the types of distribution and this entry to see how to create a NetBeans project. Well done all of this object in view will look like this (Note the way how it is distributed)



    • Now let's program each button, for this alone is enough to give NetBeans and then double-click will take you to the corresponding method:



      • We will schedule button length which will tell us how many characters of the string that will get into the textArea; to be able to get the value entered in the text box and will use the getText method to write the output in the text area will use the setText method.

        private void
         btnLongitudActionPerformed (java.awt.event.ActionEvent evt) {
        / / read the message
        txtMensaje.getText str = String ();

        / / get the length longitudinal = cadena.length
        int ();

        / / prints txtSalida.setText
        length ("The text entered is: "+ longitudinal +" characters. ");
        }


      • returned to the designer and you double click the button to separate words and enter the following code:


         private void btnSeparaPalabrasActionPerformed (java.awt.event.ActionEvent evt) {
        / / read the message String
        str = txtMensaje.getText ();
        / / get
        int length = cadena.length longitudinal ();
        / / clean area
        txtSalida.setText
        (""); / / initialize counter int
        words contpala = 0 ;
        / / separating words by space

        do {/ / do not consider space at the beginning or end
        cadena.trim str = ();
        / / locates the first character position int pos = white
        cadena.indexOf ('');
        / / get word found
        String word = "";
        if (pos> 0)
        cadena.substring word = (0, pos +1);

        else word = string;
        / / prints txtSalida.append
        found word (word + "\\ n");
        / / ignore string = word found
        cadena.substring (palabra.length ());
        / / update contpala
        word count + +;
        } while (cadena.length ()> 0);
        / / print counter txtSalida.append
        words ("\\ n" + contpala + "words found.");}



      • Do the same with the case
        button private void
         btnMayusculasActionPerformed ( java.awt.event.ActionEvent evt) {
        / / TODO add your handling code here:
        / / read the message
        String str = txtMensaje.getText ();
        / / converts it to uppercase
        cadena.toUpperCase str = ();
        / / prints new string in uppercase
        txtSalida.setText (Text converted to upper case: \\ n "+ str) ;}

      • And with the tiny button:
        private void
         btnMinusculasActionPerformed (java.awt.event.ActionEvent evt) {
        / / read the message
        txtMensaje.getText String str = ();
        / / converts it to lowercase
        cadena.toLowerCase str = ();
        / / prints new
        txtSalida.setText string lowercase ("Text converted to lower case: \\ n" + string);
        }


      • The same character with the button and other buttons (which for reasons of time and space are not going to drop but you can download it by clicking the download button)


      image
    • finally run with F6
btn_6

0 comments:

Post a Comment