Java Strings

In Java a string is a sequence of characters.But unlike many other programming languages that implements strings as character arrays,Java implements strings as objects of type String.


In java once a String object has been created you can not change the characters that comprise that String.But you can still perform all types of String operations. The key difference is that you need a modification version of the original String each time when you have to perform operations.The original String is left unchanged.


The String class is defined in java.lang package.Thus, this is available to all programs automatically.

Creation of String:There are two main ways to create String in java.

String literals

This is the simplest and most common way to create objects. You create them by enclosing text in double quotes.
Example: String name= “ApnaStudy is best website for java”

Using the new keyword with String constructor:

Less common but offers more control over String creation. This is useful for creating strings from data sources other than string literals.

Example:
char[] chars={‘H’,‘e’, ‘l’,‘l’,‘o’};
String greeting= new String (chars);
System.out.println(greeting);


/*OUTPUT*/

Hello

Java Program to demonstrate every string methods

#toUpperCase()


/*OUTPUT*/

APNASTUDY IS THE BEST WEBSITE TO LEARN JAVA

Java Program to demonstrate every string methods

#toLowerCase


/*OUTPUT*/

apnastudy is the best website to learn java

#isEmpty()

An empty string has a length of zero and contains no characters. It's represented by "" (double quotes with nothing inside).


A string with whitespace characters has a length greater than zero (depending on the number of spaces) and contains characters, even though they are not visible characters. It's represented by something like " " (space between quotes).




/*OUTPUT*/

false
false
true

#isBlank()


/*OUTPUT*/

false
true
true

#charAt()


/*OUTPUT*/

i
t

#getChars(Start,End, target[] , targetStart[])


/*OUTPUT*/

dy is t

#indexOf()


/*OUTPUT*/

10
15
17
-1

#lastIndexOf()


/*OUTPUT*/

26
17
-1

#concat(String str)


/*OUTPUT*/

ApnaStudy is the best website to learn java Join us now
ApnaStudy is the best website to learn java!

#length

Length is a property,not a method you don’t need parentheses after length.


/*OUTPUT*/

43
11

#capacity()

The concept of capacity applies more to string builders (StringBuilder and StringBuffer) in Java, rather than regular strings (String).


String(String):In Java, regular strings are immutable objects. This means that once a string is created, its content cannot be modified.


StringBuilder (StringBuilder) and StringBuffer:These classes are designed for mutable string manipulation. They are like building blocks where you can efficiently create and modify strings.


They have a property called capacity, which refers to the allocated storage space available for characters within the string builder/buffer object.



/*OUTPUT*/

current capacity 20
Hello world!

#trim()


/*OUTPUT*/

  ApnaStudy is the best website to learn java
52
ApnaStudy is the best website to learn java
43

#split()


/*OUTPUT*/

by whitespace: ApnaStudy
is
the
best
website
to
learn
java

#replace()


/*OUTPUT*/

Replaced 'e' with 'a':
ApnaStudy is tha bast wabsita to laarn java
Replaced 'a' with 'e': ApneStudy is the best website to leern jeve

#lastIndexOf(str)


/*OUTPUT*/

25
26

#substring
(beginIndex:0,endIndex:n)



/*OUTPUT*/

tudy is th

#substring(beginIndex:0)


/*OUTPUT*/

the best website to learn java

#Some other methods in java


/*OUTPUT*/

ApnaStudy is the best website to learn javaApnaStudy is the best website to learn javaApnaStudy is the best website to learn java
Hello World
false
true

Previous Next