Presented by Nishant Gupta
Presented by Nishant Gupta
Presented by Nishant Gupta
Nishant Gupta
• A linear sequence of symbols (characters
or words or phrases)
• A string is a data type used in
programming, such as an integer and
floating point unit.
• For Ex- “HCL is $5 Billion Enterprise”
STRING
Declaring Strings in Java
• String str=“HCL”(declaring by reference)
• String[] name={“Lokesh",“
Sunny",“Rosie",“Aarushe","deepak"};
(declaring explicitly)
String Manipulation
• It includes all the operations performed on
strings.
•String(String s)
Creates a new String object whose content is same as the String object passed
as an argument.
•String also provides constructors that take byte and char array as argument
and returns String object.
•String equality
String class overrides the equals() method of the Object class. It compares the
content of the two string object and returns the boolean value accordingly.
For example,
String str1=”Hello”;
String str2=”Hello”;
If(str1 == str2)
System.out.println(“Equal 1”);
Else
System.out.println(“Not Equal 1”);
•String Compare
Apart from these methods String class also provides compareTo methods.
int compareTo(String str2)
This method compares two Strings and returns an int value. It returns value 0, if
this string is equal to the string argument a value less than 0, if this string is
less than the string argument
a value greater than 0, if this string is greater than the string argument
Reading characters from String:
char charAt(index i)
Returns char at specified index. An index ranges from 0 to length() -1.
1. int indexOf(int c)
Returns the index of first occurrence of the argument char.
The replace method of String can be used to replace all occurrences of the specified
character with given character.
String replace(char oldChar, int newchar)
Eg.
L
ROCK
Getting substrings
String class provides substring method to extract specified portion of the given String.
This method has been overloaded.
String substring(int startIndex)
The code given below specifies the starting and the ending index of the string
String substring(int startIndex, int endIndex)
Eg. RockMan String SubString
The String class defines the length() method that determines the length of a string. The
length of a string is the number of characters contained in the string.
Str.length();
String str=“Hello”
System.out.println(str.length());
Output
5
String Concatenation using the + operator
The + operator is used to concatenate two strings, producing a new String object as the
result. For example,
This code will display the string "Our daily sale is 500 dollars".
Manipulating Character Case
The toUpperCase() method creates a new copy of a string after converting all the
lowercase letters in the invoking string to uppercase. The signature of the method is
given below:
String toUpperCase();
Eg JAVA
java
The toLowerCase() method creates a new copy of a string after converting all the
uppercase letters in the invoking string to lowercase. The signature of the method is
given below:
String toLowerCase() ;
Eg java
JAVA
Trimming
This method removes white space from the front and the end of a String.
String trim();
Eg “ Hello ”
“Hello” (After trimming)
Concatenation
This method returns a string with the value of string passed in to the method appended
to the end of String which used to invoke the method.
String.concat(str);