Page 349 - Computer science 868 Class 12
P. 349

To convert a character to its ASCII value, the syntax is as follows:
                    int variable = (int) character/variable containing character;
                 For example,

                    char ch= 'j';
                    int ac = (int)ch;
                    System.out.println(ac);
                 //Output: 106 (since ASCII value of ‘a’ is 97, so the value of 'j' is 106.)
                 Similarly, an ASCII value can be converted to character.

                 The syntax is as follows:

                    char ch= (char) ascii_variable/Number;
                 For example,

                    int n=66;
                    char ch=(char)n;
                    System.out.println(ch);
                 //Output: B (as ASCII value of 'A' is 65)

                     10.2 STRING FUNCTIONS
                 There are many built-in string methods. They are used to manipulate Strings. Some of the built-in methods are as
                 follows.
                   1. int length(): This method is used to return the number of characters present in the string. It returns an integer type
                    of value. The length() is the only function in which the counting starts from 1.
                    Syntax: int <variable> = Stringobject.length();

                    Example:
                        String str = "String functions";
                        int len = str.length();
                        System.out.println("The length is :" +len);
                    Output:
                    The length is: 16
                   2. char charAt(int index): This method returns the character at the given index. It returns char type data.
                    Syntax: char <variable> = Stringobject.charAt(int index);
                    Example:
                        String str = "String functions";
                        char ch = str.charAt(10);
                        System.out.println("The character is :" +ch);
                    Output:
                    The character is: c

                   3.  int indexOf(char ch): This method returns the index value of the first existence of the character ch present in the
                    String object. It returns integer type of value.

                    Syntax: int <variable> = Stringobject.indexOf(char ch);
                    Example:
                        String str = "Computer Science – Class XII";
                        int s = str.indexOf('C');
                        System.out.println("The index value is :" +s);




                                                                                                                       347
                                                                                                              Strings  347
   344   345   346   347   348   349   350   351   352   353   354