[go: up one dir, main page]

0% found this document useful (0 votes)
14 views7 pages

String Methods

The document provides an overview of several Java String methods: Join(), Repeat(), Strip(), and Intern(). Each method is described with its purpose, parameters, and examples of usage. The document also explains the concept of the String Pool and how the intern() method optimizes memory usage by sharing references for identical strings.

Uploaded by

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

String Methods

The document provides an overview of several Java String methods: Join(), Repeat(), Strip(), and Intern(). Each method is described with its purpose, parameters, and examples of usage. The document also explains the concept of the String Pool and how the intern() method optimizes memory usage by sharing references for identical strings.

Uploaded by

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

Electrical Engineering Department

1cz
1st Communication and Computer
Engineering

String
Team Members Methods
 Ahmed Hamdy
 Hossam Mostafa
 Saeed Ali
 Sayed Aymen

1.Join () Method
Supervised By: Dr Sabah
In Java, the String.Join() method is a
Saad
convenient way to concatenate multiple strings
(or elements of a collection) with a specified
delimiter.
 delimiter: The character or string used to
separate the elements (e.g., ", ", "-", " ").
 elements: The strings or collection of strings
to be joined.
 Examples:
1. Multiple Elements:

2.Single Element:

3.Empty Delimiter:

4.No Elements:
2.Repeat () Method
In Java, the repeat () method is a convenient
way to create a new string by repeating an
existing string a specified number of times.
 count: The number of times the string should
be repeated. It must be a non-negative integer
(count >= 0).
 Examples:
1.Basic Usage:

2.Repeating a Single Character:

3.Repeating an Empty String:

4.Repeating Zero Times:


3.Strip () Method
In Java, strip () method is used to
remove whitespace from both the beginning
(leading) and end (trailing) of a string. The strip
() method is like the older trim () method but is
more powerful because it handles a wider range
of whitespace characters, including Unicode
whitespace.
 Examples:
1.Basic Usage:

2. Removing Tabs and Newlines:

3.No Whitespace to Remove:


4.Unicode Whitespace:

4.Intern () Method
In Java, the intern () method is used to ensure
that strings with the same content share the
same memory location in the String Pool. It is
part of the String class and is particularly useful
for optimizing memory usage when dealing with
many strings.
What is the String Pool?
 The String Pool is a special area in memory
where Java stores unique string literals
(e.g., "hello").
 If you create a string using double quotes
(e.g., String s = "hello";), Java automatically
puts it in the String Pool.
 If you create a string using new (e.g., String s
= new String("hello");), it creates a new object
in memory, even if the same string already
exists in the pool.
What Does an intern () Do?
 When you call intern () on a string, Java
checks if the same content already exists in
the String Pool.
 If it does, Java returns the reference to the
existing string in the pool.
 If it doesn't, Java adds the string to the pool
and returns its reference.
 After calling intern (), two strings with the
same content will point to the same memory
location.

You might also like