Java Strings
Java Tutorial » Java Strings
Strings, widely used in Java programming, are used for storing characters surrounded by double quotes.
In the below example, we create a variable of type string and then we assign it a value:
Example:
Hey, Hey man.
public class Main {
public static void main(String[] args) {
String greeting = "Hey, Hey man.";
System.out.println(greeting);
}
}
Output:public static void main(String[] args) {
String greeting = "Hey, Hey man.";
System.out.println(greeting);
}
}
Hey, Hey man.
String Length
The length() method will return the number of characters that are contained in the string.
Example:
Text length of the string is: 49
public class Main {
public static void main(String[] args) {
String my_txt = "agernic tutorial contain pretty good explanations";
System.out.println("Text length of the string is: " + my_txt.length());
}
}
Output:public static void main(String[] args) {
String my_txt = "agernic tutorial contain pretty good explanations";
System.out.println("Text length of the string is: " + my_txt.length());
}
}
Text length of the string is: 49
String Concatenation
Combine more strings using + operator is called concatenation:
This will returns a new string as string1 with string2 added to it at the end.
Example: use the + method
Chris Langford
public class Main {
public static void main(String args[]) {
String Name = "Chris";
String SurName = "Langford";
System.out.println(Name + " " + SurName);
}
}
Output:public static void main(String args[]) {
String Name = "Chris";
String SurName = "Langford";
System.out.println(Name + " " + SurName);
}
}
Chris Langford
Note: we have added an empty text (" ") to create a space between mames.
We use the concat() method to concatenate two strings
Example: concat() method
Chrys Store
public class Main {
public static void main(String[] args) {
String Name = "Chrys ";
String SurName = "Store";
System.out.println(Name.concat(SurName));
}
}
Output:public static void main(String[] args) {
String Name = "Chrys ";
String SurName = "Store";
System.out.println(Name.concat(SurName));
}
}
Chrys Store
Finding a Character or character sequence in a String
In a specified text indexOf() method returns the index (position) in a string.
Example:
14
public class Main {
public static void main(String[] args) {
String my_txt = "please locate me where is the position of the word: me";
System.out.println(my_txt.indexOf("me"));
}
}
Output:public static void main(String[] args) {
String my_txt = "please locate me where is the position of the word: me";
System.out.println(my_txt.indexOf("me"));
}
}
14
Java String to init, split, length, methods, format, array, contains, compare, equals, concatenation Java Strings imutable, substring, list, compare,
Java String - java
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.