Class StringParse
- java.lang.Object
-
- Torello.Java.StringParse
-
public class StringParse extends java.lang.Object
A plethora of extensions to Java'sString
class.
String
-Tools which have been written for this'.jar'
Library.
Hi-Lited Source-Code:- View Here: Torello/Java/StringParse.java
- Open New Browser-Tab: Torello/Java/StringParse.java
File Size: 87,611 Bytes Line Count: 2,004 '\n' Characters Found
Stateless Class:This class neither contains any program-state, nor can it be instantiated. The@StaticFunctional
Annotation may also be called 'The Spaghetti Report'.Static-Functional
classes are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's@Stateless
Annotation.
- 1 Constructor(s), 1 declared private, zero-argument constructor
- 68 Method(s), 68 declared static
- 21 Field(s), 21 declared static, 21 declared final
-
-
Field Summary
Regular-Expressions Modifier and Type Field static Pattern
ALPHA_NUMERIC
static Pattern
COMMA_REGEX
static Pattern
FLOATING_POINT_REGEX
static Pattern
NEWLINEP
static Pattern
NUMBER_COMMMA_REGEX
static Pattern
WHITE_SPACE_REGEX
Predicate Modifier and Type Field static Predicate<String>
alphaNumPred
static Predicate<String>
floatingPointPred
static Predicate<String>
newLinePred
More Suff Modifier and Type Field static String[]
EMPTY_STR_ARRAY
static ReadOnlyList<String>
months
static String
REG_EX_ESCAPE_CHARS
-
Method Summary
General Methods Modifier and Type Method static int
countCharacters(String s, char c)
static String
delChar(String str, int i)
static String
firstWord(String s)
static MatchResult[]
getAllMatches(String s, Pattern regEx, boolean eliminateOverlappingMatches)
static boolean
hasWhiteSpace(String s)
static String
ifQuotesStripQuotes(String s)
static String
leftSpacePad(String s, int totalStringLength)
static String
nChars(char c, int n)
static String
nStrings(String s, int n)
static int
numLines(String text)
static String
removeDuplicateSpaces(String s)
static String
removeWhiteSpace(String s)
static String
rightSpacePad(String s, int totalStringLength)
static String
setChar(String str, int i, char c)
static String
trimLeft(String s)
static String
trimRight(String s)
File, Directory & URL Strings Modifier and Type Method static String
beforeExtension(String file)
static String
beforeLastFileSeparatorPos(String urlOrDir)
static String
beforeLastFrontSlashPos(String urlOrDir)
static String
dotDotParentDirectory(String urlAsStr)
static String
dotDotParentDirectory(String directoryStr, char dirSeparator)
static String
dotDotParentDirectory(String directoryStr, char separator, short nLevels)
static String
dotDotParentDirectory(String urlAsStr, short nLevels)
static String
dotDotParentDirectory(URL url)
static String
dotDotParentDirectory(URL url, short nLevels)
static String
dotDots(String fileName, String ancestorDirectory, char separator)
static int
findExtension(String file, boolean includeDot)
static int
findLastFileSeparatorPos(String fileOrDir)
static int
findLastFrontSlashPos(String urlOrDir)
static String
findURLRoot(String url)
static String
fromExtension(String file, boolean includeDot)
static String
fromLastFileSeparatorPos(String fileOrDir)
static String
fromLastFrontSlashPos(String urlOrDir)
static String
removeExtension(String fileNameOrURL)
static String
swapExtension(String fileNameOrURLWithExtension, String newExtension)
Date & Time Modifier and Type Method static String
dateStr()
static String
dateStr(char separator)
static String
dateStr(char separator, boolean includeMonthName)
static String
dateStrGOVCN()
static String
monthStr(int month)
static String
timeStr()
static String
timeStr(char separator)
static String
timeStrComplete()
static String
ymDateStr()
static String
ymDateStr(char separator)
static String
ymDateStr(char separator, boolean includeMonthName)
Number Strings Modifier and Type Method static String
commas(long l)
static boolean
isByte(String s)
static boolean
isDouble(String s)
static boolean
isInt(String s)
static boolean
isInteger(String s)
static boolean
isLong(String s)
protected static boolean
isOfPrimitiveType(String s, char[] minArr)
static boolean
isShort(String s)
static String
ordinalIndicator(int i)
static String
zeroPad(int n)
static String
zeroPad(int n, int powerOf10)
static String
zeroPad10e2(int n)
static String
zeroPad10e4(int n)
Base64 Object Serialization Modifier and Type Method static Object
b64MimeStrToObj(String str)
static Object
b64StrToObj(String str)
static String
objToB64MimeStr(Object o)
static String
objToB64Str(Object o)
Parentheticals Modifier and Type Method static String
removeAllParenthetical(String s)
static String
removeBraces(String s)
static String
removeBrackets(String s)
static String
removeParens(String s)
-
-
-
Field Detail
-
WHITE_SPACE_REGEX
public static final java.util.regex.Pattern WHITE_SPACE_REGEX
This regular expression simply matches white-space found in a javaString
.- See Also:
removeWhiteSpace(String)
- Code:
- Exact Field Declaration Expression:
public static final Pattern WHITE_SPACE_REGEX = Pattern.compile("\\s+");
-
COMMA_REGEX
public static final java.util.regex.Pattern COMMA_REGEX
This regular expression simply matches the comma. The only reason for including this here is because the javaclass 'Pattern'
contains a method calledStream<String> 'splitAsStream(CharSequence)'
which is used for the CSV method further below- See Also:
StrCSV.CSV(String, boolean, boolean)
,FileRW.readDoublesFromFile(String, boolean, boolean)
,FileRW.readLongsFromFile(String, boolean, boolean, int)
- Code:
- Exact Field Declaration Expression:
public static final Pattern COMMA_REGEX = Pattern.compile(",");
-
NUMBER_COMMMA_REGEX
public static final java.util.regex.Pattern NUMBER_COMMMA_REGEX
This regular expression is used for integer and floating-point numbers that use the comma (','
) between the digits that comprise the number. For example, this Regular Expression would match theString
"900,800,75.00"
.- See Also:
FileRW.readIntsFromFile(String, boolean, boolean, int)
- Code:
- Exact Field Declaration Expression:
public static final Pattern NUMBER_COMMMA_REGEX = Pattern.compile("(\\d),(\\d)");
-
NEWLINEP
public static final java.util.regex.Pattern NEWLINEP
This represents any version of the new-line character. Note that the'\r\n'
version comes before the single'\r'
version in the regular-expression, to guarantee that if both are present, they are treated as a single newline.
-
newLinePred
public static final java.util.function.Predicate<java.lang.String> newLinePred
Predicate for new-line characters- See Also:
NEWLINEP
- Code:
- Exact Field Declaration Expression:
public static final Predicate<String> newLinePred = NEWLINEP.asPredicate();
-
months
public static final ReadOnlyList<java.lang.String> months
The months of the year, as an immutable list ofString's
.
-
REG_EX_ESCAPE_CHARS
public static final java.lang.String REG_EX_ESCAPE_CHARS
This is the list of characters that need to be escaped for a regular expression- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
public static final String REG_EX_ESCAPE_CHARS = "\\/()[]{}$^+*?-.";
-
ALPHA_NUMERIC
public static final java.util.regex.Pattern ALPHA_NUMERIC
Alpha-Numeric RegEx
-
alphaNumPred
public static final java.util.function.Predicate<java.lang.String> alphaNumPred
Alpha-NumericString
Predicate.- See Also:
ALPHA_NUMERIC
- Code:
- Exact Field Declaration Expression:
public static final Predicate<String> alphaNumPred = ALPHA_NUMERIC.asPredicate();
-
EMPTY_STR_ARRAY
public static final java.lang.String[] EMPTY_STR_ARRAY
An emptyString
array.
-
FLOATING_POINT_REGEX
public static final java.util.regex.Pattern FLOATING_POINT_REGEX
A Predicate which uses a regular-expression for checking whether aString
is a valid & parseabledouble
, which is guaranteed not to throw aNumberFormatException
when using the parserDouble.parseDouble
.
The Following Description is Directly Copied From:java.lang.Double.valueOf(String)
, JDK 1.8Leading and trailing whitespace characters in
s
are ignored. Whitespace is removed as if by theString.trim()
method; that is, both ASCII space and control characters are removed. The rest ofs
should constitute a FloatValue as described by the lexical syntax rules:- FloatValue:
- Signopt
NaN
- Signopt
Infinity
- Signopt FloatingPointLiteral
- Signopt HexFloatingPointLiteral
- SignedInteger
- HexFloatingPointLiteral:
- HexSignificand BinaryExponent FloatTypeSuffixopt
- HexSignificand:
- HexNumeral
- HexNumeral
.
0x
HexDigitsopt.
HexDigits0X
HexDigitsopt.
HexDigits
- BinaryExponent:
- BinaryExponentIndicator SignedInteger
- BinaryExponentIndicator:
p
P
s
does not have the form of a FloatValue, then aNumberFormatException
is thrown. Otherwise,s
is regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to typedouble
by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value.
Note that the round-to-nearest rule also implies overflow and underflow behaviour; if the exact value ofs
is large enough in magnitude (greater than or equal to (Double.MAX_VALUE
+Math.ulp(double) ulp(MAX_VALUE)/2
), rounding todouble
will result in an infinity and if the exact value ofs
is small enough in magnitude (less than or equal toDouble.MIN_VALUE/2
), rounding to float will result in a zero.
Finally, after rounding aDouble
object representing thisdouble
value is returned.To interpret localized string representations of a floating-point value, use subclasses of
java.text.NumberFormat
Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (
1.0f
is afloat
value;1.0d
is adouble
value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. The two-step sequence of conversions, string tofloat
followed byfloat
todouble
, is not equivalent to converting a string directly todouble
. For example, thefloat
literal0.1f
is equal to thedouble
value0.10000000149011612
; thefloat
literal0.1f
represents a different numerical value than thedouble
literal0.1
. (The numerical value 0.1 cannot be exactly represented in a binary floating-point number.)To avoid calling this method on an invalid string and having a
NumberFormatException
be thrown, the regular expression below can be used to screen the input string- See Also:
floatingPointPred
,isDouble(String)
- Code:
- Exact Field Declaration Expression:
public static final Pattern FLOATING_POINT_REGEX = Pattern.compile( // NOTE: Digits, HexDigits & Exp defined ABOVE "[\\x00-\\x20]*"+ // Optional leading "whitespace" "[+-]?(" + // Optional sign character "NaN|" + // "NaN" string "Infinity|" + // "Infinity" string // A decimal floating-point string representing a finite positive // number without a leading sign has at most five basic pieces: // Digits . Digits ExponentPart FloatTypeSuffix // // Since this method allows integer-only strings as input // in addition to strings of floating-point literals, the // two sub-patterns below are simplifications of the grammar // productions from section 3.10.2 of // The Java Language Specification. // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ // . Digits ExponentPart_opt FloatTypeSuffix_opt "(\\.("+Digits+")("+Exp+")?)|"+ // Hexadecimal strings "((" + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "(\\.)?)|" + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + ")[pP][+-]?" + Digits + "))" + "[fFdD]?))" + "[\\x00-\\x20]*" // Optional trailing "whitespace"; );
-
floatingPointPred
public static final java.util.function.Predicate<java.lang.String> floatingPointPred
This is the floating-point regular-expression, simply converted to a predicate.- See Also:
FLOATING_POINT_REGEX
,isDouble(String)
- Code:
- Exact Field Declaration Expression:
public static final Predicate<String> floatingPointPred = FLOATING_POINT_REGEX.asPredicate();
-
-
Method Detail
-
commas
public static java.lang.String commas(long l)
Makes along
number like123456789
into a number-string such as:"123,456,789"
. Java'spackage java.text.*
is easy to use, and versatile, but the commands are not always so easy to remember.- Parameters:
l
- Anylong
integer. Comma's will be inserted for every third power of ten- Returns:
- After calling java's
java.text.DecimalFormat
class, aString
representing this parameter will be returned. - Code:
- Exact Method Body:
return formatter.format(l);
-
trimRight
public static java.lang.String trimRight(java.lang.String s)
Trims any white-spaceCharacters
from the end of aString
.Input String: Output String: "A Quick Brown Fox\n \t"
"A Quick Brown Fox"
"\tA Lazy Dog."
"\tA Lazy Dog."
" " (only white-space)
""
"" (empty-string)
""
null
throws NullPointerException
- Parameters:
s
- Any JavaString
- Returns:
- A copy of the same
String
- but all characters that matched Java methodjava.lang.Character.isWhitespace(char)
and were at the end of theString
will not be included in the returnedString
.
If thezero-length String
is passed to parameter's'
, it shall be returned immediately.
If the resultant-String
has zero-length, it is returned, without exception. - Code:
- Exact Method Body:
if (s.length() == 0) return s; int pos = s.length(); while ((pos > 0) && Character.isWhitespace(s.charAt(--pos))); if (pos == 0) if (Character.isWhitespace(s.charAt(0))) return ""; return s.substring(0, pos + 1);
-
trimLeft
public static java.lang.String trimLeft(java.lang.String s)
Trims any white-spaceCharacters
from the beginning of aString
.Input String: Output String: "\t A Quick Brown Fox"
"A Quick Brown Fox"
"A Lazy Dog. \n\r\t"
"A Lazy Dog. \n\r\t"
" " (only white-space)
""
"" (empty-string)
""
null
throws NullPointerException
- Parameters:
s
- Any JavaString
- Returns:
- A copy of the same
String
- but all characters that matched Java methodjava.lang.Character.isWhitespace(char)
and were at the start of theString
will not be included in the returnedString
.
If thezero-length String
is passed to parameter's'
, it shall be returned immediately.
If the resultant-String
has zero-length, it is returned, without exception. - Code:
- Exact Method Body:
int pos = 0; int len = s.length(); if (len == 0) return s; while ((pos < len) && Character.isWhitespace(s.charAt(pos++))); if (pos == len) if (Character.isWhitespace(s.charAt(len-1))) return ""; return s.substring(pos - 1);
-
leftSpacePad
public static java.lang.String leftSpacePad(java.lang.String s, int totalStringLength)
Primarily for convenience in remembering earlier C-styleprintf(...)
formatting commands.
This method will "left pad" an inputString
with spaces, ifs.length() < totalStrLength
. If input-parameter's'
is equal-to or longer-than the value in'totalStringLength'
, then the originalString
shall be returned.Input Parameters Returned String "Quick Brown Fox"
20
" Quick Brown Fox"
"Hello World"
15
" Hello World"
"Write Once, Run Anywhere"
10
"Write Once, Run Anywhere"
null
NullPointerException
- Parameters:
s
- This may be anyjava.lang.String
totalStringLength
- Ifs.length()
is smaller than'totalStringLength'
, then as many space characters (' '
) as are needed to ensure that the returned'String'
has length equal to'totalStringLength'
will be prepended to the inputString
parameter's'
.
Ifs.length()
is greater than'totalStringLength'
, then the original input shall be returned.- Throws:
java.lang.IllegalArgumentException
- IftotalStringLength
is zero or negative.- See Also:
rightSpacePad(String, int)
- Code:
- Exact Method Body:
if (totalStringLength <= 0) throw new IllegalArgumentException( "totalString length was '" + totalStringLength + ", " + "however it is expected to be a positive integer." ); return (s.length() >= totalStringLength) ? s : String.format("%1$" + totalStringLength + "s", s);
-
rightSpacePad
public static java.lang.String rightSpacePad(java.lang.String s, int totalStringLength)
Primarily for convenience in remembering earlier C-styleprintf(...)
formatting commands.
This method will "right pad" an inputString
with spaces, ifs.length() < totalStrLength
. If input-parameter's'
is equal-to or longer-than the value in'totalStringLength'
, then the originalString
shall be returned.Input Parameters Returned String "Quick Brown Fox"
20
"Quick Brown Fox "
"Hello World"
15
"Hello World "
"Write Once, Run Anywhere"
10
"Write Once, Run Anywhere"
null
NullPointerException
- Parameters:
s
- This may be anyjava.lang.String
totalStringLength
- Ifs.length()
is smaller than'totalStringLength'
, then as many space characters (' '
) as are needed to ensure that the returned'String'
has length equal to'totalStringLength'
will be postpended to the inputString
parameter's'
.
Ifs.length()
is greater than'totalStringLength'
, then the original input shall be returned.- Throws:
java.lang.IllegalArgumentException
- IftotalStringLength
is zero or negative.- See Also:
leftSpacePad(String, int)
- Code:
- Exact Method Body:
if (totalStringLength <= 0) throw new IllegalArgumentException( "totalString length was '" + totalStringLength + "', " + "however it is expected to be a positive integer." ); return (s.length() >= totalStringLength) ? s : String.format("%1$-" + totalStringLength + "s", s);
-
getAllMatches
public static java.util.regex.MatchResult[] getAllMatches (java.lang.String s, java.util.regex.Pattern regEx, boolean eliminateOverlappingMatches)
Runs a Regular-Expression over aString
to retrieve all matches that occur between inputString
parameter's'
and Regular-Expression'regEx'
.- Parameters:
s
- Any JavaString
regEx
- Any Java Regular-ExpressioneliminateOverlappingMatches
- When this parameter is passed'TRUE'
, successive matches that have portions which overlap each-other are eliminated.- Returns:
- An array of all
MatchResult's
(from package'java.util.regex.*'
) that were produced by iterating theMatcher's
'find()'
method. - Code:
- Exact Method Body:
Stream.Builder<MatchResult> b = Stream.builder(); Matcher m = regEx.matcher(s); int prevEnd = 0; while (m.find()) { MatchResult matchResult = m.toMatchResult(); // This skip any / all overlapping matches - if the user has requested it if (eliminateOverlappingMatches) if (matchResult.start() < prevEnd) continue; b.accept(matchResult); prevEnd = matchResult.end(); } // Convert the Java-Stream into a Java-Array and return the result return b.build().toArray(MatchResult[]::new);
-
setChar
public static java.lang.String setChar(java.lang.String str, int i, char c)
This sets a character in aString
to a new value, and returns a result- Parameters:
str
- Any javaString
i
- An index into the underlying character array of thatString
.c
- A new character to be placed at the i'th position of thisString
.- Returns:
- a new java
String
, with the appropriate index into theString
substituted using character parameter'c'
. - Code:
- Exact Method Body:
return ((i + 1) < str.length()) ? (str.substring(0, i) + c + str.substring(i + 1)) : (str.substring(0, i) + c);
-
delChar
public static java.lang.String delChar(java.lang.String str, int i)
This removes a character from aString
, and returns a newString
as a result.- Parameters:
str
- Any Java-String
.i
- This is the index into the underlying javachar
-array whose character will be removed from the returnString
.- Returns:
- Since Java
String
's are all immutable, thisString
that is returned is completely new, with the character that was originally at index 'i' removed. - Code:
- Exact Method Body:
if ((i + 1) < str.length()) return str.substring(0, i) + str.substring(i + 1); else return str.substring(0, i);
-
removeDuplicateSpaces
public static java.lang.String removeDuplicateSpaces(java.lang.String s)
Returns the sameString
is input, but trims all spaces down to a single space. Each and every lone / independent or contiguous white-space character is reduced to a single space-character.Input String Output String "This has extra spaces\n"
"This has extra spaces "
"This does not"
"This does not"
"\tThis\nhas\ttabs\nand\tnewlines\n"
" This has tabs and newlines "
- Parameters:
s
- Any JavaString
- Returns:
- A
String
where all white-space is compacted to a single space. This is generally how HTML works, when it is displayed in a browser. - Code:
- Exact Method Body:
return StringParse.WHITE_SPACE_REGEX.matcher(s).replaceAll(" ");
-
removeWhiteSpace
public static java.lang.String removeWhiteSpace(java.lang.String s)
This string-modify method simply removes any and all white-space matches found within a java-String
.Input String Output String "This Has Extra Spaces\n"
"ThisHasExtraSpaces"
"This Does Not"
"ThisDoesNot"
"\tThis\nHas\tTabs\nAnd\tNewlines\n"
"ThisHasTabsAndNewlines"
- Parameters:
s
- AnyString
, but if it has any white-space (space that matches regular-expression:\w+
) then those character-blocks will be removed- Returns:
- A new
String
without any\w
(RegEx for 'whitespace') - See Also:
WHITE_SPACE_REGEX
- Code:
- Exact Method Body:
return WHITE_SPACE_REGEX.matcher(s).replaceAll("");
-
nChars
public static java.lang.String nChars(char c, int n)
Generates aString
that containsn
copies of characterc
.- Returns:
n
copies ofc
, as aString
.- Throws:
java.lang.IllegalArgumentException
- If the value passed to parameter'n'
is negative- See Also:
StrSource.caretBeneath(String, int)
- Code:
- Exact Method Body:
if (n < 0) throw new IllegalArgumentException("Value of parameter 'n' is negative: " + n); char[] cArr = new char[n]; Arrays.fill(cArr, c); return new String(cArr);
-
nStrings
public static java.lang.String nStrings(java.lang.String s, int n)
Generates aString
that containsn
copies ofs
.- Returns:
n
copies ofs
as aString
.- Throws:
NException
- if the value provided to parameter'n'
is negative.- Code:
- Exact Method Body:
if (n < 0) throw new NException("A negative value was passed to 'n' [" + n + ']'); StringBuilder sb = new StringBuilder(); for (int i=0; i < n; i++) sb.append(s); return sb.toString();
-
hasWhiteSpace
public static boolean hasWhiteSpace(java.lang.String s)
This method checks whether or not a java-String
has white-space.- Parameters:
s
- Any Java-String
. If thisString
has any white-space, this method will returnTRUE
- Returns:
TRUE
If there is any white-space in this method, andFALSE
otherwise.- See Also:
WHITE_SPACE_REGEX
- Code:
- Exact Method Body:
return WHITE_SPACE_REGEX.matcher(s).find();
-
countCharacters
public static int countCharacters(java.lang.String s, char c)
Counts the number of instances of character inputchar c
contained by the inputString s
- Parameters:
s
- AnyString
containing any combination of ASCII/UniCode charactersc
- Any ASCII/UniCode character.- Returns:
- The number of times
char c
occurs inString s
- Code:
- Exact Method Body:
int count = 0; int pos = 0; while ((pos = s.indexOf(c, pos + 1)) != -1) count++; return count;
-
ifQuotesStripQuotes
public static java.lang.String ifQuotesStripQuotes(java.lang.String s)
If theString
passed to this method contains a single-quote on both sides of theString
, or if it contains a double-quote on both sides of thisString
, then this method shall return a newString
that is shorter in length by 2, and leaves off the first and last characters of the input parameterString
.
HOPEFULLY, The name of this method explains clearly what this method does- Parameters:
s
- This may be any javaString
. OnlyString's
whose first and last characters are not only quotation marks (single or double), but also they are the same, identical, quotation marks on each side.- Returns:
- A new
String
that whose first and last quotation marks are gone - if they were there when this method began. - Code:
- Exact Method Body:
if (s == null) return null; if (s.length() < 2) return s; int lenM1 = s.length() - 1; // Position of the last character in the String if ( ((s.charAt(0) == '\"') && (s.charAt(lenM1) == '\"')) // String has Double-Quotation-Marks || // ** or *** ((s.charAt(0) == '\'') && (s.charAt(lenM1) == '\'')) ) // String has Single-Quotation-Marks return s.substring(1, lenM1); else return s;
-
numLines
public static int numLines(java.lang.String text)
Counts the number of lines of text inside of a JavaString
.- Parameters:
text
- This may be any text, as aString
.- Returns:
- Returns the number of lines of text. The integer returned shall be precisely
equal to the number of
'\n'
characters plus one! - Code:
- Exact Method Body:
if (text.length() == 0) return 0; int pos = -1; int count = 0; do { pos = text.indexOf('\n', pos + 1); count++; } while (pos != -1); return count;
-
monthStr
public static java.lang.String monthStr(int month)
Converts an integer into a Month. I could just use the classjava.util.Calendar
, but it is so complicated, that using an internal list is easier.- Parameters:
month
- The month, as a number from'1'
to'12'
.- Returns:
- A month as a
String
like:"January"
or"August"
- See Also:
months
- Code:
- Exact Method Body:
return months.get(month);
-
dateStr
public static java.lang.String dateStr()
Generates a "Date String" using the character separator'.'
- Returns:
- A
String
in the form:YYYY.MM.DD
- Code:
- Exact Method Body:
return dateStr('.', false);
-
dateStr
public static java.lang.String dateStr(char separator)
Generates a "Date String" using the separator parameter as the separator between numbers- Parameters:
separator
- Any ASCII or UniCode character.- Returns:
- A
String
of the form:YYYYcMMcDD
where'c'
is the passed'separator'
parameter. - Code:
- Exact Method Body:
return dateStr(separator, false);
-
dateStrGOVCN
public static java.lang.String dateStrGOVCN()
Generates a "Date String" that is consistent with the directory-name file-storage locations used to store articles fromhttp://Gov.CN
.- Returns:
- The
String's
used for the Chinese Government Web-Portal Translation Pages - Code:
- Exact Method Body:
return dateStr('/', false).replaceFirst("/", "-");
-
dateStr
public static java.lang.String dateStr(char separator, boolean includeMonthName)
This class is primary included because although Java has a pretty reasonable "general purpose" calendar class/interface, but a consistent / sameString
since is needed because the primary use here is for building the names of files.- Parameters:
separator
- Any ASCII or Uni-Code character.includeMonthName
- When TRUE, the English-Name of the month ('January'
...'December'
) will be appended to the month number in the returnedString
.- Returns:
- The year, month, and day as a
String
. - Code:
- Exact Method Body:
Calendar c = internalCalendar; String m = zeroPad10e2(c.get(Calendar.MONTH) + 1); // January is month zero! String d = zeroPad10e2(c.get(Calendar.DAY_OF_MONTH)); if (includeMonthName) m += " - " + c.getDisplayName(Calendar.MONTH, 2, Locale.US); if (separator != 0) return c.get(Calendar.YEAR) + "" + separator + m + separator + d; else return c.get(Calendar.YEAR) + "" + m + d;
-
ymDateStr
public static java.lang.String ymDateStr()
Returns aString
that has the year and the month (but not the day, or other time components).- Returns:
- Returns the current year and month as a
String
. - Code:
- Exact Method Body:
return ymDateStr('.', false);
-
ymDateStr
public static java.lang.String ymDateStr(char separator)
Returns aString
that has the year and the month (but not the day, or other time components).- Parameters:
separator
- The single-character separator used between year, month and day.- Returns:
- The current year and month as a
String
. - Code:
- Exact Method Body:
return ymDateStr(separator, false);
-
ymDateStr
public static java.lang.String ymDateStr(char separator, boolean includeMonthName)
Returns aString
that has the year and the month (but not the day, or other time components).- Parameters:
separator
- The single-character separator used between year, month and day.includeMonthName
- When this is true, the name of the month, in English, is included with the returnString
.- Returns:
- YYYYseparatorMM(? include-month-name)
- Code:
- Exact Method Body:
Calendar c = internalCalendar; String m = zeroPad10e2(c.get(Calendar.MONTH) + 1); // January is month zero! if (includeMonthName) m += " - " + c.getDisplayName(Calendar.MONTH, 2, Locale.US); if (separator != 0) return c.get(Calendar.YEAR) + "" + separator + m; else return c.get(Calendar.YEAR) + "" + m;
-
timeStr
public static java.lang.String timeStr()
Returns the current time as aString
.- Returns:
- military time - with AM|PM (redundant) added too.
Includes only Hour and Minute - separated by a colon character
':'
- See Also:
timeStr(char)
- Code:
- Exact Method Body:
return timeStr(':');
-
timeStr
public static java.lang.String timeStr(char separator)
Returns the current time as aString
.- Parameters:
separator
- The character used to separate the minute & hour fields- Returns:
- military time - with AM|PM added redundantly, and a separator of your choosing.
- Code:
- Exact Method Body:
Calendar c = internalCalendar; int ht = c.get(Calendar.HOUR) + ((c.get(Calendar.AM_PM) == Calendar.AM) ? 0 : 12); String h = zeroPad10e2((ht == 0) ? 12 : ht); // 12:00 is represented as "0"... changes this... String m = zeroPad10e2(c.get(Calendar.MINUTE)); String p = (c.get(Calendar.AM_PM) == Calendar.AM) ? "AM" : "PM"; if (separator != 0) return h + separator + m + separator + p; else return h + m + p;
-
timeStrComplete
public static java.lang.String timeStrComplete()
Returns the current time as aString
. This method uses all time components available.- Returns:
- military time - with AM|PM added redundantly.
- Code:
- Exact Method Body:
Calendar c = internalCalendar; int ht = c.get(Calendar.HOUR) + ((c.get(Calendar.AM_PM) == Calendar.AM) ? 0 : 12); String h = zeroPad10e2((ht == 0) ? 12 : ht); // 12:00 is represented as "0" String m = zeroPad10e2(c.get(Calendar.MINUTE)); String s = zeroPad10e2(c.get(Calendar.SECOND)); String ms = zeroPad(c.get(Calendar.MILLISECOND)); String p = (c.get(Calendar.AM_PM) == Calendar.AM) ? "AM" : "PM"; return h + '-' + m + '-' + p + '-' + s + '-' + ms + "ms";
-
ordinalIndicator
public static java.lang.String ordinalIndicator(int i)
The words "ordinal indicator" are referring to the little characterString
that is often used in English to make a number seem more a part of an english sentence.- Parameters:
i
- Any positive integer (greater than 0)- Returns:
- This will return the following strings:
Input: RETURNS: i = 1 "st" (as in "1st","first") i = 2 "nd" (as in "2nd", "second") i = 4 "th" (as in "4th") i = 23 "rd" (as in "23rd") - Throws:
java.lang.IllegalArgumentException
- If i is negative, or zero- Code:
- Exact Method Body:
if (i < 1) throw new IllegalArgumentException("i: " + i + "\tshould be a natural number > 0."); // Returns the last 2 digits of the number, or the number itself if it is less than 100. // Any number greater than 100 - will not have the "text-ending" (1st, 2nd, 3rd..) affected // by the digits after the first two digits. Just analyze the two least-significant digits i = i % 100; // All numbers between "4th" and "19th" end with "th" if ((i > 3) && (i < 20)) return "th"; // set i to be the least-significant digit of the number - if that number was 1, 2, or 3 i = i % 10; // Obvious: English Rules. if (i == 1) return "st"; if (i == 2) return "nd"; if (i == 3) return "rd"; // Compiler is complaining. This statement should never be executed. return "th";
-
zeroPad
public static java.lang.String zeroPad(int n)
This just zero-pads integers with "prepended" zero's. java.text has all kinds of extremely intricate zero-padding and text-formatting classes. However, here, these are generally used for debug, line-number, or count information that is printed to the UNIX terminal. When this is the case, a simple and easily remembered 'one line method' is a lot more useful than all of the highly-scalable versions of the text-formatting classes in java.text.- Parameters:
n
- Any Integer. If'n'
is negative or greater than 1,000 - then null is returned.- Returns:
- A zero-padded
String
- to precisely three orders of 10, as in the example table below:Input RETURNS: n = 9 "009" n = 99 "099" n = 999 "999" n = 9999 null n = -10 null - See Also:
zeroPad10e2(int)
,zeroPad10e4(int)
- Code:
- Exact Method Body:
if (n < 0) return null; if (n < 10) return "00" + n; if (n < 100) return "0" + n; if (n < 1000) return "" + n; return null;
-
zeroPad10e2
public static java.lang.String zeroPad10e2(int n)
Pads an integer such that it contains enough leading zero's to ensure a String-length of two.- Parameters:
n
- Must be an integer between 0 and 99, or else null will be returned- Returns:
- A zero-padded String of the integer, to precisely two orders of
10
. Null is returned if the number cannot fit within two spaces. Example table follows:Input RETURNS: n = 9 "09" n = 99 "99" n = 999 null n = -10 null - See Also:
zeroPad(int)
- Code:
- Exact Method Body:
if (n < 0) return null; if (n < 10) return "0" + n; if (n < 100) return "" + n; return null;
-
zeroPad10e4
public static java.lang.String zeroPad10e4(int n)
Pads an integer such that it contains enough leading zero's to ensure a String-length of four.- Parameters:
n
- Must be an integer between 0 and 9999, or else null will be returned- Returns:
- A zero-padded String of the integer, to precisely four orders of 10.
Null is returned if the number cannot fit within four spaces. Example table follows:
Input RETURNS: n = 9 "0009" n = 99 "0099" n = 999 "0999" n = 9999 "9999" n = 99999 null n = -10 null - See Also:
zeroPad(int)
- Code:
- Exact Method Body:
if (n < 0) return null; if (n < 10) return "000" + n; if (n < 100) return "00" + n; if (n < 1000) return "0" + n; if (n < 10000) return "" + n; return null;
-
zeroPad
public static java.lang.String zeroPad(int n, int powerOf10)
Pad's an integer with leading zeroes into aString
. The number of zeroes padded is equal to parameter'powerOf10'
. Ifint 'powerOf10'
were equal to zero, then any integer passed to this function would return aString
that was precisely three characters long. If the value of parameterint 'n'
were larger than1,000
or negative, then null would be returned.- Parameters:
n
- Must be an integer between'0'
and'9999'
where the number of'9'
digits is equal to the value of parameterint 'powerOf10'
powerOf10
- This must be a positive integer greater than'1'
. It may not be larger'11'
. The largest value that any integer in Java may attain is'2,147,483, 647'
- Returns:
- A zero padded
String
. If a negative number is passed to parameter'n'
, then 'null' shall be returned. Null shall also be returned if the "Power of 10 Exponent of parametern
" is greater than the integer-value of parameter'powerOf10'
FOR INSTANCE: a call to:zeroPad(54321, 4);
would return null since the value of parameter'n'
has five-decimal-places, but'powerOf10'
is only 4! - Throws:
java.lang.IllegalArgumentException
- if the value parameter'powerOf10'
is less than 2, or greater than11
.- Code:
- Exact Method Body:
if (n < 0) return null; // Negative Values of 'n' not allowed char[] cArr = new char[powerOf10]; // The String's length will be equal to 'powerOf10' String s = "" + n; // (or else 'null' would be returned) int i = powerOf10 - 1; // Internal Loop variable int j = s.length() - 1; // Internal Loop variable Arrays.fill(cArr, '0'); // Initially, fill the output char-array with all // zeros while ((i >= 0) && (j >= 0)) // Now start filling that char array with the cArr[i--] = s.charAt(j--); // actual number if (j >= 0) return null; // if all of parameter 'n' was inserted into the // output (number 'n' didn't fit) then powerOf10 // was insufficient, so return null. return new String(cArr);
-
findLastFrontSlashPos
public static int findLastFrontSlashPos(java.lang.String urlOrDir)
This function finds the position of the last "front-slash" character'/'
in a java-String
- Parameters:
urlOrDir
- This is any java-String
, but preferably one that is aURL
, or directory.- Returns:
- The
String
-index of the last 'front-slash''/'
position in aString
, or-1
if there are not front-slashes. - Code:
- Exact Method Body:
return urlOrDir.lastIndexOf('/');
-
fromLastFrontSlashPos
public static java.lang.String fromLastFrontSlashPos (java.lang.String urlOrDir)
This returns the contents of aString
, after the last front-slash found.
NOTE: If not front-slash'/'
character is found, then the originalString
is returned.- Parameters:
urlOrDir
- This is any java-String
, but preferably one that is aURL
, or directory.- Returns:
- the portion of the
String
after the final front-slash'/'
character. If there are no front-slash characters found in thisString
, then the originalString
shall be returned. - Code:
- Exact Method Body:
int pos = urlOrDir.lastIndexOf('/'); if (pos == -1) return urlOrDir; return urlOrDir.substring(pos + 1);
-
beforeLastFrontSlashPos
public static java.lang.String beforeLastFrontSlashPos (java.lang.String urlOrDir)
This returns the contents of aString
, before the last front-slash found (including the front-slash'/'
itself).
NOTE: If no front-slash'/'
character is found, then null is returned.- Parameters:
urlOrDir
- This is any java-String
, but preferably one that is aURL
, or directory.- Returns:
- the portion of the
String
before and including the final front-slash'/'
character. If there are no front-slash characters found in thisString
, then null. - Code:
- Exact Method Body:
int pos = urlOrDir.lastIndexOf('/'); if (pos == -1) return null; return urlOrDir.substring(0, pos + 1);
-
findLastFileSeparatorPos
public static int findLastFileSeparatorPos(java.lang.String fileOrDir)
This function finds the position of the last'java.io.File.separator'
character in a java-String
. In UNIX-based systems, this is a forward-slash'/'
character, but in Windows-MSDOS, this is a back-slash'\'
character. Identifying which of the two is used is obtained by "using" Java'sFile.separator
class and field.- Parameters:
fileOrDir
- This may be any Java-String
, but preferably one that represents a file or directory.- Returns:
- The
String
-index of the last 'file-separator' position in aString
, or-1
if there are no such file-separators. - Code:
- Exact Method Body:
return fileOrDir.lastIndexOf(File.separator.charAt(0));
-
fromLastFileSeparatorPos
public static java.lang.String fromLastFileSeparatorPos (java.lang.String fileOrDir)
This returns the contents of aString
, after the last'java.io.File.separator'
found.
NOTE: If no'java.io.File.separator'
character is found, then the originalString
is returned.- Parameters:
fileOrDir
- This is any java-String
, but preferably one that is a filename or directory-name- Returns:
- the portion of the
String
after the final'java.io.File.separator'
character. If there are no such characters found, then the originalString
shall be returned. - Code:
- Exact Method Body:
int pos = fileOrDir.lastIndexOf(File.separator.charAt(0)); if (pos == -1) return fileOrDir; return fileOrDir.substring(pos + 1);
-
beforeLastFileSeparatorPos
public static java.lang.String beforeLastFileSeparatorPos (java.lang.String urlOrDir)
This returns the contents of aString
, before the last'java.io.File.separator'
(including the separator itself).
NOTE: If no'java.io.File.separator'
character is found, then null is returned.- Parameters:
urlOrDir
- This is any java-String
, but preferably one that is aURL
, or directory.- Returns:
- the portion of the
String
before and including the final'java.io.File.separator'
character. If there are no such characters found in thisString
, then null is returned. - Code:
- Exact Method Body:
int pos = urlOrDir.lastIndexOf(File.separator.charAt(0)); if (pos == -1) return null; return urlOrDir.substring(0, pos + 1);
-
swapExtension
public static java.lang.String swapExtension (java.lang.String fileNameOrURLWithExtension, java.lang.String newExtension)
This method swaps the ending 'File Extension' with another, parameter-provided, extension.- Parameters:
fileNameOrURLWithExtension
- Any file-name (orURL
) that has an extension.newExtension
- The file orURL
extension used as a substitute for the old extension. ThisString
may begin with the dot / period character ('.'
), and if it does not, one wil be appended.- Returns:
- The new file-name or
URL
having the substituted extension. - Throws:
StringFormatException
- If theString
passed does not have any'.'
(period) characters, then this exception will throw.
CAUTION: In lieu of an exhaustive check on whether or not the input file-name is a valid name, this method will simply check for the presence or absence of a period-character ('.'
). Checking the validity of the input name is far beyond the scope of this method.
ALSO: This method shall check to ensure that the'newExtension'
parameter does not have length zero.
To remove a file-extension, useremoveExtension(String)
- Code:
- Exact Method Body:
int dotPos = fileNameOrURLWithExtension.lastIndexOf('.'); if (dotPos == -1) throw new StringFormatException( "The file-name provided\n[" + fileNameOrURLWithExtension + "]\n" + "does not have a file-extension" ); if (newExtension.length() == 0) throw new StringFormatException( "The new file-name extension has length 0. " + " To remove an extension, use 'StringParse.removeFileExtension(fileName)'" ); return (newExtension.charAt(0) == '.') ? fileNameOrURLWithExtension.substring(0, dotPos) + newExtension : fileNameOrURLWithExtension.substring(0, dotPos) + '.' + newExtension;
-
removeExtension
public static java.lang.String removeExtension (java.lang.String fileNameOrURL)
This method simply removes all character data after the last identified period character ('.'
) found withinfileNameOrURL
.
If the input-String
does not have a period-character, the originalString
will be returned, unmodified.- Parameters:
fileNameOrURL
- Any file-name orURL
, as aString
.- Returns:
- The modified file-name, or
URL
, as aString
.
NOTE: No validity checks of any kind are performed on'fileNameOrURL'
. This method merely checks for the presence or absence of a'.'
(period-character), and if it finds one, removes everything after-and-including the last-period. - Code:
- Exact Method Body:
int dotPos = fileNameOrURL.lastIndexOf('.'); if (dotPos == -1) return fileNameOrURL; return fileNameOrURL.substring(0, dotPos);
-
findExtension
public static int findExtension(java.lang.String file, boolean includeDot)
This will return the location within aString
where the last period ('.'
) is found.
ALSO: No validity checks for valid file-system names are performed. Rather, the portion of the input-String
starting at the location of the last period is returned, regardless of what theString
contains.- Parameters:
file
- This may be any Java-String
, but preferably one that represents a file.includeDot
- When this parameter is passedTRUE
, the position-index that is returned will be the location of the last index where a period ('.'
) is found. WhenFALSE
, the index returned will be the location of that period+ 1
.- Returns:
- This will return the location of the file-extension. If no period is found, then
-1
is returned. If the period is the lastchar
in theString
, and parameter'includeDot'
isFALSE
, then-1
is returned. - Code:
- Exact Method Body:
int pos = file.lastIndexOf('.'); if (pos == -1) return -1; if (includeDot) return pos; pos++; return (pos < file.length()) ? pos : -1;
-
fromExtension
public static java.lang.String fromExtension(java.lang.String file, boolean includeDot)
This returns the contents of aString
, after the last period'.'
in thatString
. For file-system and web files, this is often referred to as the file extension.
NOTE: If no period'.'
character is found, then null is returned.
ALSO: No validity checks for valid file-system names are performed. Rather, the portion of the input-String
starting at the location of the last period is returned, regardless of what theString
contains.- Parameters:
file
- This is any java-String
, but preferably one that is a filename.includeDot
- This determines whether the period'.'
is to be included in the returned-String
.- Returns:
- the portion of the
String
after the final period'.'
character. If parameterincludeDot
has been passedFALSE
, then the portion of the input-String
beginning after the last period is returned.
If there are no period characters found in thisString
, then null is returned. - Code:
- Exact Method Body:
int pos = findExtension(file, includeDot); if (pos == -1) return null; return file.substring(pos);
-
beforeExtension
public static java.lang.String beforeExtension(java.lang.String file)
This returns the contents of aString
, before the last period'.'
in thatString
. For file-system and web files, this is often referred to as the file extension.
NOTE: If no period'.'
character is found, then the originalString
is returned.
ALSO: No validity checks for valid file-system names are performed. Rather, the portion of the input-String
starting at the location of the last period is returned, regardless of what theString
contains.- Parameters:
file
- This is any java-String
, but preferably one that is a filename.- Returns:
- the portion of the
String
before the final period'.'
character.
If there are no period characters found in thisString
, then the original file is returned. - Code:
- Exact Method Body:
int pos = file.lastIndexOf('.'); if (pos == -1) return file; return file.substring(0, pos);
-
findURLRoot
public static java.lang.String findURLRoot(java.lang.String url)
This function returns the root URL-directory of aString
SPECIFICALLY: it searches for the "last forward slash" in aString
, and returns a substring from position 0 to that point. If there aren't any forward slashes in thisString
, null is returned. The front-slash itself is included in the returnedString
.
NOTE: It is similar to the old MS-DOS call to "DIR PART"- Parameters:
url
- AnyString
that is intended to be an "Internet URL" - usually http://domain/directory/[file]- Returns:
- substring(0, index of last front-slash (
'/'
) inString
) - Code:
- Exact Method Body:
int pos = findLastFrontSlashPos(url); if (pos == -1) return null; else return url.substring(0, pos + 1);
-
firstWord
public static java.lang.String firstWord(java.lang.String s)
- Returns:
- After breaking the
String
by white-space, this returns the first 'chunk' before the first whitespace. - Code:
- Exact Method Body:
int pos = s.indexOf(" "); if (pos == -1) return s; else return s.substring(0, pos);
-
removeBrackets
public static java.lang.String removeBrackets(java.lang.String s)
This function will remove any pairs of Brackets within aString
, and returned the paired downString
- Parameters:
s
- AnyString
, which may or may not contain a "Bracket Pair"
For Example:
- This
String
does contain [a pair of brackets] within! - But this
String
does not.
- This
- Returns:
- The same
String
, but with any bracket-pairs removed. - Code:
- Exact Method Body:
return remove_(s, '[', ']');
-
removeBraces
public static java.lang.String removeBraces(java.lang.String s)
Functions the same asremoveBrackets(String)
- but removes pairs of curly-braces, instead
NOTE:These are { curly braces } that will be removed by thisString
!- Parameters:
s
- Any validString
{ such as } - (even thisString
).
For Example:
- This
String
does contain {a pair of curly-braces} within! - But this
String
does not.
- This
- Returns:
- The same
String
, but with any curly-brace-pairs removed. - See Also:
removeBrackets(String)
- Code:
- Exact Method Body:
return remove_(s, '{', '}');
-
removeParens
public static java.lang.String removeParens(java.lang.String s)
Removes Parenthesis, similar to other parenthetical removing functions.- Parameters:
s
- Any (valid)String
. Below are sample inputs:- This
String
does contain (a pair of parenthesis) within! - But this
String
does not.
- This
- Returns:
- The same
String
, but with any parenthesis removed. - See Also:
removeBrackets(String)
- Code:
- Exact Method Body:
return remove_(s, '(', ')');
-
removeAllParenthetical
public static java.lang.String removeAllParenthetical(java.lang.String s)
Removes all parenthetical notations. Calls all remove functions- Parameters:
s
- Any valid string- Returns:
- The same string, but with all parenthesis, curly-brace & bracket pairs removed.
- See Also:
removeParens(String)
,removeBraces(String)
,removeBrackets(String)
- Code:
- Exact Method Body:
return removeParens(removeBraces(removeBrackets(s)));
-
objToB64Str
public static java.lang.String objToB64Str(java.lang.Object o) throws java.io.IOException
This will convert any Serializable Java Object into a base-64 String. ThisString
may be saved, transmitted, even e-mailed to another party, if you wish and decoded else-where.
REQUIREMENTS:- Object must implement the
interface java.io.Serializable
- Receiving party or storage-device must have access to the
.jar file, or .class file(s)
needed to instantiate that object! (You must have shared your classes if you intend to let other people de-serialize instances of that class)
- Parameters:
o
- Any javajava.lang.Object
. This object must be Serializable, or else the code will generate an exception.- Returns:
- A
String
version of this object. It will be:- Serialized using the
java.io.ObjectOutputStream(...)
object-serialization method - Compressed using the
java.io.GZIPOutputStream(...)
stream-compression method - Encoded to a
String
, via Base-64 Encodingjava.util.Base64.getEncoder()
NOTE: Compression does not always make much difference, however often times when doing web-scraping projects, there are large Javajava.util.Vector<String>
filled with many lines of text, and these lists may be instantly and easily saved using object-serialization. Furthermore, in these cases, the compression will sometimes reduce file-size by an order of magnitude. - Serialized using the
- Throws:
java.io.IOException
- See Also:
b64StrToObj(String)
- Code:
- Exact Method Body:
ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos); ObjectOutputStream oos = new ObjectOutputStream(gzip); oos.writeObject(o); oos.flush(); gzip.finish(); oos.close(); bos.close(); return Base64.getEncoder().encodeToString(bos.toByteArray());
- Object must implement the
-
b64StrToObj
public static java.lang.Object b64StrToObj(java.lang.String str) throws java.io.IOException
This converts to any java.io.Serializable object from a compressed, serialized, Base-64 Encodedjava.lang.String
. This method can be thought of as one which converts objects which have been previously encoded as aString
, and possibly even transmitted across the internet, back into an JavaObject
.
REQUIREMENTS: TheObject
that is to be instantiated must have its class files accessible to the class-loader. This is the exact-same requirement expected by all Java "de-serializations" routines.- Parameters:
str
- Any previously Base-64 encoded, serialized, compressedjava.lang.Object'
that has been saved as aString
. ThatString
should have been generated using theProgramming.objToB64Str(Object o)
method in this class.- Serialized using the
java.io.ObjectOutputStream(...)
object-serialization method - Compressed using the
java.io.GZIPOutputStream(...)
sream-compression method - Encoded to a
String
, via Base-64 Encodingjava.util.Base64.getEncoder()
NOTE: Compression does not always make much difference, however often times when doing web-scraping projects, there are large Javajava.util.Vector<String>
filled with many lines of text, and these lists may be instantly and easily saved using object-serialization. Furthermore, in these cases, the compression will sometimes reduce file-size by an order of magnitude.- Serialized using the
- Returns:
- The de-compressed
java.lang.Object
converted back from aString
. - Throws:
java.io.IOException
- See Also:
objToB64Str(Object)
- Code:
- Exact Method Body:
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(str)); GZIPInputStream gzip = new GZIPInputStream(bis); ObjectInputStream ois = new ObjectInputStream(gzip); Object ret = null; try { ret = ois.readObject(); } catch (ClassNotFoundException e) { throw new IOException( "There were no serialized objects found in your String. See e.getCause();", e ); } bis.close(); ois.close(); return ret;
-
objToB64MimeStr
public static java.lang.String objToB64MimeStr(java.lang.Object o) throws java.io.IOException
This performs an identical operation as the method:objToB64Str
, however it generates an outputString
that is "MIME" compatible. All this means is that theString
itself - which could conceivable by thousands or even hundreds of thousands of characters long - will havenew-line characters
inserted such that it may be printed on paper or included in a text-file that is (slightly) more human-readable. Base64 MIME encodedString's
look like very long paragraphs of random-text data, while regular Base64-encodings are a single, very-long,String
with no space characters.- Parameters:
o
- Anyjava.lang.Object
. This object must be Serializable, or else the code will generate an exception.- Returns:
- A Base-64 MIME Encoded
String
version of any serializablejava.lang.Object
. - Throws:
java.io.IOException
- See Also:
objToB64Str(Object)
,b64MimeStrToObj(String)
- Code:
- Exact Method Body:
ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos); ObjectOutputStream oos = new ObjectOutputStream(gzip); oos.writeObject(o); oos.flush(); gzip.finish(); oos.close(); bos.close(); return Base64.getMimeEncoder().encodeToString(bos.toByteArray());
-
b64MimeStrToObj
public static java.lang.Object b64MimeStrToObj(java.lang.String str) throws java.io.IOException
This performs an identical operation as the method:b64StrToObj
, however receives a "MIME" compatible encodedString
. All this means is that theString
itself - which could conceivable by thousands or even hundreds of thousands of characters long - will havenew-line characters
inserted such that it may be printed on paper or included in a text-file that is (slightly) more human-readable. Base64 MIME encodedString's
look like very long paragraphs of random-text data, while regular Base64 encodings a single, very-long,String's
.- Returns:
- The (de-serialized) java object that was read from the input parameter
String 'str'
REQUIREMENTS: The object that is to be instantiated must have its class files accessible to the class-loader. This is the exact-same requirement expected by all Java "de-serializations" routines. - Throws:
java.io.IOException
- See Also:
b64StrToObj(String)
,objToB64MimeStr(Object)
- Code:
- Exact Method Body:
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getMimeDecoder().decode(str)); GZIPInputStream gzip = new GZIPInputStream(bis); ObjectInputStream ois = new ObjectInputStream(gzip); Object ret = null; try { ret = ois.readObject(); } catch (ClassNotFoundException e) { throw new IOException( "There were no serialized objects found in your String. See e.getCause();", e ); } bis.close(); ois.close(); return ret;
-
dotDots
public static java.lang.String dotDots(java.lang.String fileName, java.lang.String ancestorDirectory, char separator)
Computes a "relativeURL String
".- Parameters:
fileName
- This is a fileName whose ancestor directory needs to be 'relative-ised'ancestorDirectory
- This is an ancestor (container) directory.separator
- The separator character used to separate file-system directory names.- Returns:
- This shall return the "../.." structure needed to insert a relative-
URL
or link into a web-page. - Throws:
java.lang.IllegalArgumentException
- This exception shall throw if the separator character is not one of the standard file & directory separators: forward-slash'/'
or back-slash'\'
.
This exception also throws if theString
provided to parameter'fileName'
does not begin-with theString
provided to parameter'ancestorDirectory'
.- Code:
- Exact Method Body:
if ((separator != '/') && (separator != '\\')) throw new IllegalArgumentException( "The separator character provided to this method must be either a forward-slash '/' " + "or a back-slash ('\\') character. You have provided: ['" + separator + "']." ); if (! fileName.startsWith(ancestorDirectory)) throw new IllegalArgumentException( "The file-name you have provided [" + fileName + "] is a String that does " + "start with the ancestorDirectory String [" + ancestorDirectory + "]. " + "Therefore there is no relative path using the dot-dot construct to the named " + "ancestor directory fromm the directory where the named file resides." ); int levelsDeep = StringParse.countCharacters(fileName, separator) - StringParse.countCharacters(ancestorDirectory, separator); String dotDots = ""; while (levelsDeep-- > 0) dotDots = dotDots + ".." + separator; return dotDots;
-
dotDotParentDirectory
public static java.lang.String dotDotParentDirectory(java.net.URL url)
Convenience Method
Invokes:dotDotParentDirectory(String, char, short)
Converts:URL
toString
, eliminates non-essentialURI
-information (Such as:ASP, JSP, PHP Query-Strings, and others too
)
Passes:char '/'
, the separator character used inURL's
Passes:'1'
to parameter'nLevels'
- only going up on directory
-
dotDotParentDirectory
public static java.lang.String dotDotParentDirectory (java.lang.String urlAsStr)
Convenience Method
Invokes:dotDotParentDirectory(String, char, short)
Passes:char '/'
, the separator character used inURL's
Passes:'1'
to parameter'nLevels'
- only going up on directory
-
dotDotParentDirectory
public static java.lang.String dotDotParentDirectory(java.net.URL url, short nLevels)
Convenience Method
Invokes:dotDotParentDirectory(String, char, short)
Converts:URL
toString
, eliminates non-essentialURI
-information (Such as:ASP, JSP, PHP Query-Strings, and others too
)
Passes:char '/'
, the separator character used inURL's
-
dotDotParentDirectory
public static java.lang.String dotDotParentDirectory (java.lang.String urlAsStr, short nLevels)
Convenience Method
Invokes:dotDotParentDirectory(String, char, short)
Passes:char '/'
, the separator character used inURL's
-
dotDotParentDirectory
public static java.lang.String dotDotParentDirectory (java.lang.String directoryStr, char dirSeparator)
Convenience Method
Invokes:dotDotParentDirectory(String, char, short)
.
Passes:'1'
to parameternLevels
- only going up one directory.
-
dotDotParentDirectory
public static java.lang.String dotDotParentDirectory (java.lang.String directoryStr, char separator, short nLevels)
This does traverses up a directory-tree structure, and returns a 'parent-level' directory that is'nLevels'
up the tree.
NOTE: The character used as the "File Separator" and/or "Directory Separator" can be obtained using the field:java.io.File.Separator.charAt(0).
The classjava.io.File
provides access to the file-separator used by the file-system on which the JVM is currently running, although it treats it as a multi-characterString
. Just use the commonly-used java method'charAt(0)'
to obtain the forward-slash'/'
or backward-slash'\'
character.
IMPORTANT: There is no error-checking performed by this method regarding whether the inputString
represents a valid file or directory. Instead, this method just looks for the second from last separator-character (usually a'/'
forward-slash char) and returns a substring that starts at index 0, and continues to that position-plus-1 (in order to include that second-to-last separator char).- Parameters:
directoryStr
- This may be any java-String
, although it is expected to be on which represents the file & directory structure of file on the file-system. It may also beURL
for a web-siteseparator
- This is the separator currently used by that file & directory system. If trying to find the parent directory of aURL
, this should be the forward-slash character'/'
.nLevels
- This is how many "parent-level directories" (how many levels up the tree) need to be computed. This parameter must '1' or greater. If the passed parameter'directoryStr'
does not contain enough directories to traverse up the tree, then this method will throw anIllegalArgumentException
.- Returns:
- a
String
that represents 'nLevels' up the directory tree, either for a directory on the local-file system, or on a web-server from a Uniform Resource Locator. - Throws:
java.lang.IllegalArgumentException
- If the value of parametershort 'nLevels'
is negative, or does not identify a number consistent with the number of directories that are contained by the input urlAsStr parameter.
This exception shall also throw if the'separator'
character is not one of the standard file & directory separators: forward-slash'/'
or back-slash'\'
.- Code:
- Exact Method Body:
if (nLevels < 1) throw new IllegalArgumentException( "The parameter nLevels may not be less than 1, nor negative. You have passed: " + nLevels ); if ((separator != '/') && (separator != '\\')) throw new IllegalArgumentException( "The separator character provided to this method must be either a forward-slash '/' " + "or a back-slash ('\\') character. You have provided: ['" + separator + "']." ); int count = 0; for (int i=directoryStr.length() - 1; i >= 0; i--) if (directoryStr.charAt(i) == separator) if (++count == (nLevels + 1)) return directoryStr.substring(0, i + 1); throw new IllegalArgumentException( "The parameter nLevels was: " + nLevels + ", but unfortunately there only were: " + count + "'" + separator + "' characters found in the directory-string." );
-
isInteger
public static boolean isInteger(java.lang.String s)
Determines, efficiently, whether an inputString
is also an integer.
NOTE: A leading plus-sign ('+'
) will, in fact, generate aFALSE
return-value for this method.- Parameters:
s
- Any javaString
- Returns:
TRUE
if the inputString
is any integer, and false otherwise.
NOTE: This method does not check whether the number, itself, will actually fit into a field or variable of type'int'
. For example, the inputString '12345678901234567890'
(a very large integer), though an integer from a mathematical perspective, is not a valid java'int'
. In such cases,TRUE
is returned, but if Java'sInteger.parseInt
method were subsequently used, that method would throw an exception.
NOTE: The primary purpose of this method is to avoid having to writetry {} catch (NumberFormatException)
code-blocks. Furthermore, if only a check is desired, and theString
does not actually need to be converted to a number, this is also more efficient than actually performing the conversion.- See Also:
isInt(String)
- Code:
- Exact Method Body:
if (s == null) return false; int length = s.length(); if (length == 0) return false; int i = 0; if (s.charAt(0) == '-') { if (length == 1) return false; i = 1; } while (i < length) { char c = s.charAt(i++); if (c < '0' || c > '9') return false; } return true;
-
isInt
public static boolean isInt(java.lang.String s)
Convenience Method
Invokes:isOfPrimitiveType(String, char[])
Passes: The ASCII characters that compriseInteger.MIN_VALUE
-
isLong
public static boolean isLong(java.lang.String s)
Convenience Method
Invokes:isOfPrimitiveType(String, char[])
Passes: The ASCII characters that compriseLong.MIN_VALUE
-
isByte
public static boolean isByte(java.lang.String s)
Convenience Method
Invokes:isOfPrimitiveType(String, char[])
Passes: ASCII characters that compriseByte.MIN_VALUE
-
isShort
public static boolean isShort(java.lang.String s)
Convenience Method
Invokes:isOfPrimitiveType(String, char[])
Passes: ASCII characters that compriseShort.MIN_VALUE
-
isOfPrimitiveType
protected static boolean isOfPrimitiveType(java.lang.String s, char[] minArr)
Determines whether the inputString
is an integer in the range of Java's primitive type specified by an inputchar[]
array parameter. Specifically, if the the inputString
is both a mathematical integer, and also an integer in the range ofMIN_VALUE
andMAX_VALUE
for that primitive-type and then (and only then) willTRUE
be returned.
NOTE: The max and min values in which the range of valid integers must reside (for primitive-type'int'
, for instance) are as below:-2147483648
...2147483647
.
ALSO: A leading plus-sign ('+'
) will, in fact, generate aFALSE
return-value for this method.- Parameters:
s
- Any JavaString
minArr
- The value of a Java PrimitiveMIN_VALUE
, without the minus-sign, represented as achar[]
array.Primitive Type Integer as ASCII char[]
arraybyte
'2', '5', '6'
short
'6', '5', '5', '3', '6'
int
'2', '1', '4', '7,' '4', '8', '3', '6', '4', '8'
long
'2', '1', '4', '9', '2', '2', '3', '3', '7', '2', '0', '3', '6', '8', '5', '4', '7', '7', '5', '8', '0', '8'
- Returns:
TRUE
If the inputString
is both an integer, and also one which falls in the range comprised by the specified Java Primitive Type. ReturnFALSE
otherwise.
NOTE: The primary purpose of this method is to avoid having to writetry {} catch (NumberFormatException)
code-blocks. Furthermore, if only a check is desired, and theString
does not actually need to be converted to a number, this is also more efficient than actually performing the conversion.- See Also:
isInteger(String)
,isInt(String)
,isByte(String)
,isLong(String)
,isShort(String)
- Code:
- Exact Method Body:
int length = s.length(); // Zero length string's are not valid integers. if (length == 0) return false; // A negative integer may begin with a minus-sign. boolean negative = s.charAt(0) == '-'; // **************************************************************************************** // If the string is too short or too long, this method doesn't need to do any work. // We either know the answer immediately (too long), or we can call the simpler method // (in the case that it is too short) // **************************************************************************************** // If a string is shorter than (for type 'int', for example): 2147483647 (10 chars) // then we ought use the simplified method which just checks if the string is an integer. if (length < minArr.length) return isInteger(s); // If the string is longer than (for type 'int', for example): -2147483648 (11 chars) // then it cannot be an integer that fits into primitive 'int', so return false. if (length > (minArr.length + 1)) return false; // If the String is *EXACTLY* 11 characters long (for primitive-type 'int', for example), // but doesn't begin with a negative sign, we also know the answer immediately. if ((!negative) && (length == (minArr.length + 1))) return false; // If the String *EXACTLY* the length of MAX_NUUMBER, but it begins with a negative sign, // we can call the simplified method, instead as well. if (negative && (length == minArr.length)) return isInteger(s); // The **REST** of the code is only executed if the numeric part of the String // (Specifically: leaving out the '-' negative sign, which may or may not be present) // ... if the numeric part of the String is precisely the length of MAX_VALUE / MAX_NUMBER // as determined by the length of the array 'minArr'... If the input string is // **PRECISELY** that length, then the string must be checked in the loop below. int i = negative ? 1 : 0; int j = 0; boolean guaranteedFitIfInteger = false; char c = 0; while (i < length) { c = s.charAt(i); if (! guaranteedFitIfInteger) { if (c > minArr[j]) return false; if (c < minArr[j]) guaranteedFitIfInteger = true; } if (c < '0') return false; if (c > '9') return false; i++; j++; } // THE COMMENT BELOW DELINEATES WHAT HAPPENS FOR THE INPUT-CASE OF PRIMITIVE-TYPE 'INT' // (2147483648)... But it generalizes for byte, short, and long as well. // This might seem very strange. Since the MIN_VALUE ends with an '8', but the // MAX_VALUE ends with a '7', and since we are checking each character to see that // it falls within the array above, **RATHER THAN** just returning TRUE right here, // we have to catch the **LONE** border/edge case where some joker actually passed the // String 2147483648 - which must return FALSE, since the last positive integer is // 2147483647 (see that it has an ending of '7', rather than an '8'). return guaranteedFitIfInteger || negative || (c != minArr[minArr.length-1]);
-
isDouble
public static boolean isDouble(java.lang.String s)
Tests whether an input-String
can be parsed into adouble
, without throwing an exception.- Returns:
TRUE
if and only if callingDouble.valueOf(s)
(orDouble.parseDouble(s)
) is guaranteed to produce a result, without throwing aNumberFormatException
.
NOTE: Whenever analyzing performance and optimizations, it is important to know just "how costly" (as an order of magnitude) a certain operation really is. Constructors, for instance, that don't allocated much memory can be two orders of magnitude less costly than the JRE's costs for creating theStackTrace
object when an exception (such asNumberFormatException
) is thrown.
Though it costs "extra" to check whether aString
can be parsed by the Double-String Parser, if the programmer expects that exceptions will occasionally occur, the amount of time saved by checking aString
before parsing it as a Double-String will actually save time - even if only 1 in 500 of thoseString's
are invalid and would throw the exception, causing aStackTrace
constructor to be invoked.- See Also:
FLOATING_POINT_REGEX
,floatingPointPred
- Code:
- Exact Method Body:
return floatingPointPred.test(s);
-
-