Learn Strings In Java

Sharing is healthy !!

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,

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,

Also Read – 3 Best Ways In Java String Concatenation

public class String_Length {

public static void main(String[] args) {
 
String str_Example = "Flower Brackets";
int get_Length = str_Example.length();
System.out.println("String Length is : " + get_Length);

}

}

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 String_Length {

public static void main(String[] args) {
 
String str_Example = "Flower Brackets";
String get_Case = str_Example.toLowerCase();
String get_Case1 = str_Example.toUpperCase();
System.out.println("Lowercase : " + get_Case);
System.out.println("Uppercase : " + get_Case1);

}

}

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 String_Length {

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

}

}

Output:

Concatenate Two Strings : Flower Brackets


string replace function

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

public class String_Length {

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

}

}

Output:

Replace function : Flowar Brackats


indexof character

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

public class String_Length {

public static void main(String[] args) {
 
String str_Example = "Flower Brackets";
System.out.println("Indexof 'r' : " + str_Example.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.

Sharing is healthy !!

Leave a Reply

Your email address will not be published.