Charat () and codepointat () Examples of common string Methods

Source: Internet
Author: User
Tags string methods

1. chatat () -- extract the specified string

2. codepointat () -- extract index character code points

 

Java code
  1. /**
  2. * Author: Sunshine flavor
  3. * Function: charat () and codepointat () common methods of the string class ()
  4. * Date: 2010/11/07
  5. **/
  6. Public ClassStringdemo {
  7. Public Static VoidMain (string [] ARGs ){
  8. String str1 = "abcdefg ";
  9. CharCharacter = str1.charat (0 );
  10. System. Out. println ("using charat () method" +
  11. "Extract characters from a string. The result is:" + signature );
  12. IntCodepoint = 0;
  13. For(IntI = 0; I <8; I ++ ){
  14. Try{
  15. Codepoint = str1.codepointat (I );
  16. }Catch(Stringindexoutofboundsexception E1 ){
  17. System. Out. println ("index value called by codepointay ()" + I +
  18. "The length of the string to be queried is exceeded! ");
  19. }Finally{
  20. Try{
  21. System. Out. println (str1.charat (I)
  22. + "Unicode code" + ":" + codepoint );
  23. }Catch(Stringindexoutofboundsexception E2 ){
  24. System. Out. println ("charat () called index value" + I +
  25. "The length of the string to be queried is exceeded! ");
  26. }
  27. }
  28. }
  29. }
  30. }
  31. /* Out:
  32. Use the charat () method to extract characters from a string. The result is:.
  33. Unicode code of a: 97
  34. Unicode code of B: 98
  35. C Unicode code: 99
  36. D Unicode code: 100
  37. The Unicode code of E is: 101
  38. Unicode Code of F: 102
  39. Unicode code of G: 103
  40. The index value 7 called by codepointay () has exceeded the length of the string to be queried!
  41. The index value 7 called by charat () has exceeded the length of the string to be queried! */

{
Function onclick ()
{
DP. Sh. toolbar. copytoclipboard (this); Return false;
}
} "Href =" # ">

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.