String Operations in JAVA
Java is known as a pure object oriented programming language and same as other languages it also contains a wide range of variables like int, char, bool, and string, etc. For Strings, you can perform different string operations.
In this post we discuss following topics:
- How to create a String in Java and String variables in java?
- How to combine or concatenate two strings in java?
- How to find the length of the string in java?
- How to compare two strings in java?
- How to find the index of a character in a string?
- How to convert String to char array?
- How to replace a character in a string in java?
- How to split a string in java?
- How to convert a lowercase string to uppercase in Java?
String defining and declaration in java:
We can declare a string in java using two methods one is like simple variables declarations:
1 2 3 4 5 |
String name = "This is my Name"; And other is using string class constructor. String name = new String ("This is my Name"); |
The thing to remember in Java the string keyword starts with capital S.
Concatenate string in Java:
To combine two string in java concat() function is used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class MyFirstProject{ public static void main(String args[]){ String fname= "Usman"; String lname= " Siddiqe"; String name = fname.concat(lname); System.out.println("My name is :"+name); } } |
Find length of string:
To find the length of the string in Java, we use the length() function.
1 2 3 4 5 6 7 8 9 10 11 |
public class MyFirstProject{ public static void main(String args[]){ String value = "Hello Java"; System.out.println("String Length is :"+value.length()); } } |
Compare two strings:
To compare two strings in java we use the compareTo() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class MyFirstProject{ public static void main(String args[]){ String first_password = "kkghtqwe"; String second_password = "hgtytgtr"; if(first_password.compareTo(second_password)==0){ System.out.println("Strings are equal"); }else{ System.out.println("Strings are not equal"); } } } |
Find index of a character in string:
The indexOf() function is used to find any index of character in string:
1 2 3 4 5 6 7 8 9 10 11 |
public class MyFirstProject{ public static void main(String args[]){ String val = "hello world"; System.out.println("Index of 'q' is: "+val.indexOf('w')); } } |
Convert String to char array:
To a convert a string to character array toCharArray() function is used:
1 2 3 4 5 6 7 8 9 10 11 |
public class MyFirstProject{ public static void main(String args[]){ String value = "My name is khan"; System.out.print("Return Value :" ); System.out.println(value.toCharArray() ); } } |
Replace character in string:
To replace a character and a substring in a string with some other String replaceAll() function is used in Java.
1 2 3 4 5 6 7 8 9 10 |
public class MyFirstProject{ public static void main(String args[]){ String Str = new String("Hello i love india"); System.out.println("Replaced string: "+Str.replaceAll("india","Pakistan" )); } } |
Split a string in Java:
To split a string in Java string.split() function is used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class MyFirstProject{ public static void main(String args[]){ String Str = new String("Hello i love dubai"); for (String newvalue: Str.split(" ", 4)){ System.out.println(newvalue); } } } |
To lower Case:
To convert a string in lower case order toLowerCase() function is used.
1 2 3 4 5 6 7 8 9 10 11 |
public class MyFirstProject{ public static void main(String args[]){ String str = new String("Hello i love dubai"); System.out.println("new String:" + str.toLowerCase()); } } |
To Uppercase in Java:
To convert a string to upper case toUpperCase() function is used.
1 2 3 4 5 6 7 8 9 10 |
public class MyFirstProject{ public static void main(String args[]){ String str = new String("Hello i love dubai"); System.out.println("new String:" + str.toUpperCase()); } } |