View RSS Feed

Java String

Getting Characters and Substrings by Index

Rate this Entry
by , 04-26-2012 at 06:34 PM (412 Views)
Character can be obtained at one particular index, within a string, by charAt() accessor methodís invoking. First character index is 0 however last character index is length()-1. This given example shows the code which obtains the character, at index 9 present in string.
String anotherPalindrome = "Niagara. O roar again!";

char aChar = anotherPalindrome.charAt(9);

Indices start with 0 therefore the characters which are present at the index 9 are 'O'. Here it is shown below.

Name:  1.jpg
Views: 48
Size:  7.5 KB
Getting Characters and Substrings by Index

From a string obtain more then 1 characters which are also consecutive, by using substring method.

Submit "Getting Characters and Substrings by Index" to Facebook Submit "Getting Characters and Substrings by Index" to Digg Submit "Getting Characters and Substrings by Index" to del.icio.us Submit "Getting Characters and Substrings by Index" to StumbleUpon Submit "Getting Characters and Substrings by Index" to Google

Categories
String

Comments