public class StringUtils extends Object
Provides extra functionality for String objects.
Modifier and Type | Method and Description |
---|---|
static String[] |
arrayOf(String... values)
Generates an
String[] from comma-separated values. |
static String |
collectionToString(Collection list)
Outputs entries from a
Collection to a String . |
static String |
collectionToString(Collection list,
String entrySeparator)
Outputs entries from a
Collection to a String . |
static String |
createEscapedString_asStringList(Collection<String> vals) |
static String |
escapedStringToWrite(String s) |
static int[] |
find(String[] array,
String value,
Constants.SearchType searchType)
Returns the position(s) where a given value can be found into an array.
|
static String |
getLineSeparator()
Returns the line separator.
|
static String |
join(Collection<String> collection,
String separator)
Joins elements from a given collection into a
String . |
static String |
join(String[] array,
String separator)
Joins elements from a given array into a
String . |
static String |
mapToString(Map map)
Outputs entries from a
Map to a String . |
static String |
mapToString(Map map,
String keyValueSeparator,
String entrySeparator)
Outputs entries from a
Map to a String . |
static List<String> |
readEscapedString_asStringList(String val,
List<String> defaultValue) |
static IntMatrix2D |
readIntMatrix(String s)
Reads a matrix
IntMatrix2D from a String , where rows are separated by ";" and columns by spaces |
static DoubleMatrix2D |
readMatrix(String s)
Reads a matrix
DoubleMatrix2D from a String , where rows are separated by ";" and columns by spaces |
static void |
saveToFile(String text,
File file)
Outputs an
String to a file. |
static void |
saveToFile(String text,
File file,
boolean append)
Outputs an
String to a file. |
static String |
secondsToYearsDaysHoursMinutesSeconds(double seconds)
Converts a timestamp in seconds into its equivalent representation in days,
hours, minutes and seconds.
|
static String[] |
select(String[] array,
int[] indexes)
Returns a set of selected elements from an input array.
|
static String[] |
split(String string)
Splits a String into an array asumming items are separated by spaces.
|
static String[] |
split(String string,
String separators)
Splits a String into an array according to a set of separators (i.e.
|
static Map<String,String> |
stringToMap(String string)
Converts an
String to a map. |
static Map<String,String> |
stringToMap(String string,
String keyValueSeparator,
String entrySeparator)
Converts an
String to a map. |
static String[] |
toArray(Collection list)
Converts a collection (
List , Set ...) of objects to a String array. |
static boolean[] |
toBooleanArray(Collection<String> collection)
Converts a collection of
String objects to a boolean array. |
static boolean[] |
toBooleanArray(Collection<String> collection,
boolean valueForNull)
Converts a collection of
String objects to a boolean array. |
static boolean[] |
toBooleanArray(String[] array)
Converts a
String array to a boolean array. |
static boolean[] |
toBooleanArray(String[] array,
boolean valueForNull)
Converts a
String array to a boolean array. |
static List<Boolean> |
toBooleanList(String[] array)
Converts a
String array to a boolean array. |
static List<Boolean> |
toBooleanList(String[] array,
boolean valueForNull)
Converts a
String array to a Boolean list. |
static <A> Map<A,Boolean> |
toBooleanMap(Map<A,String> map)
Converts a map whose values are
String objects to another
where values are Boolean objects. |
static <A> Map<A,Boolean> |
toBooleanMap(Map<A,String> map,
boolean valueForNull)
Converts a map whose values are
String objects to another
where values are Boolean objects. |
static double[] |
toDoubleArray(Collection<String> collection)
Converts a collection of
String objects to a double array. |
static double[] |
toDoubleArray(Collection<String> collection,
double valueForNull)
Converts a collection of
String objects to a double array. |
static double[] |
toDoubleArray(String[] array)
Converts a
String array to a double array. |
static double[] |
toDoubleArray(String[] array,
double valueForNull)
Converts a
String array to a double array. |
static List<Double> |
toDoubleList(String[] array)
Converts a
String array to a double array. |
static List<Double> |
toDoubleList(String[] array,
double valueForNull)
Converts a
String array to a double array. |
static <A> Map<A,Double> |
toDoubleMap(Map<A,String> map)
Converts a map whose values are
String objects to another
where values are Double objects. |
static <A> Map<A,Double> |
toDoubleMap(Map<A,String> map,
double valueForNull)
Converts a map whose values are
String objects to another
where values are Double objects. |
static int[] |
toIntArray(Collection<String> collection)
Converts a collection of
String objects to an int array. |
static int[] |
toIntArray(Collection<String> collection,
int valueForNull)
Converts a collection of
String objects to an int array. |
static int[] |
toIntArray(String[] array)
Converts a
String array to an int array. |
static int[] |
toIntArray(String[] array,
int valueForNull)
Converts a
String array to an int array. |
static <A> Map<A,Integer> |
toIntegerMap(Map<A,String> map)
Converts a map whose values are
String objects to another
where values are Integer objects. |
static <A> Map<A,Integer> |
toIntegerMap(Map<A,String> map,
int valueForNull)
Converts a map whose values are
String objects to another
where values are Integer objects. |
static List<String> |
toList(String[] array)
Converts from a
String array to a list. |
static long[] |
toLongArray(Collection<String> collection)
Converts a collection of
String objects to a long array. |
static long[] |
toLongArray(Collection<String> collection,
long valueForNull)
Converts a collection of
String objects to a long array. |
static long[] |
toLongArray(String[] array)
Converts a
String array to a long array. |
static long[] |
toLongArray(String[] array,
long valueForNull)
Converts a
String array to a long array. |
static List<Long> |
toLongList(String[] array)
Converts a
String array to a long array. |
static List<Long> |
toLongList(String[] array,
long valueForNull)
Converts a
String array to a long array. |
static <A> Map<A,Long> |
toLongMap(Map<A,String> map)
Converts a map whose values are
String objects to another
where values are Long objects. |
static <A> Map<A,Long> |
toLongMap(Map<A,String> map,
long valueForNull)
Converts a map whose values are
String objects to another
where values are Long objects. |
static Map<String,String> |
toMap(String string,
String keyValueSeparator,
String entrySeparator)
Converts to a
Map a codified String . |
static String |
unescapedStringRead(String s) |
static String |
writeMatrix(DoubleMatrix1D mat)
Generates an
String from a vector of DoubleMatrix1D where elements are separated by spaces |
static String |
writeMatrix(DoubleMatrix2D mat)
Generates an
String from a matrix, where rows are separated by ";" and columns by spaces |
static String |
writeMatrix(IntMatrix1D mat)
Generates an
String from a vector of IntMatrix1D where elements are separated by spaces |
static String |
writeMatrix(IntMatrix2D mat)
Generates an
String from a matrix, where rows are separated by ";" and columns by spaces |
public static String[] arrayOf(String... values)
String[]
from comma-separated values.values
- Comma-separated String
valuesString[]
public static String collectionToString(Collection list)
Collection
to a String
. Default
separator between list entries is ', '.list
- Input listString
representing the list.public static String collectionToString(Collection list, String entrySeparator)
Collection
to a String
.list
- Input listentrySeparator
- Separator between list entries.String
representing the list.public static String createEscapedString_asStringList(Collection<String> vals)
public static int[] find(String[] array, String value, Constants.SearchType searchType)
array
- Input arrayvalue
- Value to be searched forsearchType
- Indicates whether the first, the last, or all occurrences are returnedpublic static String getLineSeparator()
public static String join(Collection<String> collection, String separator)
String
.collection
- Input collectionseparator
- Entry separatorpublic static String join(String[] array, String separator)
String
.array
- Input arrayseparator
- Entry separatorpublic static String mapToString(Map map)
Map
to a String
. Default
separator between each key and value is '=', and separator between key-value pair
is ', '.map
- Input mapString
representing the mappublic static String mapToString(Map map, String keyValueSeparator, String entrySeparator)
Map
to a String
.map
- Input mapkeyValueSeparator
- Separator between keys and valuesentrySeparator
- Separator between key-value pairsString
representing the mappublic static List<String> readEscapedString_asStringList(String val, List<String> defaultValue)
public static IntMatrix2D readIntMatrix(String s)
IntMatrix2D
from a String
, where rows are separated by ";" and columns by spacess
- String
public static DoubleMatrix2D readMatrix(String s)
DoubleMatrix2D
from a String
, where rows are separated by ";" and columns by spacess
- String
public static void saveToFile(String text, File file)
String
to a file.text
- String
to be savedfile
- File
where save the contentspublic static void saveToFile(String text, File file, boolean append)
String
to a file.text
- String
to be savedfile
- File
where save the contentsappend
- Indicates whether file
must be overriden if already exists (if true
), or text
is append at the end (if false
)public static String secondsToYearsDaysHoursMinutesSeconds(double seconds)
seconds
- Timestamppublic static String[] select(String[] array, int[] indexes)
array
- Input arrayindexes
- Position of elements to be selected.indexes
public static String[] split(String string)
string
- Input stringpublic static String[] split(String string, String separators)
string
- Input stringseparators
- Set of separatorsString
array (empty array if string
= null
)public static Map<String,String> stringToMap(String string)
String
to a map. Default separator between each key
and value is '=', and separator between key-value pair is ', '.string
- Input String
String
representing the mappublic static Map<String,String> stringToMap(String string, String keyValueSeparator, String entrySeparator)
String
to a map.string
- Input String
keyValueSeparator
- Separator between keys and valuesentrySeparator
- Separator between key-value pairsMap
public static String[] toArray(Collection list)
List
, Set
...) of objects to a String
array. If objects are not instances of String
, toString()
will be used.list
- Input listString
array (empty array if list
= null
)public static boolean[] toBooleanArray(Collection<String> collection)
String
objects to a boolean
array.collection
- Input collectionboolean
arraypublic static boolean[] toBooleanArray(Collection<String> collection, boolean valueForNull)
String
objects to a boolean
array.collection
- Input collectionvalueForNull
- Value for null
positionsboolean
arraypublic static boolean[] toBooleanArray(String[] array)
String
array to a boolean
array.array
- Input arrayboolean
arraypublic static boolean[] toBooleanArray(String[] array, boolean valueForNull)
String
array to a boolean
array.array
- Input arrayvalueForNull
- Value for null
positionsboolean
arraypublic static List<Boolean> toBooleanList(String[] array)
String
array to a boolean
array.array
- Input arrayboolean
arraypublic static List<Boolean> toBooleanList(String[] array, boolean valueForNull)
String
array to a Boolean
list.array
- Input arrayvalueForNull
- Value for null
positionsboolean
arraypublic static <A> Map<A,Boolean> toBooleanMap(Map<A,String> map)
String
objects to another
where values are Boolean
objects.A
- Key typemap
- Input mapBoolean
objectspublic static <A> Map<A,Boolean> toBooleanMap(Map<A,String> map, boolean valueForNull)
String
objects to another
where values are Boolean
objects.A
- Key typemap
- Input mapvalueForNull
- Value for null
entriesBoolean
objectspublic static double[] toDoubleArray(Collection<String> collection)
String
objects to a double
array.collection
- Input collectiondouble
arraypublic static double[] toDoubleArray(Collection<String> collection, double valueForNull)
String
objects to a double
array.collection
- Input collectionvalueForNull
- Value for null
positionsdouble
arraypublic static double[] toDoubleArray(String[] array)
String
array to a double
array.array
- Input arraydouble
arraypublic static double[] toDoubleArray(String[] array, double valueForNull)
String
array to a double
array.array
- Input arrayvalueForNull
- Value for null
positionsdouble
arraypublic static List<Double> toDoubleList(String[] array)
String
array to a double
array.array
- Input arraydouble
arraypublic static List<Double> toDoubleList(String[] array, double valueForNull)
String
array to a double
array.array
- Input arrayvalueForNull
- Value for null
positionsdouble
arraypublic static <A> Map<A,Double> toDoubleMap(Map<A,String> map)
String
objects to another
where values are Double
objects.A
- Key typemap
- Input mapDouble
objectspublic static <A> Map<A,Double> toDoubleMap(Map<A,String> map, double valueForNull)
String
objects to another
where values are Double
objects.A
- Key typemap
- Input mapvalueForNull
- Value for null
positionsDouble
objectspublic static int[] toIntArray(Collection<String> collection)
String
objects to an int
array.collection
- Input collectionint
arraypublic static int[] toIntArray(Collection<String> collection, int valueForNull)
String
objects to an int
array.collection
- Input collectionvalueForNull
- Value for null
positionsint
arraypublic static int[] toIntArray(String[] array)
String
array to an int
array.array
- Input arrayint
arraypublic static int[] toIntArray(String[] array, int valueForNull)
String
array to an int
array.array
- Input arrayvalueForNull
- Value for null
positionsint
arraypublic static <A> Map<A,Integer> toIntegerMap(Map<A,String> map)
String
objects to another
where values are Integer
objects.A
- Key typemap
- Input mapInteger
objectspublic static <A> Map<A,Integer> toIntegerMap(Map<A,String> map, int valueForNull)
String
objects to another
where values are Integer
objects.A
- Key typemap
- Input mapvalueForNull
- Value for null
positionsInteger
objectspublic static List<String> toList(String[] array)
String
array to a list.array
- Input arrayString
objectspublic static long[] toLongArray(Collection<String> collection)
String
objects to a long
array.collection
- Input collectionlong
arraypublic static long[] toLongArray(Collection<String> collection, long valueForNull)
String
objects to a long
array.collection
- Input collectionvalueForNull
- Value for null
positionslong
arraypublic static long[] toLongArray(String[] array)
String
array to a long
array.array
- Input arraylong
arraypublic static long[] toLongArray(String[] array, long valueForNull)
String
array to a long
array.array
- Input arrayvalueForNull
- Value for null
positionslong
arraypublic static List<Long> toLongList(String[] array)
String
array to a long
array.array
- Input arraylong
arraypublic static List<Long> toLongList(String[] array, long valueForNull)
String
array to a long
array.array
- Input arrayvalueForNull
- Value for null
positionslong
arraypublic static <A> Map<A,Long> toLongMap(Map<A,String> map)
String
objects to another
where values are Long
objects.A
- Key typemap
- Input mapLong
objectspublic static <A> Map<A,Long> toLongMap(Map<A,String> map, long valueForNull)
String
objects to another
where values are Long
objects.A
- Key typemap
- Input mapvalueForNull
- Value for null
positionsLong
objectspublic static Map<String,String> toMap(String string, String keyValueSeparator, String entrySeparator)
Map
a codified String
.string
- Input String
keyValueSeparator
- Separator between keys and valuesentrySeparator
- Separator between key-value pairsMap
represented by the String
public static String writeMatrix(DoubleMatrix1D mat)
String
from a vector of DoubleMatrix1D
where elements are separated by spacesmat
- a vectorString
public static String writeMatrix(DoubleMatrix2D mat)
String
from a matrix, where rows are separated by ";" and columns by spacesmat
- a matrixString
public static String writeMatrix(IntMatrix1D mat)
String
from a vector of IntMatrix1D
where elements are separated by spacesmat
- a vectorString
public static String writeMatrix(IntMatrix2D mat)
String
from a matrix, where rows are separated by ";" and columns by spacesmat
- a matrixString
Copyright © 2018. All rights reserved.