- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
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
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
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
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.
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
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
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
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
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