
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 7442 Articles for Java

17K+ 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

310 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

269 Views
public class RegexOccur { public static void main(String args[]) { String str = "java is fun so learn java"; String findStr = "java"; int lastIndex = 0; int count = 0; while(lastIndex != -1) { lastIndex = str.indexOf(findStr,lastIndex); if(lastIndex != -1) { count ++; lastIndex += findStr.length(); } } System.out.println(count); } }Output2

269 Views
public class RegexOccur { public static void main(String args[]) { String str = "java is fun so learn java"; String findStr = "java"; int lastIndex = 0; int count = 0; while(lastIndex != -1) { lastIndex = str.indexOf(findStr,lastIndex); if(lastIndex != -1) { count ++; lastIndex += findStr.length(); } } System.out.println(count); } }Output2

3K+ 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

1K+ 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

1K+ Views
Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".Capturing groups are numbered by counting their opening parentheses from the left to the right.In the expression ((A)(B(C))), for example, there are four such groups -((A)(B(C)))(A)(B(C))(C)Back references allow repeating a capturing group using a number like \# where # is the groupnumber. See the example below −ExampleLive Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Tester { ... Read More