9 Commits
5 changed files with 86 additions and 42 deletions
+30 -30
View File
@@ -38,7 +38,7 @@ These are the available functions in current version of library:
Appends Strings to value Appends Strings to value
```java ```java
import static strman.append import static strman.Strman.append
append("f", "o", "o", "b", "a", "r") append("f", "o", "o", "b", "a", "r")
// result => "foobar" // result => "foobar"
``` ```
@@ -48,7 +48,7 @@ append("f", "o", "o", "b", "a", "r")
Append an array of String to value Append an array of String to value
```java ```java
import static strman.appendArray import static strman.Strman.appendArray
appendArray("f", new String[]{"o", "o", "b", "a", "r"} appendArray("f", new String[]{"o", "o", "b", "a", "r"}
// result => "foobar" // result => "foobar"
``` ```
@@ -58,7 +58,7 @@ appendArray("f", new String[]{"o", "o", "b", "a", "r"}
Get the character at index. This method will take care of negative indexes. Get the character at index. This method will take care of negative indexes.
```java ```java
import static strman.at import static strman.Strman.at
at("foobar", 0) at("foobar", 0)
// result => Optional("f") // result => Optional("f")
``` ```
@@ -68,7 +68,7 @@ at("foobar", 0)
Returns an array with strings between start and end. Returns an array with strings between start and end.
```java ```java
import static strman.between import static strman.Strman.between
between("[abc][def]", "[", "]") between("[abc][def]", "[", "]")
// result => ["abc","def"] // result => ["abc","def"]
``` ```
@@ -78,7 +78,7 @@ between("[abc][def]", "[", "]")
Returns a String array consisting of the characters in the String. Returns a String array consisting of the characters in the String.
```java ```java
import static strman.chars import static strman.Strman.chars
chars("title") chars("title")
// result => ["t", "i", "t", "l", "e"] // result => ["t", "i", "t", "l", "e"]
``` ```
@@ -88,7 +88,7 @@ chars("title")
Replace consecutive whitespace characters with a single space. Replace consecutive whitespace characters with a single space.
```java ```java
import static strman.collapseWhitespace import static strman.Strman.collapseWhitespace
collapseWhitespace("foo bar") collapseWhitespace("foo bar")
// result => "foo bar" // result => "foo bar"
``` ```
@@ -98,7 +98,7 @@ collapseWhitespace("foo bar")
Verifies that the needle is contained in the value. Verifies that the needle is contained in the value.
```java ```java
import static strman.contains import static strman.Strman.contains
contains("foo bar","foo") contains("foo bar","foo")
// result => true // result => true
@@ -111,7 +111,7 @@ contains("foo bar","FOO", false) // turning off case sensitivity
Verifies that all needles are contained in value Verifies that all needles are contained in value
```java ```java
import static strman.containsAll import static strman.Strman.containsAll
containsAll("foo bar", new String[]{"foo", "bar"}) containsAll("foo bar", new String[]{"foo", "bar"})
// result => true // result => true
@@ -124,7 +124,7 @@ containsAll("foo bar", new String[]{"FOO", "bar"},false)
Verifies that one or more of needles are contained in value. Verifies that one or more of needles are contained in value.
```java ```java
import static strman.containsAny import static strman.Strman.containsAny
containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true) containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true)
// result => true // result => true
``` ```
@@ -134,7 +134,7 @@ containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true)
Count the number of times substr appears in value Count the number of times substr appears in value
```java ```java
import static strman.countSubstr import static strman.Strman.countSubstr
countSubstr("aaaAAAaaa", "aaa") countSubstr("aaaAAAaaa", "aaa")
// result => 2 // result => 2
countSubstr("aaaAAAaaa", "aaa", false, false) countSubstr("aaaAAAaaa", "aaa", false, false)
@@ -146,7 +146,7 @@ countSubstr("aaaAAAaaa", "aaa", false, false)
Test if value ends with search. Test if value ends with search.
```java ```java
import static strman.endsWith import static strman.Strman.endsWith
endsWith("foo bar", "bar") endsWith("foo bar", "bar")
// result => true // result => true
endsWith("foo Bar", "BAR", false) endsWith("foo Bar", "BAR", false)
@@ -158,7 +158,7 @@ endsWith("foo Bar", "BAR", false)
Ensures that the value begins with prefix. If it doesn't exist, it's prepended. Ensures that the value begins with prefix. If it doesn't exist, it's prepended.
```java ```java
import static strman.ensureLeft import static strman.Strman.ensureLeft
ensureLeft("foobar", "foo") ensureLeft("foobar", "foo")
// result => "foobar" // result => "foobar"
ensureLeft("bar", "foo") ensureLeft("bar", "foo")
@@ -172,9 +172,9 @@ ensureLeft("foobar", "FOO", false)
Decodes data encoded with MIME base64 Decodes data encoded with MIME base64
```java ```java
import static strman.base64Decode import static strman.Strman.base64Decode
base64Decode("c3RybWFu") base64Decode("c3RybWFu")
// result => "strma" // result => "strman"
``` ```
## base64Encode ## base64Encode
@@ -182,7 +182,7 @@ base64Decode("c3RybWFu")
Encodes data with MIME base64. Encodes data with MIME base64.
```java ```java
import static strman.base64Encode import static strman.Strman.base64Encode
base64Encode("strman") base64Encode("strman")
// result => "c3RybWFu" // result => "c3RybWFu"
``` ```
@@ -192,7 +192,7 @@ base64Encode("strman")
Convert binary unicode (16 digits) string to string chars Convert binary unicode (16 digits) string to string chars
```java ```java
import static strman.binDecode import static strman.Strman.binDecode
binDecode("0000000001000001") binDecode("0000000001000001")
// result => "A" // result => "A"
``` ```
@@ -202,7 +202,7 @@ binDecode("0000000001000001")
Convert string chars to binary unicode (16 digits) Convert string chars to binary unicode (16 digits)
```java ```java
import static strman.binEncode import static strman.Strman.binEncode
binEncode("A") binEncode("A")
// result => "0000000001000001" // result => "0000000001000001"
``` ```
@@ -212,7 +212,7 @@ binEncode("A")
Convert decimal unicode (5 digits) string to string chars Convert decimal unicode (5 digits) string to string chars
```java ```java
import static strman.decDecode import static strman.Strman.decDecode
decDecode("00065") decDecode("00065")
// result => "A" // result => "A"
``` ```
@@ -222,7 +222,7 @@ decDecode("00065")
Convert string chars to decimal unicode (5 digits) Convert string chars to decimal unicode (5 digits)
```java ```java
import static strman.decEncode import static strman.Strman.decEncode
decEncode("A") decEncode("A")
// result => "00065" // result => "00065"
``` ```
@@ -232,7 +232,7 @@ decEncode("A")
Ensures that the value ends with suffix. If it doesn't, it's appended. Ensures that the value ends with suffix. If it doesn't, it's appended.
```java ```java
import static strman.ensureRight import static strman.Strman.ensureRight
ensureRight("foo", "bar") ensureRight("foo", "bar")
// result => "foobar" // result => "foobar"
@@ -248,7 +248,7 @@ ensureRight("fooBAR", "bar", false)
Returns the first n chars of String Returns the first n chars of String
```java ```java
import static strman.first import static strman.Strman.first
first("foobar", 3) first("foobar", 3)
// result => "foo" // result => "foo"
``` ```
@@ -258,7 +258,7 @@ first("foobar", 3)
Return the first char of String Return the first char of String
```java ```java
import static strman.head import static strman.Strman.head
head("foobar") head("foobar")
// result => "f" // result => "f"
``` ```
@@ -268,7 +268,7 @@ head("foobar")
Convert hexadecimal unicode (4 digits) string to string chars Convert hexadecimal unicode (4 digits) string to string chars
```java ```java
import static strman.hexDecode import static strman.Strman.hexDecode
hexDecode("0041") hexDecode("0041")
// result => "A" // result => "A"
``` ```
@@ -278,7 +278,7 @@ hexDecode("0041")
Convert string chars to hexadecimal unicode (4 digits) Convert string chars to hexadecimal unicode (4 digits)
```java ```java
import static strman.hexEncode import static strman.Strman.hexEncode
hexEncode("A") hexEncode("A")
// result => "0041" // result => "0041"
``` ```
@@ -288,7 +288,7 @@ hexEncode("A")
Tests if two Strings are inequal Tests if two Strings are inequal
```java ```java
import static strman.inequal import static strman.Strman.inequal
inequal("a", "b") inequal("a", "b")
// result => true // result => true
``` ```
@@ -298,7 +298,7 @@ inequal("a", "b")
Inserts 'substr' into the 'value' at the 'index' provided. Inserts 'substr' into the 'value' at the 'index' provided.
```java ```java
import static strman.insert import static strman.Strman.insert
insert("fbar", "oo", 1) insert("fbar", "oo", 1)
// result => "foobar" // result => "foobar"
``` ```
@@ -308,7 +308,7 @@ insert("fbar", "oo", 1)
Return the last n chars of String Return the last n chars of String
```java ```java
import static strman.last import static strman.Strman.last
last("foobarfoo", 3) last("foobarfoo", 3)
// result => "foo" // result => "foo"
``` ```
@@ -318,7 +318,7 @@ last("foobarfoo", 3)
Returns a new string of a given length such that the beginning of the string is padded. Returns a new string of a given length such that the beginning of the string is padded.
```java ```java
import static strman.leftPad import static strman.Strman.leftPad
leftPad("1", "0", 5) leftPad("1", "0", 5)
// result => "00001" // result => "00001"
``` ```
@@ -328,7 +328,7 @@ leftPad("1", "0", 5)
This method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from the offset. This method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from the offset.
```java ```java
import static strman.lastIndexOf import static strman.Strman.lastIndexOf
lastIndexOf("foobarfoobar", "F", false) lastIndexOf("foobarfoobar", "F", false)
// result => 6 // result => 6
``` ```
@@ -338,7 +338,7 @@ lastIndexOf("foobarfoobar", "F", false)
Removes all spaces on left Removes all spaces on left
```java ```java
import static strman.leftTrim import static strman.Strman.leftTrim
leftTrim(" strman") leftTrim(" strman")
// result => "strman" // result => "strman"
``` ```
-1
View File
@@ -3,7 +3,6 @@ plugins {
} }
group 'com.shekhargulati' group 'com.shekhargulati'
version '0.1.0-SNAPSHOT'
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven' apply plugin: 'maven'
+1 -1
View File
@@ -1 +1 @@
version=0.1.0 version=0.2.0
+28 -8
View File
@@ -28,7 +28,6 @@
package strman; package strman;
import java.util.*; import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@@ -488,6 +487,18 @@ public abstract class Strman {
* @param second The second String * @param second The second String
* @return true if first and second are not equal false otherwise * @return true if first and second are not equal false otherwise
*/ */
public static boolean unequal(final String first, final String second) {
return !Objects.equals(first, second);
}
/**
* Tests if two Strings are inequal
*
* @param first The first String
* @param second The second String
* @return true if first and second are not equal false otherwise
* @deprecated use unequal instead
*/
public static boolean inequal(final String first, final String second) { public static boolean inequal(final String first, final String second) {
return !Objects.equals(first, second); return !Objects.equals(first, second);
} }
@@ -517,7 +528,12 @@ public abstract class Strman {
*/ */
public static boolean isUpperCase(final String value) { public static boolean isUpperCase(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Objects.equals(value, value.toUpperCase()); for (int i = 0; i < value.length(); i++) {
if (Character.isLowerCase(value.charAt(i))) {
return false;
}
}
return true;
} }
/** /**
@@ -528,7 +544,12 @@ public abstract class Strman {
*/ */
public static boolean isLowerCase(final String value) { public static boolean isLowerCase(final String value) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
return Objects.equals(value, value.toLowerCase()); for (int i = 0; i < value.length(); i++) {
if (Character.isUpperCase(value.charAt(i))) {
return false;
}
}
return true;
} }
/** /**
@@ -709,11 +730,10 @@ public abstract class Strman {
public static String removeLeft(final String value, final String prefix, final boolean caseSensitive) { public static String removeLeft(final String value, final String prefix, final boolean caseSensitive) {
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
validate(prefix, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); validate(prefix, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
BiFunction<String, String, String> fx = (f, s) -> f.startsWith(s) ? f.replaceFirst(s, "") : f;
if (caseSensitive) { if (caseSensitive) {
return fx.apply(value, prefix); return value.startsWith(prefix) ? value.substring(prefix.length()) : value;
} }
return fx.apply(value.toLowerCase(), prefix.toLowerCase()); return value.toLowerCase().startsWith(prefix.toLowerCase()) ? value.substring(prefix.length()) : value;
} }
/** /**
@@ -790,7 +810,7 @@ public abstract class Strman {
if (caseSensitive) { if (caseSensitive) {
return value.replace(search, newValue); return value.replace(search, newValue);
} }
return value.toLowerCase().replace(search.toLowerCase(), newValue); return Pattern.compile(search, Pattern.CASE_INSENSITIVE).matcher(value).replaceAll(Matcher.quoteReplacement(newValue));
} }
/** /**
@@ -1049,7 +1069,7 @@ public abstract class Strman {
public static String toDecamelize(final String value, final String chr) { public static String toDecamelize(final String value, final String chr) {
String camelCasedString = toCamelCase(value); String camelCasedString = toCamelCase(value);
String[] words = camelCasedString.split("(?=\\p{Upper})"); String[] words = camelCasedString.split("(?=\\p{Upper})");
return Arrays.stream(words).map(w -> w.toLowerCase()).collect(joining(Optional.ofNullable(chr).orElse(" "))); return Arrays.stream(words).map(String::toLowerCase).collect(joining(Optional.ofNullable(chr).orElse(" ")));
} }
/** /**
+25
View File
@@ -25,6 +25,7 @@
*/ */
package strman; package strman;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
@@ -819,5 +820,29 @@ public class StrmanTest {
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toSnakeCase(%s) should be de_camelize", el), toSnakeCase(el), equalTo("de_camelize"))); Arrays.stream(fixture).forEach(el -> assertThat(String.format("toSnakeCase(%s) should be de_camelize", el), toSnakeCase(el), equalTo("de_camelize")));
} }
@Test
public void unequal_shouldTestInequalityOfStrings() throws Exception {
assertThat(unequal("a", "b"), equalTo(true));
assertThat(unequal("a", "a"), equalTo(false));
assertThat(unequal("0", "1"), equalTo(true));
}
@Test
public void removeLeft_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = removeLeft("This HAS A THIS IN FRONT", "THIS ", false);
assertThat(result, is("HAS A THIS IN FRONT"));
}
@Test
public void replace_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = replace("One and two and THREE and Four", "and", "&", false);
assertThat(result, is("One & two & THREE & Four"));
}
@Test
public void removeRight_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = removeRight("Remove the END at the end", " END", false);
assertThat(result, is("Remove the END at the"));
}
} }