Banner 468

Monday, April 11, 2011

Strings in Java

0 comments
 
Preliminary In other languages ​​like C / C + + and Pascal (Delphi), the string is an array of characters. In Java, strings are objects and not the array of characters. Can be made array of characters, but he was not a string. The string consists of a series of characters bounded by double quotation marks (double-quote). Here's the difference between the declaration and the declaration char String:

char inputKey; char ampersand = '&';
PerancangJava String = "James Gosling"; StrAmpersand String = "&";

Note the single quotes and double quotes that distinguish the characters and strings. Although both variables contain data of the ampersand character, but Variable ampersand has a primitive type char. Variable strAmpersand have type String object.
String Operations
concat (str) used to mengabungkan 2 pieces string string. isEmpty () used to check whether the string is empty or not. trim () used to remove the spaces on the left and right strings. length () used to calculate many characters in the string. equals (str) used to compare 2 pieces of string. substring (,) used to retrieve a substring from a string. charAt () used to take an existing character on the index. lastIndexOf (kar) used to find the last index of character strings containing kar. equalsIgnoreCase (str) used to compare string with str, not case sensitive. copyValueOf (arr_char) used to format strings from an array of characters. replace (,) used to change into.
Displaying Strings To display the string in a non-GUI Java programs (text), we can use the command System.out.println () or System.out.println (). println () will add a line break character (CR + LF) at the end of the string, while print () does not
{TampilString class public static void main (String args []) { System.out.println ("Designer C:"); System.out.println ("Brian Kernighan and Dennis Ritchie.");
System.out.println (); / / switch row
System.out.println ("Designer Java:"); System.out.println ("James Gosling."); } }

Note the difference between print () and println (). Output program (shown string as follows):
Designer C + +: Brian Kernighan and Dennis Ritchie. Java Designer: James Gosling.

String with the new operator A String object can be made using the new operator. results would be tantamount to a declaration manner described above. Example: String s1 = "JBuilder 9."; String s2 = new String ("JBuilder 9.");

Both variables s1 and s2 would be equally a String object containing the text: JBuilder 9. With the new operator, it can also create a String object from an array of characters. Example: chArray char [] = {'J', 'B', 'u', 'i', 'l', 'd', 'e', ​​'r', '9 '}; String s1 = new String (chArray); / / s1 = "JBuilder 9."

Can also choose a sub-array of array of characters to be converted into a String object. General form is as follows: String [var name] = new String (char [] chArray, int offset, int count)
Where, chArray an array of characters, offset an initial index of the sub-array, count is the number of characters to be retrieved. Example:

Index array or string starting from the number 0 (zero-based index). With offset = 1, then the first character is 'B' (index 1). With count = 5, will be taken as many as 5 characters starting from 'B'. Sub-array that is taken is: 'Build'.

char char [] = {'J', 'B', 'u', 'i', 'l', 'd', 'e', ​​'r', '', '9 '}; String s1 = new String (char, 1, 5); / / s1 = 'Build'.

String as a class In Java, variables of type String is an object of class String. When declared a string, then automatically, the Java compiler will create a String object. In the JDK, the class String is stored in the package java.lang. So the full-name for this class is: java.lang.String. As a class, String have several members in the form of the method. Here are some important methods STRING LENGTH: LENGTH () TAKING SUB-STRING: SUBSTRING () TAKEN CHARACTERS: charAt () DRAWING INDEX: INDEXOF () CASING: toUpperCase (), TOLOWERCASE ()
A. LENGTH () Method length () is used to retrieve the length of the String variable. For example:
String s1 = "JBuilder 9."; int lenStr = s1.length (); / / lenStr = 11 System.out.println (lenStr); / / 11

B. SUBSTRING () Method substring () is used to retrieve sub-string from a String object. There are two forms of this method The first form (overloaded method): String substring (int beginIndex, int endIndex); String substring (int beginIndex); Forms are taking a sub-string starting from position beginIndex to endIndex-1 position
A second form. / / Position: "01234567890" String s1 = "JBuilder 9.";
String cutStr = s1.substring (5); / / cutStr = "der 9." String substr = s1.substring (1, 6); / / substr = "Build"
Forms are taking a sub-string starting from position until the end of the string beginIndex
C. charAt () Method charAt () is used to retrieve characters from the String object on a particular index position. Example: / / Position: "01234567890" String s1 = "JBuilder 9.";
char c1 = s1.charAt (1); / / c1 = 'B' char c2 = s1.charAt (9); / / c2 = '9 ' char c3 = s1.charAt (6); / / c3 = 'e'

D. INDEXOF () Method indexOf () is used to retrieve the index value of a particular character position within the String object. The position of the characters are looking for is the position of the first characters encountered. Example: / / Position: "0123456789012345678" String s1 = "JBuilder for Java";
int pos1 = s1.indexOf ('u'), / / ​​pos1 = 2 int pos2 = s1.indexOf ('t'); / / pos2 = 11 int pos3 = s1.indexOf ('J'); / / pos3 = 0

E. ToUpperCase (), TOLOWERCASE () Method toUpperCase () or toLowerCase () is used to convert all the characters in the String object to uppercase or lowercase. String toUpperCase (); String toLowerCase ();

Leave a Reply