A. Introduction
Strings are such an important type of data that we have to deal with almost in each program. So what do we usually do with strings?
B. Common string operations
String variables and literals
Concatenation: +
4+7+"Hi" vs "Hi"+4+7
String length and index: length()
a character of a string: charAt();
Substrings: substring()
Search for a substring: indexOf(), lastIndexOf(), contains()
compare strings: equals(), compareTo()
C. Try:
Starting: //convert variables to final values
str = "harry" n = 5 mystery = str.substring(0,1) + str.substring(4,5) Output: //hy Explanation: //notice that mystery is a string, which means it will concatenate. str.substring(0,1) is starting from h, and does not include a. So the first one is h. The next one is also pretty straightforward. Beginning index and ending index.
String str = "harry"; int n = str.length();//finds the length of str String mystery = str.substring(0,1) + str.substring(n - 1, n); System.out.println(mystery); Output: hy