GotHub Strman-java – 字符串处理

news/2025/2/24 8:46:07

Strmen-java是一个字符串处理工具,你可以通过maven将它引入到项目中。除了Java本身的字符串处理方式外,我们还可以使用Apache Common Langs里的StringUtils来简化String的操作。但以上两种方式对于我们日常编程中最容易碰到的字符串处理来说,仍然显得有些不足。Strmen-java为我们提供了一个非常完整且强大的解决方案,使用它可以解决几乎所有字符串处理场景。

 

Getting Started

To use strman in your application, you have to add strman in your classpath. strman is available on Maven Central so you just need to add dependency to your favorite build tool as show below.

For Apache Maven users, please add following to your pom.xml.

<dependencies>
    <dependency>
        <groupId>com.shekhargulati</groupId>
        <artifactId>strman</artifactId>
        <version>0.2.0</version>
        <type>jar</type>
    </dependency>
</dependencies>

Gradle users can add following to their build.gradle file.

compile(group: 'com.shekhargulati', name: 'strman', version: '0.2.0', ext: 'jar'){
        transitive=true
}

Available Functions

These are the available functions in current version of library:

append

Appends Strings to value

import static strman.Strman.append
append("f", "o", "o", "b", "a", "r")
// result => "foobar"

appendArray

Append an array of String to value

import static strman.Strman.appendArray
appendArray("f", new String[]{"o", "o", "b", "a", "r"}
// result => "foobar"

at

Get the character at index. This method will take care of negative indexes.

import static strman.Strman.at
at("foobar", 0)
// result => Optional("f")

between

Returns an array with strings between start and end.

import static strman.Strman.between
between("[abc][def]", "[", "]")
// result => ["abc","def"]

chars

Returns a String array consisting of the characters in the String.

import static strman.Strman.chars
chars("title")
// result => ["t", "i", "t", "l", "e"]

collapseWhitespace

Replace consecutive whitespace characters with a single space.

import static strman.Strman.collapseWhitespace
collapseWhitespace("foo    bar")
// result => "foo bar"

contains

Verifies that the needle is contained in the value.

import static strman.Strman.contains
contains("foo bar","foo")
// result => true

contains("foo bar","FOO", false) // turning off case sensitivity
// result => true

containsAll

Verifies that all needles are contained in value

import static strman.Strman.containsAll
containsAll("foo bar", new String[]{"foo", "bar"})
// result => true

containsAll("foo bar", new String[]{"FOO", "bar"},false)
// result => true

containsAny

Verifies that one or more of needles are contained in value.

import static strman.Strman.containsAny
containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true)
// result => true

countSubstr

Count the number of times substr appears in value

import static strman.Strman.countSubstr
countSubstr("aaaAAAaaa", "aaa")
// result => 2
countSubstr("aaaAAAaaa", "aaa", false, false)
// result => 3

endsWith

Test if value ends with search.

import static strman.Strman.endsWith
endsWith("foo bar", "bar")
// result => true
endsWith("foo Bar", "BAR", false)
// result => true

ensureLeft

Ensures that the value begins with prefix. If it doesn't exist, it's prepended.

import static strman.Strman.ensureLeft
ensureLeft("foobar", "foo")
// result => "foobar"
ensureLeft("bar", "foo")
// result => "foobar"
ensureLeft("foobar", "FOO", false)
// result => "foobar"

base64Decode

Decodes data encoded with MIME base64

import static strman.Strman.base64Decode
base64Decode("c3RybWFu")
// result => "strman"

base64Encode

Encodes data with MIME base64.

import static strman.Strman.base64Encode
base64Encode("strman")
// result => "c3RybWFu"

binDecode

Convert binary unicode (16 digits) string to string chars

import static strman.Strman.binDecode
binDecode("0000000001000001")
// result => "A"

binEncode

Convert string chars to binary unicode (16 digits)

import static strman.Strman.binEncode
binEncode("A")
// result => "0000000001000001"

decDecode

Convert decimal unicode (5 digits) string to string chars

import static strman.Strman.decDecode
decDecode("00065")
// result => "A"

decEncode

Convert string chars to decimal unicode (5 digits)

import static strman.Strman.decEncode
decEncode("A")
// result => "00065"

ensureRight

Ensures that the value ends with suffix. If it doesn't, it's appended.

import static strman.Strman.ensureRight
ensureRight("foo", "bar")
// result => "foobar"

ensureRight("foobar", "bar")
// result => "foobar"

ensureRight("fooBAR", "bar", false)
// result => "foobar"

first

Returns the first n chars of String

import static strman.Strman.first
first("foobar", 3)
// result => "foo"

head

Return the first char of String

import static strman.Strman.head
head("foobar")
// result => "f"

hexDecode

Convert hexadecimal unicode (4 digits) string to string chars

import static strman.Strman.hexDecode
hexDecode("0041")
// result => "A"

hexEncode

Convert string chars to hexadecimal unicode (4 digits)

import static strman.Strman.hexEncode
hexEncode("A")
// result => "0041"

inequal

Tests if two Strings are inequal

import static strman.Strman.inequal
inequal("a", "b")
// result => true

insert

Inserts 'substr' into the 'value' at the 'index' provided.

import static strman.Strman.insert
insert("fbar", "oo", 1)
// result => "foobar"

last

Return the last n chars of String

import static strman.Strman.last
last("foobarfoo", 3)
// result => "foo"

leftPad

Returns a new string of a given length such that the beginning of the string is padded.

import static strman.Strman.leftPad
leftPad("1", "0", 5)
// result => "00001"

lastIndexOf

This method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from the offset.

import static strman.Strman.lastIndexOf
lastIndexOf("foobarfoobar", "F", false)
// result => 6

leftTrim

Removes all spaces on left

import static strman.Strman.leftTrim
leftTrim("     strman")
// result => "strman"

prepend

Return a new String starting with prepends

prepend("r", "f", "o", "o", "b", "a")
// "foobar"

removeEmptyStrings

Remove empty Strings from string array

removeEmptyStrings(new String[]{"aa", "", "   ", "bb", "cc", null})
// result => ["aa", "bb", "cc"]

removeLeft

Returns a new String with the prefix removed, if present.

removeLeft("foofoo", "foo")
// "foo"

removeNonWords

Remove all non word characters.

removeNonWords("foo&bar-")
// result => "foobar"

removeRight

Returns a new string with the 'suffix' removed, if present.

removeRight("foobar", "bar")
// result => "foo"
removeRight("foobar", "BAR",false)
// result => "foo"

removeSpaces

Remove all spaces and replace for value.

removeSpaces("foo bar")
// result => "foobar"

repeat

Returns a repeated string given a multiplier.

repeat("1", 3)
// result  => "111"

reverse

Reverse the input String

reverse("foo")
// result => "oof"

rightPad

Returns a new string of a given length such that the ending of the string is padded.

rightPad("1", "0", 5)
// result => "10000"

rightTrim

Remove all spaces on right.

rightTrim("strman   ")
// result => "strman"

safeTruncate

Truncate the string securely, not cutting a word in half. It always returns the last full word.

safeTruncate("foo bar", 4, ".")
// result => "foo."
safeTruncate("A Javascript string manipulation library.", 16, "...")
// result => "A Javascript..."

truncate

Truncate the unsecured form string, cutting the independent string of required position.

truncate("A Javascript string manipulation library.", 14, "...")
// result => "A Javascrip..."

htmlDecode

Converts all HTML entities to applicable characters.

htmlDecode("&SHcy;")
// result => Ш

htmlEncode

Convert all applicable characters to HTML entities.

htmlEncode("Ш")
// result => "&SHcy;"

shuffle

It returns a string with its characters in random order.

shuffle("shekhar")

slugify

Convert a String to a slug

slugify("foo bar")
// result => "foo-bar"

transliterate

Remove all non valid characters. Example: change á => a or ẽ => e.

transliterate("fóõ bár")
// result => "foo bar"

surround

Surrounds a 'value' with the given 'prefix' and 'suffix'.

surround("div", "<", ">"
// result => "<div>s"

tail

tail("foobar")
// result => "oobar"

toCamelCase

Transform to camelCase

toCamelCase("CamelCase")
// result => "camelCase"
toCamelCase("camel-case")
// result => "camelCase"

toStudlyCase

Transform to StudlyCaps.

toStudlyCase("hello world")
// result => "HelloWorld"

toDecamelize

Decamelize String

toDecamelize("helloWorld",null)
// result => "hello world"

toKebabCase

Transform to kebab-case.

toKebabCase("hello World")
// result => "hello-world"

toSnakeCase

Transform to snake_case.

toSnakeCase("hello world")
// result => "hello_world"

 

https://github.com/shekhargulati/strman-java


http://www.niftyadmin.cn/n/3039983.html

相关文章

[windows server 2008 站点系列一]AD的站点建立与子网的管理

本次课程将给大家介绍AD中站点和子网的功能、站点和子网之间的关联&#xff0c;以及相关的设置步骤。应用背景介绍&#xff1a;contoso公司的总部在西安&#xff08;Xian&#xff09;&#xff0c;陕南的汉中&#xff08;Shannan&#xff09;和陕北的榆林&#xff08;Shanbei&am…

在CentOS 7上部署Ghost博客

作者&#xff1a;waringid一、简介跟静态博客不同的是&#xff0c;Ghost 这种轻量级的动态博客&#xff0c;有一个管理后台&#xff0c;可以直接写作和管理博客。本质上&#xff0c;跟 WordPress 是相通的&#xff0c;只是 Ghost 搭建在 Node.js 环境上&#xff0c;轻量&#x…

云主机的建站的优势

无论是公司还是说个人&#xff0c;在做网站的时候&#xff0c;往往都有一些比较基本的要求&#xff0c;他们希望可以浏览顺畅、响应较快、不会宕机等。而云主机所具有的优异性能&#xff0c;给网站运营者营造了一个十分良好的基础环境&#xff0c;要比虚拟主机、物理服务器这些…

absolute、relative,toggle()

測试代码例如以下&#xff1a;<div><div class"global">不应用样式</div><div class"global abs">位置为&#xff1a;absolute </div><div class"global rel">位置为&#xff1a;relative</div></…

专家看台:盛大架构师周爱民回顾职业历程,分享十项建议

【CSDN 11月20日消息】也许是由于《Delphi源代码分析》&#xff0c;也许是《大道至简》&#xff0c;也许是他的博客、项目&#xff0c;也许是他的培训讲座……总之我们知道了周爱民&#xff08;网名&#xff1a;Aimingoo&#xff09;&#xff0c;而且我们会很中肯的说一句“嗯&…

Java深入 - Filter过滤器

Java的1.3開始&#xff0c;对servlet2.3规范中增加了过滤器的支持。过滤器可以让我们对目标资源的请求和响应进行截取。 一些filter的特性&#xff1a; 1. Filter是Servlet规范的规定&#xff0c;须要Servlet容器的支持。 2. Filter不能使用Spring框架中的资源对象。 3. Filter…

企业站为什么更偏好上云

网络安全事件层出不穷&#xff0c;互联网企业不得不处处小心&#xff0c;更令传统企业心跳加速。当前信息社会&#xff0c;远离互联网就等于死亡。可是&#xff0c;传统企业没有网络安全技术能力和处理危机、防范风险的经验。研究报告显示&#xff0c;传统企业对IT基础的关注点…

由Redis的hGetAll函数所引发的一次服务宕机事件

昨晚通宵生产压测&#xff0c;终于算是将生产服务宕机的原因定位到了&#xff0c;心累。这篇博客&#xff0c;算作一个复盘和记录吧。。。 先来看看Redis的缓存淘汰算法思维导图&#xff1a; 说明&#xff1a;当实际占用的内存超过Redis配置的maxmemory时&#xff0c;Redis就会…