by Dinesh Thakur Category: array

The String class also provide methods for extracting characters from a string in an array of bytes. This is achieved using the getBytes () method in the String class. This method converts the original string characters into the character encoding used by the underlying operating system which is usually ASCII . For example : The statement,



 
by Dinesh Thakur Category: array

As we know that strings are not arrays so the String class also provides methods to convert a string to an array of characters and vice versa. These include

 
by Dinesh Thakur Category: array

Programmers often need to retrieve an individual character or a group of characters (substring) from a string. For example: In a word processing program, when a part of string is copied or deleted. While charAt () method returns only a single character from a string, the substring () method in the String class can be used to obtain a substring from a string.

 
by Dinesh Thakur Category: array

boolean matches (String regex): This method tests whether or not the invoking string matches the given regular expression regex. It returns true if invoking string matches the given regular expression otherwise it returns false.It is different from the equals () method where you can only use the fixed strings. Forexample,

 
by Dinesh Thakur Category: array

String [] split (String regex):This method splits the invoking string into substrings at each match for a specified regular expression regex and returns a reference to an array of substrings. For example, consider the statements.

 
by Dinesh Thakur Category: array

String replaceFirst (String regex, String replace): This method replaces the first substring in the invoking string that matches the given regular expression with the specified replacement string replace. For example,

 
by Dinesh Thakur Category: array

String replaceAll (String regeX, String replace): This method replaces all substrings in the invoking string that matches the given regular expression pattern regeX with the specified replacement string replace. For example,

 
by Dinesh Thakur Category: array

String replace (char oldchar, char newchar): This method returns a new string resulting from replacing all the occurrence of oldchar in the invoking string with newchar. For example,

 
by Dinesh Thakur Category: array

String trim (): This method returns a copy of the string, with leading and trailing white spaces omitted. For example,

 
by Dinesh Thakur Category: array

String toLowercase (): This method converts all of the characters in the string to lowercase.

For example,

 
by Dinesh Thakur Category: array

String toUppercase ():This method converts all of the characters in the invoking string to uppercase. For example,

 
by Dinesh Thakur Category: array

The String class also provides a number of methods for comparing strings. While discussing these, we assume string sl contains 'Welcome' in it.

 
by Dinesh Thakur Category: array

Searching a string for finding the location of either a character or group of characters (substring) is a common task. For example: In some situations, usernames and passwords are stored in a single string in which they are separated from each other by a special character such as colon (Example: username : password). In order to get the username and password separately from such a string, there must be some method such that we can search the string and return the location of the colon character. The String class provides the following methods for performing these tasks,

 
by Dinesh Thakur Category: array

The String class provides methods for obtaining length, retrieving individual characters and concatenating strings. These include

 
by Dinesh Thakur Category: array

The String object can be created explicitly by using the new keyword and a constructor in the same way as you have created objects in previously. For example The statement
String str = new String("Welcome to Java");

 
by Dinesh Thakur Category: array

Just like one-dimensional arrays, a two-dimensional array can also be passed to a method and it can also be returned from the method. The syntax is similar to one-dimensional arrays with an exception that an additional pair of square brackets is used.

 
by Dinesh Thakur Category: array

when we created arrays of arrays (i.e. two-dimensional array), the arrays in the array were of same length i.e. each row had the same number of elements. As each array in a multidimensional array is a separate array so it can have different number of elements. Such arrays are known as non-rectangular arrays or ragged arrays.

 
by Dinesh Thakur Category: array

Like one dimensional array, one can also initialize multidimensional array when they are declared. For example: A two-dimensional array table with 2 rows and 3 columns can be declared and initialized as follows,

 
by Dinesh Thakur Category: array

Multi-dimensional arrays that contain more than one index and can store information in multiple dimensions.  The most common one is a two-dimensional array, also called a matrix or table. In the two-dimensional array, each element associated with two indexes.  We can visualize the two-dimensional array as a spreadsheet, rectangular in shaper and containing elements that divided into columns and rows. However, Java does not indeed support multidimensional arrays. However, one can achieve the same functionality by declaring an array of arrays.



 
by Dinesh Thakur Category: array

Just like a method can return a primitive type, similarly a method can also return an array. If a method returns an array then its return type is an array. This is really useful when a method computes a sequence of values.

In order to understand how an array is returned from a method, consider a program that returns an array which is the reverse of another array.

 

Page 3 of 5



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles