如何在 Ballerina 中迭代一个字符串?
How to iterate a string in Ballerina?
我正在尝试获取字符串的每个字符并进行一些格式化并在 Ballerina 中生成一个新字符串。我怎样才能按字符迭代?
我在 Ubuntu 16.04 和 Ballerina 0.975.0
有什么建议吗?
基于可用的 string functions,我可以想出以下解决方案。
解决方案 1:
string s = "This is my string";
foreach c in s.split("") {
io:println(c);
}
解决方案 2:
string s = "This is my string";
foreach i in 0..< lengthof s {
io:println(s.substring(i, i+1));
}
我正在尝试获取字符串的每个字符并进行一些格式化并在 Ballerina 中生成一个新字符串。我怎样才能按字符迭代?
我在 Ubuntu 16.04 和 Ballerina 0.975.0
有什么建议吗?
基于可用的 string functions,我可以想出以下解决方案。
解决方案 1:
string s = "This is my string";
foreach c in s.split("") {
io:println(c);
}
解决方案 2:
string s = "This is my string";
foreach i in 0..< lengthof s {
io:println(s.substring(i, i+1));
}