Learn Strings In Java

Welcome to flower brackets blog. Today we are going to learn strings in java.

In this post we are going to cover what are strings and how we can use strings in java.

What is a string?

A string is a sequence of characters or a string is a character array. To declare a string you just need to use “String” keyword like this,

Also Read – Learn String Literal In Java

String name = "George";

Now there are some properties and functions related to strings which we can use.

Learn strings in java : length of the string

Let’s find the length of the string,

public class StringLength
{
   public static void main(String[] args)
   {
      String strExample = "Flower Brackets";
      int getLength = strExample.length();
      System.out.println("String Length is : " + getLength);
   }
}

Output:

String Length is : 15

In the above example i have declared variable called get_Length, which is equal to variable name of string dot length. And this function length will return the length of the string.


Learn strings in java : lowercase and uppercase

Java itself has many properties and functions. In the below example let’s convert the given string to lowercase and uppercase.

public class StringLength
{
   public static void main(String[] args)
   {
      String strExample = "Flower Brackets";
      String getCase = strExample.toLowerCase();
      String getCase1 = strExample.toUpperCase();
      System.out.println("Lowercase : " + getCase);
      System.out.println("Uppercase : " + getCase1);
   }
}

Output:

Learn Strings In Java


Learn strings in java : concatenate two strings

Here we will join two strings using “+” sign. “+” sign is used between two strings to concatenate like this,

public class StringLength
{
   public static void main(String[] args)
   {
      String strExample = "Flower " + "Brackets";
      System.out.println("Concatenate Two Strings : " + strExample);
   }
}

Output:

Concatenate Two Strings : Flower Brackets


String replace function

In the below example we will replace some character from given string,

public class StringLength
{
   public static void main(String[] args)
   {
      String strExample = "Flower Brackets";
      System.out.println("Replace function : " + strExample.replace('e', 'a'));
   }
}

Output:

Replace function : Flowar Brackats


Indexof character

For example if you want to find the index ‘r’,

public class StringLength
{
   public static void main(String[] args)
   {
      String strExample = "Flower Brackets";
      System.out.println("Indexof 'r' : " + strExample.indexOf('r'));
   }
}

Output:

Indexof ‘r’ : 5


Conclusion

So just find your function which suits your requirement and you will be good to go. In this way you can use strings in java. I hope you enjoyed this post.

Related Posts