strings in java
  • Introduction To Strings
  • Methods of Strings
  • Implementation With Examples
  • Other Concept

Introduction To Strings

The String is an object in java.

The string represents a sequence of characters, But unlike many other languages that implement String as character arrays, java implements string as objects of type string.

Here is a string “Hello World” OR more accurately it’s a string literal, which means a string value 

A String is Instantiated as 

The most appropriate approach to make a string is: 

String firstEx = “Hello World!”;

The String is a class but can be used as a data type

String firstEx(refrence) = “Hello World!”(object);

Creating String

Using Literal Using new keyword

String name = “Great”; String str= new String();

Methods Of String

The String class in Java provides a number of useful methods which helps to modifying of string:

Methods Effects

• concate() concatenates two strings

• length() To find the length of String

• replace(“a”, “b”) replaces Characters of ‘a’ with ‘b’

• toUpperCase() converts the string to upper case

• toLowerCase() converts the string to lower case

• trim() removes the whitespaces at the beginning and at the end

• indexOf(“a”) returns the position of first occurrence of a in the string.

• startsWith(“x”) returns a boolean true or false

• endsWith(“x”) returns a boolean true or false

• equals(“x”) returns a boolean true or false

Implementation With Examples

1. How to Concatenate Strings 

We can concatenate or joining a string with another one using the plus(+) operator For Example

The string is a class it has members that we can access using the dot(.) operator so we can type Example.method().       

We can also use the concate() method to join a string for Example:

2. endsWith()   

We have the method endsWith() and with this, we can check to see if our string ends with a character or sequence of characters For example 

Output: true

When we run the program we get true. So, this method that we have returns a boolean value which can be true or false  

Now take a look one more time what happens 

Output: false

3. startsWith()

We also have another method startsWith() let’s take a look 

in this case we get false because our Example doesn’t start with “thon”.             One more Example 

Output: true

4. length()

Another useful method is length so we can call that to get the length of a string which is the number of characters 

Output: 4

So in this string, we have 4 characters and this is useful in situations where you want to check the length of the input by the user for example

You might have a Sign-up form with a username field you can check the length of someone’s username and give them an error if the username is longer than 22 characters

 5. indexOf()

We also have another method that is indexOf() and this returns the index of the first occurrence of the character or the string for example       If you write “J” the index of J is 0

Output: 0

If you write “a” we get 1

Output: 1

because the index of the first “a” in this Example is 1   

Now what if you write a character or string that doesn’t exist in this Example

let’s write “python” let’s take a look

Output: -1

we get a negative 1. So with this method, we can check if a string contains certain characters or words or sentences and so on.

6. replace()

We have a method replace() and with this, we can replace one or more characters with something else for example

We can replace “P” with “J” So this replace method has two parameters, one is target the other is replacement and here we’re writing two values for these parameters 

here’s the first value “P”, the second value “J” and we have separated these values using a comma(,)

Now in programming terms, we refer to these values as arguments a lot of programmers don’t know the difference between parameters and arguments parameters are the holes that we define in our methods,

arguments are the actual values that we write to these methods. So in this case target and replacement are parameters but “P” and “J” are arguments 

Now take a look at what happens 

Output: Jython

Our P is replaced with J, now what is important here is that this method does not modify our original string it returns a new string. 

So, If we print our original string after 

Output: Jython

Output: Python //Original string

You can see the original string is not changed because in Java strings are Immutable.

We cannot mutate them we cannot change them so any methods that modify a string will always return a new string object.

7. toLowerCase()

We also have another useful method toLowerCase()          For example 

Output: java

Output: JAVA //Original string

toLowerCase() converts all characters to lowercase and once again you can see that the original string is not affected because this method returns a new string.

8. toUpperCase()

We also have toUpperCase() Method

Output: PYTHON

Output: python //Original string

this method converts all characters to Uppercase.

9. trim()

Another useful Method is trim() and with this, we can get rid of extra white spaces that can be at the beginning or the end of a string. Sometimes our users type unnecessary spaces in form fields so using the trim() method we can get rid of these white spaces for example

Output: JAVA & python

Output:    JAVA & python    //Original string

So we can see this if we add a couple of spaces before and after our Example then when we trim it, these white spaces are gonna get removed.

so the original string you can see two white spaces at the beginning.

10. equals()

The equals() method checks a string is equals with another one for example

Output: true

This example gives a boolean value true. Because the string is the same as after modification

here’s another example

Output: false   this example gives Boolean value false.

These are some useful methods in the String class.

Other Concepts

Escape Sequence

We have the string  

Hi, My name is Ankit & I am the Student of Maths” 

If we want to surround Maths with double-quote   

Now here’s the problem if we add a double quote before Maths, the Java compiler thinks this is the termination of our string so it doesn’t understand what we have after the double quote that’s why the Java compiler gives a compilation error. 

to fix this problem we need to prefix this double code with a backslash (\”)

so using this backslash we have escaped the double quote now take a look at what we get

So we get     

Output: Hi, My name is Ankit & I am the Student of “Maths”

In this case we learn about a Special Character

Java gives us some more Special Characters :

Escape Sequence Effect

\n Create a new line

\t Insert a TAB

\b Create a backspace character  

\’ Create a quote

\” Create a Double quote

\\ Create a Backslash

If we want to write a directory on Windows so that will look like this

c:\user\admin\documents…

Now if you want to write this in a string we need to escape each backslash

so for this, you have to add two backslashes (\\) for example

The Other special character is backslash N(\n) and we use that to add a new line to our strings so let’s write the backslash N now take look at what happens 

now our string is broken down onto multiple lines by the first line we have Hello

then we have Students so wherever we had a backslash N Java will insert a new line

We can also add a tab in our strings

To add, a tab write backslash t (\t) in string

let’s take a look

here we have Hello a tab and then Students  

The String is immutable:

A string is immutable i.e. we cannot change the string after creation. However, a variable declared as a  String reference can be changed to point to some other string object at any time.

We can still perform all types of string operations. Each time we need an altered version of an existing string, a new string object is created that contains the modifications.

The original string is left unchanged. This approach is used because fixed, immutable strings can be implemented more efficiently than changeable ones.

For those cases in which a modified string is desired, there is a companion class called StringBuffer, whose objects contain strings that can be modified after they are created.

0

LEAVE A REPLY

Please enter your comment!
Please enter your name here

four × 3 =