1.

How To Get Characters And Substrings By Index With In A String?

Answer»

You can get the character at a particular INDEX WITHIN a string by invoking the charAt() accessor method.

String str = "Example String";
char resChar = str.charAt(3);

Will give char ‘m’. If you WANT to get more than one consecutive character from a string, you can USE the substring method. The substring method has two versions -

  • String substring(int beginIndex, int endIndex) - Returns a new string that is a substring of this string.
  • String substring(int beginIndex) - Returns a new string that is a substring of this string.

You can get the character at a particular index within a string by invoking the charAt() accessor method.

String str = "Example String";
char resChar = str.charAt(3);

Will give char ‘m’. If you want to get more than one consecutive character from a string, you can use the substring method. The substring method has two versions -



Discussion

No Comment Found