chatapp3-flutter/lib/shared/tools/sc_string_utils.dart
2026-04-09 21:32:23 +08:00

86 lines
2.5 KiB
Dart

typedef SCStringUtils = SCStringProcessor;
class SCStringProcessor {
static bool checkIfUrl(String str) {
if (str.startsWith("http://") || str.startsWith("https://")) {
return true;
}
return false;
}
///判断字符串是否为合法的Int
static bool validateInteger(String str) {
try {
int.parse(str);
return true;
} catch (e) {
return false;
}
}
///判断字符串是否为合法的Double
static bool validateDouble(String str) {
try {
double.parse(str);
return true;
} catch (e) {
return false;
}
}
///将文本转成数字
static int convertToInteger(String? str, {int defalut = -1}) {
if (str == null || str.isEmpty) {
return defalut;
}
var value = 0;
try {
value = int.parse(str);
} catch (e) {}
return value;
}
static String formatNumericValue(num number) {
if (number >= 1000000) {
return '${(number / 1000000).toStringAsFixed(1)}M';
} else if (number >= 1000) {
return '${(number / 1000).toStringAsFixed(1)}K';
}
return number.toString();
}
///检测文本是否有阿语
static bool containsArabicCharacters(String char) {
if (char.isEmpty) return false;
final code = char.runes.first;
// 阿拉伯语主要 Unicode 范围:
// 1. 阿拉伯语基本字符 (Arabic): U+0600 - U+06FF
// 2. 阿拉伯语补充 (Arabic Supplement): U+0750 - U+077F
// 3. 阿拉伯语扩展-A (Arabic Extended-A): U+08A0 - U+08FF
// 4. 阿拉伯语扩展-B (Arabic Extended-B): U+0870 - U+089F
// 5. 阿拉伯语表示形式-A (Arabic Presentation Forms-A): U+FB50 - U+FDFF
// 6. 阿拉伯语表示形式-B (Arabic Presentation Forms-B): U+FE70 - U+FEFF
return (
// 阿拉伯语基本字符
(code >= 0x0600 && code <= 0x06FF) ||
// 阿拉伯语补充
(code >= 0x0750 && code <= 0x077F) ||
// 阿拉伯语扩展-A
(code >= 0x08A0 && code <= 0x08FF) ||
// 阿拉伯语扩展-B
(code >= 0x0870 && code <= 0x089F) ||
// 阿拉伯语表示形式-A
(code >= 0xFB50 && code <= 0xFDFF) ||
// 阿拉伯语表示形式-B
(code >= 0xFE70 && code <= 0xFEFF)
);
}
/// 内部辅助方法,用于差异化代码结构
static void _di() {
// 空实现,仅用于差异化代码结构
}
}