1 48.
Practising String Methods
2 1. You can get methods easily.
3 2. String in Java are immutable.
4 3. Below function does not affect the current passed string but return the
5 modified string.
6 4. When we assigned new string to older reference variable, the older string
7 before assignment is lost and collected by the garbage collection
8 mechanism.
9 5. int str.length(): It is used to get string of the number.
10 Example:
11 String str = “Shreyash”;
12 int length = str.length(str);
13 System.out.println(“length”);
14 O/P: 8
16 6. String str.toUpperCase(): It is used to get modified string to upper case.
17 Example:
18 String str = “Shreyash”;
19 String str1 = str.toUpperCase();
20 System.out.println(“str1”);
21 O/P: SHREYASH
23 7. String str.trim(): It is used to remove blank spaces(White Spaces).
24 Example:
25 String str = “ Shreyash ”;
26 System.out.println(“str1”);
27 String str1 = str.trim (str);
28 System.out.println(“str1”);
29 O/P 1: Shreyash
30 O/P 2: Shreyash
31 8. String str.substring(int begin): It is used get the substrings(cut out small
32 string from main string).
1 a. Example:
2 String str = “Shreyash”;
3 String str1 = str.substring(2);
4 System.out.println(“str1”);
5 O/P:reyash
6 b. Example for (Overloaded Function)
7 Syntax: str.substring(starting index, ending index);
8 *Ending index is not included.
9
10 String str = “Shreyash”;
11 String str1 = str.substring(2,5);
12 System.out.println(“str1”);
13 O/P: rey
14 9. String str.replace(char old, char new): It is used to replace a character with
15 any other character.
16 Example:
17 String str = “Shreyash”;
18 String str1 = str.replace (‘e’,’M’);
19 System.out.println(“str1”);
20 O/P:ShMyash
22
23 49. Practising String Methods #2
24 1. -1 means something is not found or invalid.
25 2. boolean str.startsWith(String s): It is used to check whether the string starts
26 with give string or a character.
27 It does take space or tab as parameter.
28 Example:
29 String name = ‘Shreyash’;
30 System.out.println(name.startsWith(‘Sh’));
31 O/P: true
1 3. boolean str.endsWith(String s): It is used to check whether the string ends
2 with given string or a character. Example:
3 String name = ‘Shreyash’;
4 System.out.println(name.endsWith (‘sh’));
5 O/P: true
7 4. char str.charAt(int index): It is used to get the character at the given index
8 number.
9 Example:
10 String name = ‘Shreyash’;
11 System.out.println(name.charAt (0));
12 O/P: S
13 5. int indexOf(String s): It is used to get the starting index of a given particular
14 character or of the given String.
15 Example:
16 String name = “Shreyash”;
17 System.out.println(name.indexOf(‘h’));
18 O/P: 1
19 6. int lastIndexOf(Strign s): It is used to get the ending index of a given
20 particular character or of the given String.
21 Example:
22 String name = “Shreyash”;
23 System.out.println(name.lastIndexOf(‘h’));
24 O/P: 7
25
26 50.Practising String Methods 2
27 1. Example:
28 class Two
29 {
30 public static void main(String[] args)
31 {
32 String str1 = "Mr. Shreyash Lokhande";
33 System.out.println(str1.startsWith("Mr"));
34 System.out.println(str1.endsWith("."));
35 System.out.println(str1.charAt(4));
1 System.out.println(str1.indexOf(".", 4));
2 System.out.println(str1.lastIndexOf("."));
3 }
4 }
5
6 51.String Methods #3
7 1. boolean equals(String s): It is used to compare two strings.
8 It compare only the literals(value).
9 *Unlike == compares references.
10 2. boolean equalsIgnoreCase(String s): It is used to equate strings without
11 considering case of the Strings.
12 Example:
13 str1.equalsIgnoreCase(str2);
14
15 3. int compareTo(String s): It is used to compare two strings in dictionary order.
16 It compares only the literal (value).
17 Example:
18 str1.compareTo(String s);
19 (Everything happens according to ASCII code)
20 * smaller = comes first.
21 * bigger = comes last.
22 * -1 means first string is smaller than second string.(context dictionary)
23 * 1 means second string is smaller than first string. (context dictionary)
24 * 0 means both string are same.
25 * Upper case of a string is smaller than Lower case of string.
26 * The equals() and == are used to compare two strings.
27 str1.equals(str2) means only string value is compared.
28 str1 == str2 means string value and data-type of the object is compared.
29 4. boolean contains(String s): It is used to check whether string contains the
30 given substring.
31 Example:
32 str = “Hello wall”;
33 System.out.println(str.contains(“all”));
1 O/P: true
2
3 5. String concat(String s) / ‘+’ : It is used to get two concatenated strings.
4 Example:
5
6 6. String valueOf(int i): It is used converting any value of any datatype into
7 String. This is a static method.
8
9 Example:
10 public class Three
11 {
12 public static void main(String[] args)
13 {
14 String str2="java";
15 String str1 = new String("java");
16 String str3="python";
17 String str4="Python";
18 System.out.println(str2.equals(str3));
19 System.out.println(str2.equalsIgnoreCase(str3));
20 System.out.println(str4.compareTo(str3));
21 System.out.println(str2.valueOf(21));
22
23 // reference
24 System.out.println(str1 == str2);
25 System.out.println(str2.equals(str1));
26
27 System.out.println(str1.valueOf(21).getClass());
28
29
30 }
31 }
32
33
34
35 51. Regular Expressions
36 1. Regular expressions are used for defining patterns for strings.
37 2. Boolean matches(String s):
38 3. Matching Symbols
39 Boolean matches(): It is used to find out patterns.
40
Regular Expression Description
. Any character
[abc] Exactly given letters. Between a to c
[abc][vc] EEnter firither first or second test. a to
c and v to z.
*Used for multiple symbols.
[^abc] Except abc
A|B A or B
XZ Exactly XZ (Exact string)
1
2 Meta Characters(Used for only one character):
3 \\d is used.
Regular Expression Description
\d Digits
\D Not Digits
\s Space
\S Not Space
\w Alphabets or digit
\w Neither alphabet or digit
4
5 54. String matching with regular expressions.
6 1. Quantifiers(To select one or multiple):
Regular Expression Description
* 0 or more time
+ One or more
? 0 or 1 time
{X} X times. (length of strings be X)
{X,Y} Between X and Y. (X = minimum and Y
= maximum *Range)
7
8 55. Student Challenge: String methods.
9 1. W.A.P to separate username and domain name from e-mail Id.
10 public class Six
11 {
12 public static void main(String[] args)
13 {
14 //useing methods of string class find out whether gmail id is upon gmail or not
15 with domain name and username.
16 String str = "“
[email protected]"”;
17 String userNamesername = str.substring(0,str.indexOf('‘@'’));
18 String domainName = str.substring(str.indexOf('‘@'’) +1, str.length());
19 System.out.println(userNamesername);
1 System.out.println(domainName);
2
3 if(domainName.matches("“gmail"”))
4 {
5 System.out.println("“Yes it is a gmail."”);
6 }
7 }
8 }
9
10 56. Student Challenge: Regular Expressions #1
11 Program: Write Regular for the following.
12 1. Check whether given number is a binary number or not.
13 2. Check whether the number or string in the form of number is a hexa-
14 decimal or not.
15 3. Check whether the date is in the Date format (dd/mm/yyyy) or not.
16 4. import java.util.*;
17 class Seven
18 {
19 public static void main(String[] args)
20 {
21 Scanner sc = new Scanner(System.in);
22 System.out.println("Enter your number:");
23 String number = "0110";
24 String number1 = "AB873C";
25 String dateFormat = "13/12/2004";
26
27 System.out.println(number.matches("[01]+"));
28 System.out.println(number1.matches("([A-F0-9])*")); // Done it in ([A-Z]|
29 [0-9])+ way.
30 System.out.println(dateFormat.matches("[0-3][0-9]/[01][0-9]/[0-9]")); //
31 year we have done it in a way [0-9][0-9][0-9][0-9].
32 }
33 }
34
35 *A way to convert integer into a string.
36 int b = 10110001;
37 String str = b+””;
38 Or
39 int b = 10110001;
40 String str = String.valueOf(b);
41
42 57. Student Challenge: Regular Expression #2
43
44 1. W.A.P
1 a. Remove Special characters from a string.
2 b. Remove extra spaces from string.
3 c. Find number of words in a String
4 2. String replaceAll(String s): It is used to replace string with a sub-string of
5 a give string.
6 3. Split(String s): It is used to split the string according to the string and return
7 the array.
8
9 *Notes:
10 1. We can’t use meta characters in normal String functions except matches().
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1
10