0% found this document useful (0 votes)
4 views12 pages

String Handling

The document provides an overview of string handling in Java, explaining that strings are a series of characters and are immutable. It details various methods for string operations, such as length(), concatenation, charAt(), and substring(), along with examples for each method. Additionally, it covers string modification techniques like trim(), replace(), and sorting arrays of strings.

Uploaded by

naikbhavani431
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)
4 views12 pages

String Handling

The document provides an overview of string handling in Java, explaining that strings are a series of characters and are immutable. It details various methods for string operations, such as length(), concatenation, charAt(), and substring(), along with examples for each method. Additionally, it covers string modification techniques like trim(), replace(), and sorting arrays of strings.

Uploaded by

naikbhavani431
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 HANDLING

What is a String?
String is a series of characters . Strings are constant and
their values cannot be changed after they are created but
string objects can be shared.

In java, String is a class and not a primitive data type.

String Declaration:-
String StringName;
StringName= new String(“Hello”);
Methods Used in String Operations:

1)length():- A string is a series of characters and its


length is the number of characters it contains. To
obtain the length of a string, length() method is
used.

public class Main {


public static void main(String[] args)
{
String txt = “COMPUTER APPLICATIONS";
[Link]([Link]());
}}

Length of the string is 21


[Link] of String and + Operator

The + operator is used to concatenate two strings in Java.


String s= “ter”;
[Link](“Compu” +s + “Applications”);

The String is ComputerApplications.

Extracting characters from String

charAt():- The charAt() method returns the character at a


specified index. An index range from 0 to total length minus 1.

char charAt(int position)


for ex:- char a;
a= “ritiprateek”.charAt(3)
In the above example, the value “t” will be assigned to “a”.
3. getchars()

The getchars() method returns more than one character at a time.

getchars(int stringstart, int stringend, char target[],int targetStart)

public class Main {


public static void main(String[] args) {
char[] myArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
[Link](myArray);

String myStr = "Hello World";


[Link](7, 12, myArray, 4);//(beginning,end,char target[],start)
[Link](myArray);
}
}
The Output is 0123456789
New output is 0123World9
[Link]()

The getbytes() method stores characters in a array of


bytes. The getbytes method converts the string into
bytes according to the specified character encoding and
storing the result into a new array.

public class Main {


public static void main(String[] args) {
String myStr = "Goal";
byte[] result = [Link]();
[Link](result[2]);
}}

Output is:- 97
[Link]()

Converts a string array to char.


public class Main {
public static void main(String[] args) {
String myStr = "Hello";
char[] myArray = [Link]();
[Link](myArray[3]); }} //Output is L

String Modification
[Link] substring(int startindex,int endindex)
The substring() method returns a new string which is a substring of the
string
public class Main {
public static void main(String[] args) {
String myStr = “Government";
[Link]([Link](3, 6));
}} Output is :- ern
[Link]():-
The concat () method concatenates the specified string to the end of
the given string.
public class Main {
public static void main(String[] args) {
String firstName = "John ";
String lastName = "Doe";
[Link]([Link](lastName));
}}

3. trim():- The trim() method trims white spaces from both the ends
of a string.
public class Main {
public static void main(String[] args) {
String myStr = " Hello World! ";
[Link](myStr);
[Link]([Link]());
}}
[Link]() and Uppercase():-

public class Main {


public static void main(String[] args) {
String txt = "Hello World";
[Link]([Link]());
[Link]([Link]());
}}

[Link]():- It replaces all occurences of one character in the


specified string with another character.

public class Main {


public static void main(String[] args) {
String myStr = "Hello";
[Link]([Link]('l', 'p'));
}}
Output:- Heppo
[Link]():-This method inserts one string into another.
class StringInsert {
public void main()
{
StringBuffer str= new StringBuffer(“I am solving problems”);
[Link](5,”learning Java and “);
[Link](str);
}}

7. reverse():- This method is used to reverse a string.

class Ever {
public void main() {
StringBuffer str= new StringBuffer(“PRATIEK MALHOTRA”);
[Link](“Original String=“+str);
[Link]();
[Link](“Reversed String=“ +str);
}}
Original String=PRATIEK MALHOTRA
Reversed String=ARTOHLAM KEITARP

[Link]():- Comparing two strings,equals() method of String class is used.

public class Main {


public static void main(String[] args) {
String myStr1 = "Akshay";
String myStr2 = "Hello";
String myStr3 = "Akshay";
[Link]([Link](myStr2));
[Link]([Link](myStr3));
}}

Output is:- false


true
import [Link];
class SortDaysOfWeek {
public static void main(String[] args) {
String[] daysOfTheWeek = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday“ };

[Link]("Original order:");
for (String day : daysOfTheWeek) {
[Link](day + " "); }
[Link]();
// Sort the array in dictionary order
[Link](daysOfTheWeek);

[Link]("Sorted order (dictionary):");


for (String day : daysOfTheWeek) {
[Link](day + " ");}
[Link]();
}}
String valueOf()

public class Main {


public static void main(String[] args) {
char[] myArray = {'a', 'b', 'c'};
[Link]([Link](myArray));
[Link]([Link]('A'));
[Link]([Link](true));
[Link]([Link](4.5f));
[Link]([Link](5.2));
[Link]([Link](12));
[Link]([Link](1400L));
}}

Output is :- abc, A, true, 4.5, 5.2 ,12, 1400

You might also like