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

String Final

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

String Final

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

String class and methods

String:
• It represents a sequence of characters enclose in a double
quotes(“ “).
• In java it is not represented using character array.they are
represented using classes string.
• A java String ia an instantiated object of a String class. It
is not a character array.
• Syntax:
• String string_name = new String(“string value”);
• E.g
• String city = new String(“Aurangabad”);
• String class method:

• A string class defines a number of methods that allows us to


accomplish a variety of string manipulation task.

• length(): it is used to calculate length of a string.i.e number of


character present in the string.

• Syntax : int variable = String_name . length();

String nm = new String(“java”);

Int size = nm.lenght();

System.out.println(“length of string=“+size);

o/p: length of string = 4


• toLowerCase(): it is used to convert all characters in a string to
lowercase. Non alphabetical characters such as digits & special
symbols are not affected.

• Syntax : s2 = s1.toLowerCase();

• e.g. String s1 = new String (“JAVA”);

• String s2 = s1.toLowerCase();

• System.out.println(s2);

• o/p : java
• toUpperCase(): it is used to convert all characters in a string to
uppercase. Non alphabetical characters such as digits & special
symbols are not affected.

• Syntax : S2 = s1.toUpperCase();

• e.g. String s1 = new String (“java”);

• String s2 = s1.toUpperCase();

• System.out.println(s2);

• o/p JAVA
• replace() : it replaces all of one character with another
character.

• Syntax: Stringname.replace(‘source_char’,’dest_char’);

• String name = new String(“TALL”);

• String temp = name.replace(‘T’,’B’);

• System.out.println(temp);

• o/p BALL
• charAt() :

• It is used to extract a single character from a string by


specifying its index.

• Syntax :

• V= String_name.charAt(position)

• Where v is character variable.

• Position = integer value

• E.g String s = new String(“abc”);

• Char ch = s1.charAt(1);

• System.out.println(ch); // output : b
• Concat() :

• It is used to concatenate two strings. The second string is


appended at the end of first string.

• String s1 = new String(“Good”);

• String s2 = s1.concat(“Afternoon”);

• System.out.println(s2);

• Output : Good Afternoon


• substring()

• The java string substring() method returns a part of the string.

• We pass begin index and end index number position in the java substring method.

• start index starts from 0 whereas end index starts from 1.

• There are two types of substring methods in java string.

• Syntax :

• public String substring(int startIndex)

• and

• public String substring(int startIndex, int endIndex)


public class SubstringExample2 {

public static void main(String[] args) {

String s1="Javatpoint";

String substr = s1.substring(0); // Starts with 0 and goes to end

System.out.println(substr);

String substr2 = s1.substring(5,10); // Starts from 5 and goes to 10

System.out.println(substr2);

String substr3 = s1.substring(5,15); // Returns Exception

Output:

Javatpoint

point

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 5, end 15, length 10


• equals(): The java string equals() method compares the two given
strings based on the content of the string.

• If any character is not matched, it returns false. If all characters are


matched, it returns true.

• true if characters of both strings are equal otherwise false.

public class EqualsExample{


public static void main(String args[]){
String s1="javatpoint";
String s2="javatpoint";
String s3="JAVATPOINT";
String s4="python";
System.out.println(s1.equals(s2));//true because content and case is same
System.out.println(s1.equals(s3));//false because case is not same
System.out.println(s1.equals(s4));//false because content is not same
}}
• equalsIgnoreCase()

• The String equalsIgnoreCase() method compares

the two given strings on the basis of content of the

string irrespective of case of the string.

• It is like equals() method but doesn't check case. If

any character is not matched, it returns false

otherwise it returns true.


public class EqualsIgnoreCaseExample{

public static void main(String args[]){

String s1="javatpoint";

String s2="javatpoint";

You might also like