是否有外部库或简单函数来 replace/remove 字符串中的特殊字符 for Flutter(dart)
Is there an external library or a simple function to replace/remove special characters in Strings for Flutter(dart)
App Screenshot
我如何从 txt 文件中获取数据。
Future<String>? get textAsString async {
Uri? uri = Uri.tryParse(text.url);
if (uri != null) {
String text = await http.read(uri);
return text;
}
return '';
}
我的小部件结构和代码布局。
FutureBuilder<String>(
future: currentScene.textAsString,
builder: (context, snapshot) {
String? text = snapshot.data;
if (snapshot.hasData && text != null) {
return ListView(
padding: kAppPadding,
controller: _controller,
children: [
Text(
text,
style: TextStyle(
height: 1.8,
fontFamily: 'Roboto',
color: kWhiteColor,
fontWeight: FontWeight.w300,
fontSize: 17,
),
),
],
);
} else if (snapshot.hasError) {
return Center(
child: AppErrorText(
onPressed: () {},
),
);
} else {
return Center(
child: AppProgressIndicator(),
);
}
})
我有一个 TXT url 存储在云存储中,我想检索文本并创建一个文本 reader 应用程序。
我使用 http.read(uri) 获取 TXT 文件的内容并将字符串传递给用 FutureBuilder 包装的文本小部件
我注意到字符串包含一些奇怪的字符 (â)...所以我正在寻找一种方法来 remove/replace 这些字符。
我不确定外部库,但您可以使用 ASCII 码来识别空格字符。
这是空格字符的ASCII码
ASCII = 32 to 47 // 32 is a code for blank space
ASCII = 58 to 64
ASCII = 91 to 96
ASCII = 123 to 126
在flutter中你可以得到字符串的ASCII值如下
String _string = 'Hello @World#';
List<int> asciiList = _string.codeUnits;
String newString = _string;
for (int i = 0; i < asciiList.length; i++) {
if (asciiList[i] >= 33 && asciiList[i] <= 47 || //we don't want to remove blank space so we start from 33
asciiList[i] >= 58 && asciiList[i] <= 64 ||
asciiList[i] >= 91 && asciiList[i] <= 96 ||
asciiList[i] >= 123 && asciiList[i] <= 126) {
newString = newString.replaceAll(_string[i], '');
} else {
print("It's not a spacial character");
}
}
print(newString); //Hello world
正如我们在评论中的讨论所怀疑的那样,这是由于类型编码。
因为,它是 .txt
文件,而不是您服务器上的 String
。您需要使用 Decoder
来获取格式正确的数据。
这样用,
添加这些导入,
import 'dart:convert';
import 'dart:io';
然后改变你的textAsString
函数
Future<String>? get textAsString async {
Uri uri = Uri.parse('https://firebasestorage.googleapis.com/v0/b/fantasyapp-c636c.appspot.com/o/AppContent%2FScenes%2F0Hmv9ZZL2tunZAQh%2FScene%202.txt?alt=media&token=48fe5c72-9584-4c52-941e-b9c5859b9479');
return await new HttpClient()
.getUrl(uri) // Reads the data
.then((HttpClientRequest request) => request.close()) // Then closes the request to return us the Future of response
.then((HttpClientResponse response) =>
response.transform(new Utf8Decoder()).join()); // transforms the response using UTF-8
}
App Screenshot
我如何从 txt 文件中获取数据。
Future<String>? get textAsString async {
Uri? uri = Uri.tryParse(text.url);
if (uri != null) {
String text = await http.read(uri);
return text;
}
return '';
}
我的小部件结构和代码布局。
FutureBuilder<String>(
future: currentScene.textAsString,
builder: (context, snapshot) {
String? text = snapshot.data;
if (snapshot.hasData && text != null) {
return ListView(
padding: kAppPadding,
controller: _controller,
children: [
Text(
text,
style: TextStyle(
height: 1.8,
fontFamily: 'Roboto',
color: kWhiteColor,
fontWeight: FontWeight.w300,
fontSize: 17,
),
),
],
);
} else if (snapshot.hasError) {
return Center(
child: AppErrorText(
onPressed: () {},
),
);
} else {
return Center(
child: AppProgressIndicator(),
);
}
})
我有一个 TXT url 存储在云存储中,我想检索文本并创建一个文本 reader 应用程序。
我使用 http.read(uri) 获取 TXT 文件的内容并将字符串传递给用 FutureBuilder 包装的文本小部件
我注意到字符串包含一些奇怪的字符 (â)...所以我正在寻找一种方法来 remove/replace 这些字符。
我不确定外部库,但您可以使用 ASCII 码来识别空格字符。 这是空格字符的ASCII码
ASCII = 32 to 47 // 32 is a code for blank space
ASCII = 58 to 64
ASCII = 91 to 96
ASCII = 123 to 126
在flutter中你可以得到字符串的ASCII值如下
String _string = 'Hello @World#';
List<int> asciiList = _string.codeUnits;
String newString = _string;
for (int i = 0; i < asciiList.length; i++) {
if (asciiList[i] >= 33 && asciiList[i] <= 47 || //we don't want to remove blank space so we start from 33
asciiList[i] >= 58 && asciiList[i] <= 64 ||
asciiList[i] >= 91 && asciiList[i] <= 96 ||
asciiList[i] >= 123 && asciiList[i] <= 126) {
newString = newString.replaceAll(_string[i], '');
} else {
print("It's not a spacial character");
}
}
print(newString); //Hello world
正如我们在评论中的讨论所怀疑的那样,这是由于类型编码。
因为,它是 .txt
文件,而不是您服务器上的 String
。您需要使用 Decoder
来获取格式正确的数据。
这样用,
添加这些导入,
import 'dart:convert';
import 'dart:io';
然后改变你的textAsString
函数
Future<String>? get textAsString async {
Uri uri = Uri.parse('https://firebasestorage.googleapis.com/v0/b/fantasyapp-c636c.appspot.com/o/AppContent%2FScenes%2F0Hmv9ZZL2tunZAQh%2FScene%202.txt?alt=media&token=48fe5c72-9584-4c52-941e-b9c5859b9479');
return await new HttpClient()
.getUrl(uri) // Reads the data
.then((HttpClientRequest request) => request.close()) // Then closes the request to return us the Future of response
.then((HttpClientResponse response) =>
response.transform(new Utf8Decoder()).join()); // transforms the response using UTF-8
}