My simple library

..of useful code



Chapters

Strings

For more info check out the String java docs

StringBuffer demoString = new StringBuffer(“GeeksforGeeks”);
StringBuilder demoString = new StringBuilder(); demoString.append(“GFG”);

String demoString = “Geeks”; //In String constant pool
String demoString = new String(“Geeks”); //Stored in heapp

Regex

For a quick regex builder click here

Regex classes
Class/Interface Description
Pattern Class Used for defining patterns
Matcher Class Used for performing match operations on text using patterns
PatternSyntaxException Class Used for indicating syntax error in a regular expression pattern
MatchResult Interface Used for representing the result of a match operation
Pattern class
Method Description

compile(String regex)

It is used to compile the given regular expression into a pattern.

compile(String regex, int flags)

It is used to compile the given regular expression into a pattern with the given flags.

matcher(CharSequence input)

It is used to create a matcher that will match the given input against this pattern.

matches(String regex, CharSequence input)

It is used to compile the given regular expression and attempts to match the given input against it.

pattern()

It is used to return the regular expression from which this pattern was compiled.

quote(String s)

It is used to return a literal pattern String for the specified String.

split(CharSequence input)

It is used to split the given input sequence around matches of this pattern.

split(CharSequence input, int limit)

It is used to split the given input sequence around matches of this pattern. The limit parameter controls the number of times the pattern is applied.
Matcher class
Method Description

find()

It is mainly used for searching multiple occurences of the regex in the text

find(int start)

Used for searching uccurences of the regex in the text starting form the given index

start()

Used for getting the start index of a match that is being found using find() method

end()

Used for getting the end index of a match that is being found using find() method. Returns the index of th char next to the last matching char

groupCount()

Used to find the total number of the matched subsequence

group()

Used to find the matched subsequence

matches()

Used to test whether the regex matches the pattern
Regex Character Classes
Character Class Description
[xyz] x, y, or z
[^xyz] Any characters other than x, y, or z
[a-zA-Z] Characters from range a to z or A to Z
[a-f[m-t]] Union of a to f and m to t
[a-z && p-y] All the range of elements intersection between two ranges
[a-z && [^bc]] a to z union with except b and c
[a-z && [^m-p]] a to z union with except range m to p
Regex Quantifiers
Regex Description
X? X appears once or not
X+ X appears once or more than once
X* X appears zero times or more
X{n} X appears exactly n times
X{n,} X appears n times or more
X{n,m} X appears at least n times and at most m times
Character classes
Regex Description
. Any character
\d Any digits [0-9]
\D Any non-digit [^0-9]
\s White space character [\t\n\x0B\f\r]
\S Non whitespace character
\w Word character [a-zA-Z_0-9]
\W Non Word character [^a-zA-Z_0-9]
\b Word boundary
\B Non word boundary