mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-15 19:27:36 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
555f50b2b1 | ||
|
|
bf081e6b48 | ||
|
|
ab6c36c078 | ||
|
|
dfc05347bf | ||
|
|
e251cc07d8 | ||
|
|
68ff5f4ebf | ||
|
|
6a4866fed8 | ||
|
|
3c7ec0d113 | ||
|
|
43b6b3ad17 |
@@ -38,7 +38,7 @@ These are the available functions in current version of library:
|
||||
Appends Strings to value
|
||||
|
||||
```java
|
||||
import static strman.append
|
||||
import static strman.Strman.append
|
||||
append("f", "o", "o", "b", "a", "r")
|
||||
// result => "foobar"
|
||||
```
|
||||
@@ -48,7 +48,7 @@ append("f", "o", "o", "b", "a", "r")
|
||||
Append an array of String to value
|
||||
|
||||
```java
|
||||
import static strman.appendArray
|
||||
import static strman.Strman.appendArray
|
||||
appendArray("f", new String[]{"o", "o", "b", "a", "r"}
|
||||
// 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.
|
||||
|
||||
```java
|
||||
import static strman.at
|
||||
import static strman.Strman.at
|
||||
at("foobar", 0)
|
||||
// result => Optional("f")
|
||||
```
|
||||
@@ -68,7 +68,7 @@ at("foobar", 0)
|
||||
Returns an array with strings between start and end.
|
||||
|
||||
```java
|
||||
import static strman.between
|
||||
import static strman.Strman.between
|
||||
between("[abc][def]", "[", "]")
|
||||
// result => ["abc","def"]
|
||||
```
|
||||
@@ -78,7 +78,7 @@ between("[abc][def]", "[", "]")
|
||||
Returns a String array consisting of the characters in the String.
|
||||
|
||||
```java
|
||||
import static strman.chars
|
||||
import static strman.Strman.chars
|
||||
chars("title")
|
||||
// result => ["t", "i", "t", "l", "e"]
|
||||
```
|
||||
@@ -88,7 +88,7 @@ chars("title")
|
||||
Replace consecutive whitespace characters with a single space.
|
||||
|
||||
```java
|
||||
import static strman.collapseWhitespace
|
||||
import static strman.Strman.collapseWhitespace
|
||||
collapseWhitespace("foo bar")
|
||||
// result => "foo bar"
|
||||
```
|
||||
@@ -98,7 +98,7 @@ collapseWhitespace("foo bar")
|
||||
Verifies that the needle is contained in the value.
|
||||
|
||||
```java
|
||||
import static strman.contains
|
||||
import static strman.Strman.contains
|
||||
contains("foo bar","foo")
|
||||
// result => true
|
||||
|
||||
@@ -111,7 +111,7 @@ contains("foo bar","FOO", false) // turning off case sensitivity
|
||||
Verifies that all needles are contained in value
|
||||
|
||||
```java
|
||||
import static strman.containsAll
|
||||
import static strman.Strman.containsAll
|
||||
containsAll("foo bar", new String[]{"foo", "bar"})
|
||||
// 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.
|
||||
|
||||
```java
|
||||
import static strman.containsAny
|
||||
import static strman.Strman.containsAny
|
||||
containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, 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
|
||||
|
||||
```java
|
||||
import static strman.countSubstr
|
||||
import static strman.Strman.countSubstr
|
||||
countSubstr("aaaAAAaaa", "aaa")
|
||||
// result => 2
|
||||
countSubstr("aaaAAAaaa", "aaa", false, false)
|
||||
@@ -146,7 +146,7 @@ countSubstr("aaaAAAaaa", "aaa", false, false)
|
||||
Test if value ends with search.
|
||||
|
||||
```java
|
||||
import static strman.endsWith
|
||||
import static strman.Strman.endsWith
|
||||
endsWith("foo bar", "bar")
|
||||
// result => true
|
||||
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.
|
||||
|
||||
```java
|
||||
import static strman.ensureLeft
|
||||
import static strman.Strman.ensureLeft
|
||||
ensureLeft("foobar", "foo")
|
||||
// result => "foobar"
|
||||
ensureLeft("bar", "foo")
|
||||
@@ -172,9 +172,9 @@ ensureLeft("foobar", "FOO", false)
|
||||
Decodes data encoded with MIME base64
|
||||
|
||||
```java
|
||||
import static strman.base64Decode
|
||||
import static strman.Strman.base64Decode
|
||||
base64Decode("c3RybWFu")
|
||||
// result => "strma"
|
||||
// result => "strman"
|
||||
```
|
||||
|
||||
## base64Encode
|
||||
@@ -182,7 +182,7 @@ base64Decode("c3RybWFu")
|
||||
Encodes data with MIME base64.
|
||||
|
||||
```java
|
||||
import static strman.base64Encode
|
||||
import static strman.Strman.base64Encode
|
||||
base64Encode("strman")
|
||||
// result => "c3RybWFu"
|
||||
```
|
||||
@@ -192,7 +192,7 @@ base64Encode("strman")
|
||||
Convert binary unicode (16 digits) string to string chars
|
||||
|
||||
```java
|
||||
import static strman.binDecode
|
||||
import static strman.Strman.binDecode
|
||||
binDecode("0000000001000001")
|
||||
// result => "A"
|
||||
```
|
||||
@@ -202,7 +202,7 @@ binDecode("0000000001000001")
|
||||
Convert string chars to binary unicode (16 digits)
|
||||
|
||||
```java
|
||||
import static strman.binEncode
|
||||
import static strman.Strman.binEncode
|
||||
binEncode("A")
|
||||
// result => "0000000001000001"
|
||||
```
|
||||
@@ -212,7 +212,7 @@ binEncode("A")
|
||||
Convert decimal unicode (5 digits) string to string chars
|
||||
|
||||
```java
|
||||
import static strman.decDecode
|
||||
import static strman.Strman.decDecode
|
||||
decDecode("00065")
|
||||
// result => "A"
|
||||
```
|
||||
@@ -222,7 +222,7 @@ decDecode("00065")
|
||||
Convert string chars to decimal unicode (5 digits)
|
||||
|
||||
```java
|
||||
import static strman.decEncode
|
||||
import static strman.Strman.decEncode
|
||||
decEncode("A")
|
||||
// result => "00065"
|
||||
```
|
||||
@@ -232,7 +232,7 @@ decEncode("A")
|
||||
Ensures that the value ends with suffix. If it doesn't, it's appended.
|
||||
|
||||
```java
|
||||
import static strman.ensureRight
|
||||
import static strman.Strman.ensureRight
|
||||
ensureRight("foo", "bar")
|
||||
// result => "foobar"
|
||||
|
||||
@@ -248,7 +248,7 @@ ensureRight("fooBAR", "bar", false)
|
||||
Returns the first n chars of String
|
||||
|
||||
```java
|
||||
import static strman.first
|
||||
import static strman.Strman.first
|
||||
first("foobar", 3)
|
||||
// result => "foo"
|
||||
```
|
||||
@@ -258,7 +258,7 @@ first("foobar", 3)
|
||||
Return the first char of String
|
||||
|
||||
```java
|
||||
import static strman.head
|
||||
import static strman.Strman.head
|
||||
head("foobar")
|
||||
// result => "f"
|
||||
```
|
||||
@@ -268,7 +268,7 @@ head("foobar")
|
||||
Convert hexadecimal unicode (4 digits) string to string chars
|
||||
|
||||
```java
|
||||
import static strman.hexDecode
|
||||
import static strman.Strman.hexDecode
|
||||
hexDecode("0041")
|
||||
// result => "A"
|
||||
```
|
||||
@@ -278,7 +278,7 @@ hexDecode("0041")
|
||||
Convert string chars to hexadecimal unicode (4 digits)
|
||||
|
||||
```java
|
||||
import static strman.hexEncode
|
||||
import static strman.Strman.hexEncode
|
||||
hexEncode("A")
|
||||
// result => "0041"
|
||||
```
|
||||
@@ -288,7 +288,7 @@ hexEncode("A")
|
||||
Tests if two Strings are inequal
|
||||
|
||||
```java
|
||||
import static strman.inequal
|
||||
import static strman.Strman.inequal
|
||||
inequal("a", "b")
|
||||
// result => true
|
||||
```
|
||||
@@ -298,7 +298,7 @@ inequal("a", "b")
|
||||
Inserts 'substr' into the 'value' at the 'index' provided.
|
||||
|
||||
```java
|
||||
import static strman.insert
|
||||
import static strman.Strman.insert
|
||||
insert("fbar", "oo", 1)
|
||||
// result => "foobar"
|
||||
```
|
||||
@@ -308,7 +308,7 @@ insert("fbar", "oo", 1)
|
||||
Return the last n chars of String
|
||||
|
||||
```java
|
||||
import static strman.last
|
||||
import static strman.Strman.last
|
||||
last("foobarfoo", 3)
|
||||
// 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.
|
||||
|
||||
```java
|
||||
import static strman.leftPad
|
||||
import static strman.Strman.leftPad
|
||||
leftPad("1", "0", 5)
|
||||
// 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.
|
||||
|
||||
```java
|
||||
import static strman.lastIndexOf
|
||||
import static strman.Strman.lastIndexOf
|
||||
lastIndexOf("foobarfoobar", "F", false)
|
||||
// result => 6
|
||||
```
|
||||
@@ -338,7 +338,7 @@ lastIndexOf("foobarfoobar", "F", false)
|
||||
Removes all spaces on left
|
||||
|
||||
```java
|
||||
import static strman.leftTrim
|
||||
import static strman.Strman.leftTrim
|
||||
leftTrim(" strman")
|
||||
// result => "strman"
|
||||
```
|
||||
@@ -564,4 +564,4 @@ toSnakeCase("hello world")
|
||||
|
||||
License
|
||||
-------
|
||||
strman is licensed under the MIT License - see the `LICENSE` file for details.
|
||||
strman is licensed under the MIT License - see the `LICENSE` file for details.
|
||||
|
||||
@@ -3,7 +3,6 @@ plugins {
|
||||
}
|
||||
|
||||
group 'com.shekhargulati'
|
||||
version '0.1.0-SNAPSHOT'
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'maven'
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
version=0.1.0
|
||||
version=0.2.0
|
||||
@@ -28,7 +28,6 @@
|
||||
package strman;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -488,6 +487,18 @@ public abstract class Strman {
|
||||
* @param second The second String
|
||||
* @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) {
|
||||
return !Objects.equals(first, second);
|
||||
}
|
||||
@@ -517,7 +528,12 @@ public abstract class Strman {
|
||||
*/
|
||||
public static boolean isUpperCase(final String value) {
|
||||
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) {
|
||||
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) {
|
||||
validate(value, 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) {
|
||||
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) {
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1043,13 +1063,13 @@ public abstract class Strman {
|
||||
* Decamelize String
|
||||
*
|
||||
* @param value The input String
|
||||
* @param chr string to use
|
||||
* @param chr string to use
|
||||
* @return String decamelized.
|
||||
*/
|
||||
public static String toDecamelize(final String value, final String chr) {
|
||||
String camelCasedString = toCamelCase(value);
|
||||
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,6 +25,7 @@
|
||||
*/
|
||||
|
||||
package strman;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
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")));
|
||||
}
|
||||
|
||||
@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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user