0% found this document useful (0 votes)
19 views2 pages

String Methods in Java Explained

The document provides an overview of various string methods in programming, including how to define, concatenate, count length, extract characters, compare, search, and manipulate strings. It includes examples for each method, demonstrating their usage and expected outputs. Additionally, it covers miscellaneous methods such as replace, trim, substring, and case conversion.

Uploaded by

yakshajain20
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

String Methods in Java Explained

The document provides an overview of various string methods in programming, including how to define, concatenate, count length, extract characters, compare, search, and manipulate strings. It includes examples for each method, demonstrating their usage and expected outputs. Additionally, it covers miscellaneous methods such as replace, trim, substring, and case conversion.

Uploaded by

yakshajain20
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String Methods

Methods Used for Defining a String


1. String s = new String(); // Blank String
2. String s = new String();
s = “hi”; //hi
3. String s = new String(“Hello”); //hello
4. char abc[] = {‘I’,’n’,’d’,’i’,’a’};
String s = new String(abc); //india
5. char abc[] = {‘H’,’i’,’n’,’d’,’u’,’s’,’t’,’a’,’n’};
String s = new String(abc, 3, 4); //dust
6. String s = new String (“Hello and Bye”);
String s2 = new String (s); //Hello and Bye
7. String s = new String(“I Love My India”);
String s2 = new String([Link](2,6)); //Love
8. String s = “India will Rule the World”;

Methods for Concatenation (Merging)


1. String s1 = “Sachin ”;
String s2 = “Tendulkar”;
String s3 = s1 + s2; //Sachin Tendulkar
2. String name = “Mohandas ” + “Karamchand ” + “Gandhi”;
3. String s = “Adolf ”;
String t = “Hitler ”;
String nameSurname = [Link](t); // Adolf Hitler
String surnameName = [Link](s); // Hitler Adolf
4. String all = “His age is “ + age + “ and his height is “ + height;
// His age is 19 and his height is 5.11

Methods for counting length of the String


1. String abc = “India is Great”;
int len = [Link](); // 14
2. int b = "Short String".length(); // 12

Methods for Extracting Characters


1. char ch;
ch = “abcdefghijklmnopqrstuvwxyz”.charAt(3); //d
2. String s = "Subashchandra Bose";
int len = [Link](); //Measuring length of the string
char abc[] = new char[len]; //Declaring array of size len
//Copying String to char array using charAt()
for(int i=0; i<len; ++i) {
//Copying character by character
abc[i] = [Link](i);
//Displaying char array
[Link](abc[i]); }//End of for loop

Methods for comparing Strings


1. String first = “India”;
String second = “INDIA”;
boolean b = [Link](second); // o/p -> false
boolean bool = [Link](second); // o/p -> true
Comparing two strings for sorting using compareTo()
1. String i = "INDIA";
String b = "BHARAT";
int x = [Link](i); // o/p -> -7
int y = [Link](b); // o/p -> 7

Methods of searching String


1. Searching for a character or a string using indexOf()
String abc = “We are the World”;
int a = [Link](‘W’); // o/p -> 0
int b = [Link](‘W’, 5); // o/p -> 11
int c = [Link](“the”); // o/p -> 7
2. Searching for a character or a string using lastIndexOf()
String xyz = “The Mahatma Gandhi was The Great Indian”;
int x = [Link](‘G’); // o/p -> 27
int y = [Link](‘G’, 20); // o/p -> 12
int z = [Link](“The”); // o/p -> 23
int p = [Link](“The”, 10); // o/p -> 0

Miscellneous Methods
1. replace() Method
String str = “Hello”.replace(‘l’,’w’) // o/p -> Hewwo
String abc = “We are the world”;
String xyz = [Link](“the world”,”indians”); //We are indians
2. trim() Method
String s = “ Hello World “;
String p = [Link]();
3. substring() Method
E.g. 1 : String w = “We Are Indians”;
String y = [Link](7); // o/p -> Indians
E.g.2 : String s1 = “I feel like GOD”;
String s2 = [Link](2,11); // o/p -> feel like
4. toUpperCase() Method
String s = “India Is Great”;
String upp = [Link](); //o/p -> INDIA IS GREAT
5. toLowerCase() Method
String s = “India Is Great”;
String low = [Link](); //o/p -> india is great

You might also like