In Java, A group of characters known as a string class are ones that a script interprets literally. A string is represented as a byte (or term) range information structure in order to preserve a number of components, frequently letters. Additional types and arrangements of sequence (or array) data, such as more adaptable arrays, may also be designated by the sequence.
Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.
To know more about String Class in java read the complete article.
String replaceFirst()Method:
One of the Java String Methods is the replaceFirst method, which replaces the first occurring substring with a freshly provided string. The Java String replaceFirst Method substitutes a new string given by the user for the first substring that appears in an object.
The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.
Returns The resulting String
Note: Replaces the first substring of this string that matches the given regular expression with the given replacement.
public class Main {
public static void main(String[] args) {
String str = "PrepInsta Prime";
System.out.println("Returning words:");
String[] arr = str.split("P", 0);
for (String w : arr) {
System.out.println(w);
}
System.out.println("Split array length: "+arr.length);
}
}
Output
Hey I am Prepxtar
Hey I am zrepStar
Explanation:Here the output comes is( Hey I am Prepxtar) (Hey I am zrepStar) as we can see the char S is replaced by x and P by z USING replace method.
public class Main {
public static void main(String[] args) {
String myStr = "My PrepInsta Prime is account is of one xear";
System.out.println(myStr.replace('x', 'y'));
}
}
Output
My PrepInsta Prime is account is of one year
Explanation:Here the output comes is My PrepInsta Prime is account is of one year because using Replace method
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment