# Strings ## Character A character represents a single symbol. A symbol can be any of the following: * Uppercase Characters **[A - Z]** * Lowercase Characters **[a - z]** * Numeric Characters **[0 - 9]** * Special Characters **[@ , $ , # , < , = , etc.]** ### Syntax ``` java char name = 'single_character'; ``` Code : ``` java char ch = 'a'; ``` ### American Standard Code for Information Interchange ![](https://i.imgur.com/AZTBm7N.png) ### Rules of Typecasting 1. **Char to int** is implicit typecasting, we get the ASCII value of the character Code 1: ``` java Line1: int x = 'a'; Line2: x = x+2; Line3: System.out.println(x); ``` Output: ``` java 99 ``` Explanation: ``` The ASCII value of 'a' is 97. So, 97 is stored in variable x. Line 2 increments the value of x by 2 Hence, Line 3 prints 99 in the output. ``` Code 2: ``` java Line1: char ch = 'A'; Line2: int x = ch; Line3: System.out.println(x); ``` Output: ``` java 65 ``` Explanation: ``` The ASCII value of 'A' is 65. So, when we try to store the value of ch into x, 65 is stored. Hence, Line 3 prints 65 in the output. ``` 2. **Char to int** gives error sometimes, so explicitly typecast Code 1: ``` java Line1: char ch = 'A' + 2; Line2: System.out.println(ch); ``` Output: ``` C ``` Explanation: ``` When we perform operation between char and int we get int. 65 + 2 gives 67 which is the ASCII value of 'C' Line 2 prints C in the output. ``` Code 2: ``` java Line1: char ch5 = 'A'; Line2: ch5 = ch5 + 32; Line3: System.out.println(ch5); ``` Output: ``` Error : possible lossy conversion from int to char ``` Explanation: ``` When we perform operation between char and int we get int. We are trying to store int to char and it gives error. ``` Code 3: ``` java Line1: char ch5 = 'A'; Line2: ch5 = (char)(ch5 + 32); Line3: System.out.println(ch5); ``` Output: ``` a ``` Explanation: ``` When we perform operation between char and int we get int. 65 + 32 gives 97 which is the ASCII value of 'a' Line 2 prints a in the output. ``` 3. We can use operators with char Code 1: ``` java Line1: char ch7 = 'B'; Line2: if(ch7 >= 90){ Line3: System.out.println("YES"); Line4: } Line5: else{ Line6: System.out.println("NO"); Line7: } ``` Output: ``` NO ``` Explanation: ``` The ASCII value of 'B' is 66. Since 66 is not greater than or equals to 90, the condition is false. So, else block is executed. Line 6 prints NO in the output. ``` Code 2: ``` java Line1: char ch1 = 'B'; Line2: char ch2 = 'a'; Line3: if(ch2 >= ch1){ Line4: System.out.println("YES"); Line5: } Line6: else{ Line7: System.out.println("NO"); Line8: } ``` Output: ``` YES ``` Explanation: ``` The ASCII value of 'B' is 66 and 'a' is 97. Since 97 is greater than or equals to 66, the condition is true. So, if block is executed. Line 4 prints YES in the output. ``` ## Character Array ### Syntax ``` char name[] = new char[size]; ``` Code: ``` java static void printChar(char data[]){ int N = data.length; // Index : 0 to N-1 for(int i=0;i<N;i++){ System.out.print(data[i] + " "); } System.out.println(); } public static void main(String args[]) { char ch[] = new char[5]; ch[0] = 'A'; ch[1] = 'B'; ch[2] = 67; ch[3] = 'D'; ch[4] = 'E'; printChar(ch); } ``` Output: ``` java A B C D E ``` Explanation: ``` An array of character is created of size 5. ch : [ 'A' | 'B' | 'C' | 'D' | 'E' ] 0 1 2 3 4 We print all the elments of the array using the function printChar() ``` ## String ### Syntax ``` String name = "value"; ``` Code: ``` java String str = "MonaLisa"; ``` Each of the characters have indexes starting from 0. ``` "MonaLisa" index: 01234567 ``` ### Properties 1. To get total number of characters present in string: **name.length()** Code: ``` java String str = "MonaLisa"; System.out.println(str.length()); ``` Output: ```java 8 ``` Explanation: ``` The given string has 8 characters. ``` 2. To access a character at any particular index: **name.charAt(index)** Code: ``` java String str = "MonaLisa"; System.out.println(str.charAt(4)); ``` Output: ```java L ``` Explanation: ``` The given string has 'L' at 4th index. ``` 3. We **cannot** update characters at particular indices. 4. We can append a character or String at the start or end of a String Code 1: ``` java String str = "Hell"; str = str + 'o'; System.out.println(str); ``` Output: ``` java Hello ``` Explanation: ``` Here, we are appending a character at the end. ``` Code 2: ``` java String str = "Hello"; str = "Guys " + str; System.out.println(str); ``` Output: ``` java Guys Hello ``` Explanation: ``` Here, we are appending a string at the start. ``` ## StringBuilder In order to update character at particular indices we use StringBuilder. ### Syntax ``` StringBuilder name = new StringBuilder(String_name); ``` Code: ``` java String str = "Hello"; StringBuilder sb = new StringBuilder(str); System.out.println(sb); ``` Output: ``` java Hello ``` Explanation: ``` StringBuilder sb is created having value "Hello" in it. ``` ### Functions Code: ``` java Line1: String str = "Hello"; Line2: StringBuilder sb = new StringBuilder(str); Line3: System.out.println(sb.length()); Line4: System.out.println(sb.charAt(1)); Line5: sb.setCharAt(1, 'x'); Line6: System.out.println(sb); Line7: System.out.println(sb.append("abc")); Line8: System.out.println(sb.append('w')); ``` Output: ``` java 5 e Hxllo Hxlloabc Hxlloabcw ``` Explanation: ``` name.length() -> gives total number of characters name.charAt(index) -> gives character present at that index name.setCharAt(index, character) -> helps us to update the character at particular index name.append(String) -> helps us add the string at the end of the stringBuilder name.append(character) -> helps us add a character at the end of stringBuilder ``` ***NOTE:*** Whenever you want to make changes in your String prefer using StringBuilder. ### Converting StringBuilder to String We can use **name.toString()** function to convert a StringBuilder into String. For eg. Write a code to change character at 1st index to 'x' in the given String. Code: ```java String str = "Hello"; StringBuilder sb = new StringBuilder(str); sb.setCharAt(1, 'x'); String ans = sb.toString(); System.out.println(ans); ``` Output: ``` java Hxllo ``` To know more details about StringBuilder refer this link: [Link](https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#setCharAt(int,%20char))