Dlang 实现 Laravel Str 类常用方法
// Str::after
// 返回在指定字符串之后的所有字符
public static string after(string subject, string search) {
if (search == "") {
return subject;
}
int pos = subject.indexOf(search);
if (pos == -1) {
return "";
}
return subject[pos + search.length .. $];
}
// Str::afterLast
// 返回在字符串中最后一个出现的指定字符串之后的所有字符
public static string afterLast(string subject, string search) {
if (search == "") {
return subject;
}
int pos = subject.lastIndexOf(search);
if (pos == -1) {
return "";
}
return subject[pos + search.length .. $];
}
// Str::ascii
// 将字符串转换为 ASCII
public static string ascii(string value) {
// TODO: 实现具体的 ASCII 转换逻辑
return value;
}
// Str::before
// 返回在指定字符串之前的所有字符
public static string before(string subject, string search) {
if (search == "") {
return subject;
}
int pos = subject.indexOf(search);
if (pos == -1) {
return "";
}
return subject[0 .. pos];
}
// Str::beforeLast
// 返回在字符串中最后一个出现的指定字符串之前的所有字符
public static string beforeLast(string subject, string search) {
if (search == "") {
return subject;
}
int pos = subject.lastIndexOf(search);
if (pos == -1) {
return "";
}
return subject[0 .. pos];
}
// Str::between
// 返回位于两个指定字符串之间的所有字符
public static string between(string subject, string start, string end) {
if (start == "" || end == "") {
return subject;
}
int startPos = subject.indexOf(start);
if (startPos == -1) {
return "";
}
int endPos = subject.indexOf(end, startPos + start.length);
if (endPos == -1) {
return "";
}
return subject[startPos + start.length .. endPos];
}
// Str::camel
// 将字符串转换为驼峰式大小写
public static string camel(string value) {
// TODO: 实现具体的驼峰式转换逻辑
return value;
}
// Str::contains
// 判断指定字符串是否在给定的字符串中出现
public static bool contains(string haystack, string[] needles) {
foreach (needles) {
if (needle != "" && haystack.indexOf(needle) != -1) {
return true;
}
}
return false;
}
// Str::containsAll
// 判断指定字符串是否包含所有给定的子字符串
public static bool containsAll(string haystack, string[] needles) {
foreach (needles) {
if (!contains(haystack, needle)) {
return false;
}
}
return true;
}
// Str::endsWith
// 判断指定字符串是否以给定字符串结尾
public static bool endsWith(string haystack, string[] needles) {
foreach (needles) {
if (needle == haystack[haystack.length - needle.length .. $]) {
return true;
}
}
return false;
}
// Str::excerpt
// 从字符串中提取一段摘录
public static string excerpt(string value, string phrase, int radius = 100, string ending = "...") {
if (phrase is null) {
return value[0 .. radius] ~ ending;
}
string[] phrases = phrase.split(" ");
foreach (phrases) {
int pos = value.indexOf(phrase);
if (pos != -1) {
break;
}
}
if (pos == -1) {
return value[0 .. radius] ~ ending;
}
int start = max(0, pos - radius);
int length = min(value.length, pos + phrase.length + radius) - start;
return (start > 0 ? ending : "") ~ value[start .. length] ~ (pos + phrase.length < value.length ? ending : "");
}
// Str::finish
// 给定字符串以指定字符串结尾
public static string finish(string value, string cap) {
// TODO: 实现具体的结尾添加逻辑
return value ~ cap;
}
// Str::headline
// 将字符串转换为标题大小写
public static string headline(string value) {
// TODO: 实现具体的标题大小写转换逻辑
return value;
}
// Str::is
// 判断字符串是否与给定模式匹配
public static bool is(string pattern, string value) {
if (pattern == value) {
return true;
}
string[] patterns = pattern.split(" ");
foreach (patterns) {
if (pattern == value) {
return true;
}
// TODO: 实现具体的模式匹配逻辑
}
return false;
}
// Str::isAscii
// 判断字符串是否全部由 ASCII 字符组成
public static bool isAscii(string value) {
// TODO: 实现具体的 ASCII 字符判断逻辑
return true;
}
// Str::isUuid
// 判断字符串是否为 UUID
public static bool isUuid(string value) {
// TODO: 实现具体的 UUID 判断逻辑
return true;
}
原文地址: https://www.cveoy.top/t/topic/ockJ 著作权归作者所有。请勿转载和采集!