- There are 4 types of special string Operations
- String Literals
- String Concatenation
- String Concatenation with Other Data Types
- String Conversion and toString( )
- String literal is created by using double quotes.
- For Example:
String s="javaguides"; - String Concatenation can be done using + operator, which concatenates two strings, producing a String object.
- You can concatenate strings with other types of data.
- For Example:
String age = “10"; String s = "He is " + age + " years old."; System.out.println(s); //Output: He is 10 years old You can concatenate strings with other types of data. - You can concatenate strings with other types of data.
String Conversion and toString( ) : valueOf() method For Example The java string valueOf() method converts different types of values into string. By the help of string valueOf() method, you can convert int to string, long to string, boolean to string, character to string, float to string, double to string, object to string and char array to string
class Main{
public static void main(String[] args) {
int value=30;
String s1=String.valueOf(value); System.out.println(s1+10);//concatenating string with 10
}
}
String Handling Character Extraction - There are several ways by which characters can be extracted from String class object.
- charAt() -- method is used to extract a single character at an index.
Example of “Character Extraction” Program - class temp
- {
- public static void main(String args[])
- {
- String str="Hello";
- char ch=str.charAt(2);
- System.out.println(ch);
- }
- }
Do'stlaringiz bilan baham: |