how to format string data like this '[1,fish#, 15,bird#, 4,horse#]' to '1,fish#15,bird#4,horse#'
发布于 2022-10-20 05:24:01
您需要创建模型并将列表映射到模型
在您的例子中,您的模型类如下所示
class User {
final int user;
final String tag;
User({ required this.user, required this.tag,
});
}像这样的列表
final List<User> userlist = [User(user: 4, tag: "ahmed#"),User(user: 15, tag: "up#"),];当您需要像这样使用数据时
userlist[0].tag,//0 is index示例
print(userlist[0].tag,) //this will print **ahmed#**发布于 2022-10-20 07:05:19
只需使用join和replaceAll。
export default function App() {
const encode = (source: string[]): string => {
return source.join(",").replaceAll("#,", "#");
};
const decode = (source: string): string[] => {
return source
.split(",")
.reduce((p, n) => [...p, ...n.split("#")], new Array<string>())
.map((e, i) => (i % 2 === 0 ? e : `${e}#`))
.filter((e) => !!e);
};
let source = ["1", "fish#", "15", "bird#", "4", "horse#"];
let sourceEncoded = encode(source);
console.log("encode", sourceEncoded);
// -> 1,fish#15,bird#4,horse#
let sourceDecoded = decode(sourceEncoded);
console.log("decode", sourceDecoded);
// -> ["1", "fish#", "15", "bird#", "4", "horse#"]
return (
<div className="App">
...
</div>
);
}代码筛选箱示例(控制台)。
https://stackoverflow.com/questions/74134727
复制相似问题