String:
Strings are used for storing text.
A String variable contains a collection of characters surrounded by double quotes.
String is a sequence of characters. In Java, objects of the String class are immutable which means they cannot be changed once created.
Creating a String
There are two ways to create string in Java:
- Using String literal
String s = “GeeksforGeeks”;
- Using new keyword
String s = new String (“GeeksforGeeks”);
String Length
A String in Java is actually an object, which contain methods that can perform certain operations on strings. For example, the length of a string can be found with the length() method:
Example
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());
Output:The length of the txt string is: 26
More String Methods(TBD)
There are many string methods available, for example
toUpperCase();
toLowerCase();
Example:Example
String txt = "Hello World";
System.out.println(txt.toUpperCase()); // Outputs "HELLO WORLD"
System.out.println(txt.toLowerCase()); // Outputs "hello world"
Reference:https://www.w3schools.com/java/java_strings.asp
Reference:https://www.geeksforgeeks.org/string-class-in-java/
Example program:
package String_start;
public class BasicString_methods {
public static void main(String[] args) {
String name = "Harish kumar";
System.out.println(name.charAt(3));// to find position of char 3--i
System.out.println(name.toLowerCase());// lowercase
System.out.println(name.toUpperCase());// uppercase
System.out.println(name.contains("kumar"));// it will given word present or not---true if present
System.out.println(name.indexOf('u'));// to find position of char--8index
System.out.println(name.lastIndexOf('r'));// 11index
// for(int i=0;i<name.length();i++)
// {
// System.out.println(name.charAt(i));//string to char
//
// }
}
}
Output:
i
harish kumar
HARISH KUMAR
true
8
11
StringBuffer class in Java
StringBuffer is a class in Java that represents a mutable sequence of characters. It provides an alternative to the immutable String class, allowing you to modify the contents of a string without creating a new object every time.
StringBuffer s = new StringBuffer();
Features of StringBuffer Class
Here are some important features and methods of the StringBuffer class:
StringBuffer objects are mutable, meaning that you can change the contents of the buffer without creating a new object.
The initial capacity of a StringBuffer can be specified when it is created, or it can be set later with the ensureCapacity() method.
The append() method is used to add characters, strings, or other objects to the end of the buffer.
The insert() method is used to insert characters, strings, or other objects at a specified position in the buffer.(TBD)
The delete() method is used to remove characters from the buffer.(TBD)
The reverse() method is used to reverse the order of the characters in the buffer.(TBD)
public class StringBufferExample {
public static void main(String[] args){
// Creating StringBuffer
StringBuffer s = new StringBuffer();
// Adding elements in StringBuffer
s.append("Hello");
s.append(" ");
s.append("world");
// String with the StringBuffer value
String str = s.toString();
System.out.println(str);
}
}
Output
Hello world
Advantages of using StringBuffer in Java
There are several advantages of using StringBuffer over regular String objects in Java:
**Mutable: StringBuffer objects are mutable, which means that you can modify the contents of the object after it has been created.
In contrast, String objects are immutable, which means that you cannot change the contents of a String once it has been created.
Efficient: Because StringBuffer objects are mutable, they are more efficient than creating new String objects each time you need to modify a string. This is especially true if you need to modify a string multiple times, as each modification to a String object creates a new object and discards the old one.**
Java Wrapper Classes
Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.
Wrapper classes no need object.
ex:int-->Integer
Integer no=10;-->Integer act as object
Creating Wrapper Objects
To create a wrapper object, use the wrapper class instead of the primitive type. To get the value, you can just print the object:
public class Main {
public static void main(String[] args) {
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
}
Output:
5
5.99
A
Reference:https://www.w3schools.com/java/java_wrapper_classes.asp
String split() (TBD)Split a string into an array of strings:
The split() method splits a string into an array of substrings using a regular expression as the separator.
Reference:https://www.w3schools.com/java/ref_string_split.asp
Top comments (0)