String in Java
In Java, a string is a sequence of characters. Strings are objects, which means they are instances of the class java.lang.String
.
Here's an example of how to declare a string variable in Java:
javaString myString = "Hello, world!";
In this example, the variable myString
is declared as a string and initialized with the value "Hello, world!".
You can perform various operations on strings in Java, such as concatenation, length calculation, and substring extraction. Here are some examples:
java// Concatenation
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // "John Doe"
// Length calculation
String message = "This is a message.";
int length = message.length(); // 18
// Substring extraction
String sentence = "The quick brown fox jumps over the lazy dog.";
String substring = sentence.substring(4, 9); // "quick"
In addition to the basic string manipulation methods, the java.lang.String
class provides many other useful methods, such as charAt
, indexOf
, replace
, toLowerCase
, toUpperCase
, and more.
Comments
Post a Comment