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

module1 String handling-AJ-BIS402

The document provides an overview of string handling in Java, detailing various constructors of the String class and their usage. It includes examples of creating strings from character arrays, byte arrays, and other strings, as well as demonstrating special string operations like substring and character replacement. Additionally, it compares StringBuffer and StringBuilder regarding their synchronization and performance in multithreaded environments.

Uploaded by

sujay15042005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

module1 String handling-AJ-BIS402

The document provides an overview of string handling in Java, detailing various constructors of the String class and their usage. It includes examples of creating strings from character arrays, byte arrays, and other strings, as well as demonstrating special string operations like substring and character replacement. Additionally, it compares StringBuffer and StringBuilder regarding their synchronization and performance in multithreaded environments.

Uploaded by

sujay15042005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Module -1

Strings in java
String class constructors

-- To create and initialize string objects based on the various types of arguments.
String(): default constructor used to create an empty string
Syntax: String s = new String();
It will create a string object in the heap area with no value.
String(String str): to create a string initialized by an array of characters
Syntax : String st = new String(String str);
Example:
String s2 = new String("Hello Java");
String(char chs[ ])
--Creates a string object and stores the array of characters in it.
Syntax:
String str = new String(char ch[])
Example:
char ch [] = { 'a', 'b', 'c', 'd' };
String s3 = new String(ch);

String(char ch[ ], int startIndex, int count)


-- Creates and initializes a string object with a subrange of a character array.
--startIndex specifies the index at which the subrange begins
--count specifies the number of characters to be copied.
Syntax:-
String str = new String(char chs[ ], int startIndex, int count);
Example:
Output-?
char chs[] = { 'w', 'i', 'n', 'd', 'o', 'w', 's' };
String str = new String(chs, 2, 3);

String s = new String(chs, 0,4);


Output-?
System.out.println(s);

String(String Stringobj)
-- Create a string object that contains the same characters sequence as another string object.
char chars[] = { 'F', 'A', 'N' };
String s1 = new String(chars);

String s2 = new String(s1); Output: ?


System.out.println(s1);
System.out.println(s2);
-- String class provides constructor that initialize a string when given a byte array
String(byte byteArr[ ])
String(byte byteArr[ ], int startIndex, int count)
--This type of string constructor constructs a new string object by decoding the given array
of bytes (i.e., by decoding ASCII values into the characters) according to the system’s default
character set.
Example 1
byte b[] = { 97, 98, 99, 100 }; // Range of bytes: -128 to 127. These byte values will be converted into corresponding characters.
String s = new String(b); Example 2
System.out.println(s);
byte b[] = { 65, 66, 67, 68, 69, 70 };
Key points: String s = new String(b, 2, 4);
97 is the Unicode value of a, 98 ➨ b, 99 ➨ c, 100 ➨ d. System.out.println(s);
65 is the Unicode value of A.
Special String Operations
String
Example
Example
// Java program to demonstrate the working of toCharArray() method
OUTPUT

// Driver Class
class C_ARRAY { G
// main function O
public static void main(String args[]) G
{ R
String s = "GOGREEN"; E
char[] gfg = s.toCharArray(); E
N
for (int i = 0; i < gfg.length; i++) {
System.out.println(gfg[i]);
}
}
}
Example
Example

public class Example


{ Output
public static void main(String[] args) ?
{
String S1 = new String("This is an example");
String S2 = new String("isan");
System. out. print("Result of comparing S1 with S2: ");
System. out. println(S1. regionMatches(true, 2, S2, 0, 2));
System. out. println(S1. regionMatches(true, 5, S2, 0, 2));
System. out. println(S1. regionMatches(true, 15, S2, 0, 2));
}
}
startsWith()
Example

Replace Character in a String at Specific Index


Output
??
int index=6;

String str=“Sunny gor the Day";

System.out.println(str.substring(0, index)+"F"+str.substring(index+1));
lastIndexOf(int
ch) str = "012301230123";
String lastIndexOf(String
str.lastIndexOf('0') str)
8 String str = "Hello Jello Trello";
str.lastIndexOf(48)
8 str.lastIndexOf("llo")
str.lastIndexOf('a') 15
-1
lastIndexOf(int ch, int fromIndex) str.lastIndexOf("Jello")
String str = "Hello World"; 6
str.lastIndexOf('l', 7)
3 str.lastIndexOf("123")
str.lastIndexOf('l', 50) -1
9
str.lastIndexOf('l', -5)
-1
str.lastIndexOf('l', 1)
-1
String Str = new String("Welcome to green
palace"); Output
Green palace
System.out.print("The extracted substring green
is : ");
System.out.println(Str.substring(10));
System.out.println(Str.substring(10,16));
class ValueOfExa {
public static void main(String arg[])
{
int iNum = 30;

double fNum = 4.56789;


String s = "91";

// Returns the string representation of int iNum.


String sample = String.valueOf(iNum); Output

System.out.println(sample); 30
3091
// concatenating string with iNum
System.out.println(sample + s); 4.56789
914.56789
// Returns the string representation of the float
// fnum.
sample = String.valueOf(fNum);
System.out.println(sample);

// concatenating string with fNum


System.out.println(s + sample);
}}
Example
The ensureCapacity() method
-- StringBuffer class ensures the capacity to at least equal to the specified
minimumCapacity.
-- Using the ensureCapacity() method, we are trying to check whether the new
capacity is equal to the minimum capacity or not. Since the current capacity is less
than the argument, then the method will generate the new capacity which is twice
the old capacity and plus 2.

public void ensureCapacity(int minimumCapacity)


This method takes one parameter minimumCapacity which is the minimum desired capacity.
Return Value: This method do not return anything.
. Write a java program to demonstrate capacity () of string Buffer class
import java.io.*;

class A {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // default 16
sb.append("Hello");
System.out.println(sb.capacity()); // now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());
// Now (16*2)+2=34 i.e (oldcapacity*2)+2
} }
Character
Example
Output
a=42!
StringBuffer is synchronized, meaning its methods are thread-safe and can be
safely used in a multithreaded environment.
StringBuilder is not synchronized, which makes it faster than StringBuffer, but it is
not thread-safe and should not be used in a multithreaded environment.
End of the I Module

Queries
???...

You might also like