0% found this document useful (0 votes)
57 views

String Inbuilt Methods

The document contains code snippets demonstrating various inbuilt string methods in Java like substring(), replace(), startsWith(), contains(), endsWith(), equalsIgnoreCase(), indexOf(), trim(), toCharArray(), format(), intern(), isEmpty(), join(), valueOf(), lastIndexOf() for the String class. It also contains examples showing string builder methods like insert(), delete(), deleteCharAt(), replace() for manipulating strings.

Uploaded by

Jhagan A. M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

String Inbuilt Methods

The document contains code snippets demonstrating various inbuilt string methods in Java like substring(), replace(), startsWith(), contains(), endsWith(), equalsIgnoreCase(), indexOf(), trim(), toCharArray(), format(), intern(), isEmpty(), join(), valueOf(), lastIndexOf() for the String class. It also contains examples showing string builder methods like insert(), delete(), deleteCharAt(), replace() for manipulating strings.

Uploaded by

Jhagan A. M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Strings Inbuilt Methods

1 // Predict the output


2
3 public class TestSubstring
4 {
5 public static void main(String args[]){
6 String s = "Hardik Pandya";
7 System.out.println(s.substring(7));
8 System.out.println(s.substring(0,7));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class ReplaceExample1
4 {
5 public static void main(String args[]){
6 String s1 = "Negative thoughts";
7 String replaceString = s1.replace('e','a');
8 System.out.println(replaceString);
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class ReplaceExample1
4 {
5 public static void main(String args[]){
6 String s1 = "Negative thoughts";
7 String replaceString = s1.replace("Negative","Positive");
8 System.out.println(replaceString);
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StartsWithExample{
4 public static void main(String args[]){
5 String s1 = "You must be the change you wish to see in the world";
6 System.out.println(s1.startsWith("Y"));
7 System.out.println(s1.startsWith("You must"));
8 System.out.println(s1.startsWith("a"));
9 System.out.println(s1.startsWith("o", 1));
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class ContainsExampleMain1 {
4 public static void main(String args[]){
5 String name = "what do you know about me";
6 System.out.println(name.contains("do you know"));
7 System.out.println(name.contains("about"));
8 System.out.println(name.contains("hello"));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class ContainsExampleMain3 {
4 public static void main(String[] args) {
5 String str = "To learn Java visit focusacademy.in";
6 if(str.contains("focusAcademy.in.com")) {
7 System.out.println("This string contains focusacademy.in");
8 }
9 else
10 System.out.println("Result not found");
11 }
12 }
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class EndsWithExample{
4 public static void main(String args[]){
5 String s1 = "Beauty is in the eye of the beholder";
6 System.out.println(s1.endsWith("r"));
7 System.out.println(s1.endsWith("holder"));
8 System.out.println(s1.endsWith("eye"));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class EqualsIgnoreCaseExample{
4 public static void main(String args[]){
5 String s1 = "The walking Dead";
6 String s2 = "The walking Dead";
7 String s3 = "THE WALKING DEAD";
8 String s4 = "The WEST WEEDS";
9 System.out.println(s1.equalsIgnoreCase(s2));
10 System.out.println(s1.equalsIgnoreCase(s3));
11 System.out.println(s1.equalsIgnoreCase(s4));
12 }
13 }
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class IndexOfExample{
4 public static void main(String args[]){
5 String s1 = "This is the world";
6 int index4 = s1.indexOf('s');
7 System.out.println(index4);
8 int index1 = s1.indexOf("is");
9 int index2 = s1.indexOf("world");
10 System.out.println(index1);
11 System.out.println(index2);
12 int index3 = s1.indexOf("is",4);
13 System.out.println(index3);
14 }
15 }
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class IndexOfExample2 {
4 public static void main(String[] args) {
5 String s1 = "This is the example";
6 int index = s1.indexOf("example", 10);
7 System.out.println(index);
8 index = s1.indexOf("example", 20);
9 System.out.println(index);
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class IndexOfExample3 {
4 public static void main(String[] args) {
5 String s1 = "This is indexOf method";
6 int index = s1.indexOf('O', 12);
7 System.out.println(index);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringTrimExample{
4 public static void main(String args[]){
5 String s1 = " Game of ";
6 System.out.println(s1 + "thrones");
7 System.out.println(s1.trim() + "thrones");
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringToCharArrayExample{
4 public static void main(String args[]){
5 String s1= "Twilight Saga";
6 char[] ch = s1.toCharArray();
7 for(int i = 0; i < ch.length; i++){
8 System.out.print(ch[i]);
9 }
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringToCharArrayExample2 {
4 public static void main(String[] args) {
5 String s1 = "Welcome to Jumanji";
6 char[] ch = s1.toCharArray();
7 int len = ch.length;
8 System.out.println(len);
9 for (int i = 0; i < len; i++) {
10 System.out.print(ch[i]);
11 }
12 }
13 }
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class FormatExample{
4 public static void main(String args[]){
5 String name = "CSK";
6 String sf1 = String.format("%s",name);
7 String sf2 = String.format("%f",32.33434);
8 String sf3 = String.format("%16.12f",32.33434);
9 System.out.println(sf1);
10 System.out.println(sf2);
11 System.out.println(sf3);
12 }
13 }
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class InternExample{
4 public static void main(String args[]){
5 String s1 = new String("Dravid");
6 String s2 = "Dravid";
7 String s3 = s1.intern();
8 System.out.println(s1 == s2);
9 System.out.println(s2 == s3);
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class InternExample2 {
4 public static void main(String[] args) {
5 String s1 = "Game starts";
6 String s2 = s1.intern();
7 String s3 = new String("Game starts");
8 String s4 = s3.intern();
9 System.out.println(s1 == s2);
10 System.out.println(s1 == s3);
11 System.out.println(s1 == s4);
12 System.out.println(s2 == s3);
13 System.out.println(s2 == s4);
14 System.out.println(s3 == s4);
15 }
16 }
17
18
19
20
21
22
1 // Predict the output
2
3 public class IsEmptyExample{
4 public static void main(String args[]){
5 String s1 = "";
6 String s2 = "java";
7 System.out.println(s1.isEmpty());
8 System.out.println(s2.isEmpty());
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringJoinExample{
4 public static void main(String args[]){
5 String joinString1=String.join("","welcome","to","jurassic","world");
6 System.out.println(joinString1);
7 }
8 }
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringValueOfExample{
4 public static void main(String args[]){
5 int value = 30;
6 String s1 = String.valueOf(value);
7 System.out.println(s1 + 10);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringValueOfExample{
4 public static void main(String[] args) {
5 float f = 10.05645f;
6 double d = 10.02;
7 String s1 = String.valueOf(f);
8 String s2 = String.valueOf(d);
9 System.out.println(s1);
10 System.out.println(s2);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class LastIndexOfExample{
4 public static void main(String args[]){
5 String s1 = "this is the world";
6 int index1 = s1.lastIndexOf('s');
7 System.out.println(index1);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class LastIndexOfExample{
4 public static void main(String args[]){
5 String s1 = "this is the world";
6 int index1 = s1.lastIndexOf('s', 5);
7 System.out.println(index1);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class LastIndexOfExample{
4 public static void main(String[] args) {
5 String str = "This is last index of example";
6 int index = str.lastIndexOf("last");
7 System.out.println(index);
8 index = str.lastIndexOf("of", 25);
9 System.out.println(index);
10 index = str.lastIndexOf("of", 10);
11 System.out.println(index);
12 }
13 }
14
15
16
17
18
19
20
21
22
StringBuilder Inbuilt Methods
1 // Predict the output
2
3 public class StringBuilderExample{
4 public static void main(String args[]){
5 StringBuilder sb = new StringBuilder("Rhit man");
6 sb.insert(1,"Sharma");
7 System.out.println(sb);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringBuilderExample{
4 public static void main(String args[]){
5 StringBuilder sb = new StringBuilder("Ravindra Jadeja");
6 sb.delete(4,8);
7 System.out.println(sb);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringBuilderExample{
4 public static void main(String args[]){
5 StringBuilder sb = new StringBuilder("Ravindraa Jadeja");
6 sb.deleteCharAt(7);
7 System.out.println(sb);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class StringBuilderExample{
4 public static void main(String args[]){
5 StringBuilder sb = new StringBuilder("Mark");
6 sb.replace(1, 3, "Zukerberg");
7 System.out.println(sb);
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 public class Main{
4 public static void main(String[] args) {
5 StringBuffer s = new StringBuffer("Programmer");
6 s.insert(5, "xyz");
7 System.out.println(s);
8 s.insert(0, 5);
9 System.out.println(s);
10 s.insert(3, true);
11 System.out.println(s);
12 s.insert(5, 41.35d);
13 System.out.println(s);
14 s.insert(8, 41.35f);
15 System.out.println(s);
16 char arr[] = {'h','e','l','l','o'};
17 s.insert(2, arr);
18 System.out.println(s);
19 }
20 }
21
22
Rapid Fire
1 // Predict the output
2
3 public class Main{
4 public static void main (String[] args) {
5 String str1 = "Great";
6 String str2 = " job";
7 String str = "joy";
8 if(str1.equals(str2))
9 {
10 str1.concat(str2);
11 }
12 else{
13 str = str2.trim();
14 }
15 System.out.println(str1.toUpperCase());
16 System.out.println(str);
17 }
18 }
19
20
21
22
Question 1
A) GREAT
joy

B) GREAT joy

C) GREAT job

D) GREAT
job
1 // Predict the output
2
3 public class Main{
4 public static void main(String args[])
5 {
6 String str1 = "Great things never come from comfort zones";
7 if (str1.contains("Comfort"))
8 {
9 System.out.println("mountain");
10 }
11 else
12 {
13 System.out.println("peak");
14 }
15 }
16 }
17
18
19
20
21
22
Question 2
A) mountain

B) peak

C) Error

D) None of the mentioned


1 // Predict the output
2
3 public class Main{
4 public static void main(String[] args)
5 {
6 StringBuffer sb1 = new StringBuffer("Spider man");
7 StringBuffer sb2 = new StringBuffer("Spider man");
8 System.out.println(sb1.equals(sb2));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 3
A) true

B) false

C) Error

D) None of the mentioned


1 // Predict the output
2
3 public class Main{
4 public static void main(String[] args)
5 {
6 String s1 = "Force";
7 String s2 = "Force";
8 System.out.println(s1.equals(s2));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 4
A) true

B) false

C) Error

D) None of the mentioned


1 // Predict the output
2
3 public class Main{
4 public static void main(String args[])
5 {
6 String str = "Fire";
7 StringBuilder sb = new StringBuilder("Fire");
8 System.out.print(str.equals(sb) + " " + sb.equals(str));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 5
A) true false

B) false true

C) true true

D) false false
1 // Predict the output
2
3 public class Main{
4 public static void main(String[] args) {
5 String s1 = "Rare";
6 StringBuffer sb1 = new StringBuffer("Rare");
7 String s2 = sb1.toString();
8 System.out.println(s1.equals(s2));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 6
A) true

B) false

C) Error

D) None of the mentioned


1 // Predict the output
2
3 public class Main{
4 public static void main(String args[])
5 {
6 String s1 = "I want to become a full stack developer";
7 System.out.println(s1.replace('a','e'));
8 System.out.println(s1.charAt(7));
9 System.out.println(s1.startsWith("i"));
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
Question 7
A) I went to become a full stack developer
t
false
B) I went to become a full stack developer
t
true
C) I went to become e full steck developer
t
true
D) I went to become e full steck developer
t
false
1 // Predict the output
2
3 public class Main{
4 public static void main (String[] args)
5 {
6 StringBuilder sb = new StringBuilder("Welcome");
7 sb.append(" to programming..");
8 sb.setCharAt(6, 'Z');
9 System.out.print(sb);
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
Question 8
A) Welcome to programming..

B) WelcomZ to programming..

C) WelcomZ

D) Error
1 // Predict the output
2
3 public class output{
4 public static void main(String args[])
5 {
6 StringBuffer s1 = new StringBuffer("Microsoft");
7 s1.insert(9 , " Employee");
8 System.out.println(s1);
9 s1.delete(0, 10);
10 System.out.println(s1);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
Question 9
A) Microsoft Employee
Employee

B) Microsoft Employee
Employee

C) MicrosEmployee ft
Employee

D) Error
1 // Predict the output
2
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 StringBuilder str = new StringBuilder("Keep Going");
8 String str2 = str.toString();
9 StringBuilder s = str2.deleteCharAt(8);
10 System.out.println(s);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
Question 10
A) Keep Goig

B) Keep Goin

C) Keep Gong

D) Error
THANK YOU

You might also like