Strings
Strings are an array of characters.
To declare a string
String str = "Sumanjeet";
Take input from user
String str = sc.next();
it will take only the first word as input.
String str = sc.nextLine();
it will take all words (complete line) as input.
Some important functions
charAt(i)
it will give the character at index i.
length()
it will give the length of the string (blank spaces are also counted)
indexOf(i)
it will give the first occurrence of letter i in the string.
compareTo()
it will give the difference between the ASCII value of the first non matching character. it can give the answer 0, positive integer & negative integers.
example if str1 = "Hello" and str2 = "Dello" then str1.compareTo(str2) will give 4 because the ASCII character of H is 72 and D is 68.
contains()
it will return true or false, we have to provide strings and it is case sensitive.
startsWith()
it will return true or false if the string starts with a provided string
endsWith()
it will return true or false if the string ends with a provided string
toUpperCase()
it will make every character of the string to upper-case
toLowerCase()
it will make every character of the string to lowercase
concat()
it concatenates one string to the end of another string and returns it. It does not change the original string. str1.concat(str2)
will return str1 + str2. we cannot change the existing string but we can create a new string and points the new string to old variable name.
its output will be abcxyzr10
, whatever we add to a string char, integer etc. will first get converted into a string and then it is added into old string.
"abc"+"xyz"+10+20
it will become abcxyz1020
10+20+"abc"+"xyz"
it will become 30abcxyz
this is because the operation happens from left to right.
substring()
it is the continuous part of a string.
substring(i,j)
it will return characters from the ith index to the j-1 index.
substring(i)
it will return characters from the ith index to the last index.
Internings and New
Interning means that if a string is present in the Heap memory then the new variable which is created with the same value will point to the same heap memory location, it will not create a new variable in the Heap memory. example String s1 = "Hello"
& String s2 = "Hello"
both will point to the same address in Heap memory.
If we want to create a new memory in Heap memory then we have to write String s = new String("Sumanjeet")
Immutability of Strings
Strings are immutable it means that we cannot change any characters or in other words, we can change the reference but not the exact string itself.It happens due to security reasons and Internings in strings.
str.charAt(1)='a'
it will give an error.
we have to use concepts of substrings to change strings basically it creates a new string and gives the reference of the new string to a variable.
str.equals() vs ==
the "==
" operator checks for the address of the strings and if the address is same it will return true and if the address are different it will return false, it will never compares the actual strings.
The str1.equals(str2)
operator will compare the strings character by character and if all the characters are same then it will return true otherwise false.
String Builders
StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates an immutable sequence of characters, the StringBuilder class provides an alternative to the String Class, as it creates a mutable sequence of characters.it can be initialized as
StringBuilder str = new StringBuilder("hello");
To take input from user
StringBuilder str = new StringBuilder(sc.nextLine);
Strings Builders inbuilt functions
setCharAt(index,char)
it will replace the character at "index" to "char".
append()
It can append char,int,float,string etc to the string.
insert(index,ch)
it will add the "ch" character at the provided index.
deleteCharAt(index)
it will delete the character at the provided index.
reverse()
it will reverse all the characters of the string.
delete(i,j)
it will delete characters from i to j-1.