Found 9326 Articles for Object Oriented Programming

What is the package for String Class in Java?

Jai Janardhan
Updated on 30-Jul-2019 22:30:21

4K+ Views

String class belongs to the java.lang package.

Manipulating Strings in Java.

Manikanth Mani
Updated on 26-Feb-2020 05:12:20

529 Views

Since String class is immutable once created we cannot modify the data of the string. But still if you want to manipulate string data you can rely on StringBuffer or StringBuilder classes.Examplepublic class Test {    public static void main(String args[]) {       String str = "Hi welcome ";       StringBuffer sb= new StringBuffer(str);       sb.append("to Tutorialspoint");       System.out.println(sb);    } }OutputHi welcome to Tutorialspoint

Are there any ways to Manipulate Strings in Java.

Ayyan
Updated on 26-Feb-2020 05:11:31

98 Views

Since String class is immutable once created we cannot modify the data of the string. But still, if you want to manipulate string data you can rely on StringBuffer or StringBuilder classes.Examplepublic class Test {    public static void main(String args[]) {       String str = "Hi welcome ";       StringBuffer sb= new StringBuffer(str);       sb.append("to Tutorialspoint");       System.out.println(sb);    } }OutputHi welcome to Tutorialspoint

Java Program to reverse a given String with preserving the position of space.

Akshaya Akki
Updated on 26-Feb-2020 05:09:40

3K+ Views

You can reverse the contents of a given String using leaving the spaces using the reverse() method of the StringBuffer class.Examplepublic class Test {    public static void main(String args[]) {       String str = "hi welcome to Tutorialspoint";       String strArray[] = str.split(" ");       StringBuffer sb= new StringBuffer(str);       sb.reverse();       for(int i=0 ; i

Learn Everything about Java String?

George John
Updated on 30-Jul-2019 22:30:21

125 Views

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant, their values cannot be changed after they are created. To learn more about Strings visit Tutorialspoint Strings page.

How to remove the last character from a string in Java?

Alankritha Ammu
Updated on 26-Feb-2020 05:07:49

2K+ Views

The StringBuffer class contains a method known as deleteCharAt(). This method deletes the character at a specified index/position. You can use this method to delete/remove a particular character from a string in Java.Examplepublic class Test {    public static void main(String args[]){       String str = "hi welcome to Tutorialspoint";       StringBuffer sb= new StringBuffer(str);       sb.deleteCharAt(sb.length()-1);       System.out.println(sb);    } }Outputhi welcome to Tutorialspoint

String Handling in Java

Arushi
Updated on 26-Feb-2020 05:46:34

16K+ Views

Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects.The Java platform provides the String class to create and manipulate strings.The most direct way to create a string is to write −String greeting = "Hello world!";Whenever it encounters a string literal in your code, the compiler creates a String object with its value in this case, "Hello world!'.ExampleLive Demopublic class StringDemo {    public static void main(String args[]) {       char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };       String helloString ... Read More

How to extract a group from a Java String that contains a Regex pattern

Arnab Chakraborty
Updated on 21-Jun-2020 06:30:13

218 Views

How to extract a group from a Java String that contains a Regex patternimport java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTest {    public static void main(String[] args) {       Pattern pattern = Pattern.compile("fun");       Matcher matcher = pattern.matcher("Java is fun");       // using Matcher find(), group(), start() and end() methods       while (matcher.find()) {          System.out.println("Found the text \"" + matcher.group()             + "\" starting at " + matcher.start()             + " index and ending at index ... Read More

How to capture multiple matches in the same line in Java regex

Arnab Chakraborty
Updated on 20-Jun-2020 10:49:20

2K+ Views

Exampleimport java.util.regex.*; class PatternMatcher {    public static void main(String args[]) {       int count = 0;       // String to be scanned to find the pattern.       String content = "aaa bb aaa";       String string = "aaa";       // Create a Pattern object       Pattern p = Pattern.compile(string);       // get a matcher object       Matcher m = p.matcher(content);       while(m.find()) {          count++;          System.out.println("Match no:"+count);         ... Read More

Search and Replace with Java regular expressions

Sravani S
Updated on 26-Feb-2020 08:09:33

869 Views

Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar to the Perl programming language and very easy to learn.A regular expression is a special sequence of characters that help you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.The replaceFirst() and replaceAll() methods replace the text that matches a given regular expression. As their names indicate, replaceFirst replaces the first occurrence, and replaceAll replaces all occurrences.ExampleLive Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { ... Read More

Advertisements