例如,我想替換 A = 1、B = 2、C = 3 等和空字符 = /。
輸出將是什么:
Enter a string: Hello world
Output: 8 5 12 12 15 / 23 15 18 12 4
反之亦然。我環顧四周,但似乎無法得到令人滿意的答案。
uj5u.com熱心網友回復:
也許這有幫助:
String myfunction(String input){
final alpha= RegExp(r'^[a-zA-Z ] $');
final numeric= RegExp(r'^[0-9] $');
String output = '';
Map<String, String> lookupInt = {
'1' :'a',
'2' :'b',
'3' :'c',
'4' :'d',
'5' :'e',
'6' :'f',
'7' :'g',
'8' :'h',
'9' :'i',
'10':'j',
'11':'k',
'12':'l',
'13':'m',
'14':'n',
'15':'o',
'16':'p',
'17':'q',
'18':'r',
'19':'s',
'20':'t',
'21':'u',
'22':'v',
'23':'w',
'24':'x',
'25':'y',
'26':'z',
'/' :' '
};
Map<String, String> lookupChart = lookupInt.map((k, v) => MapEntry(v, k));
if(alpha.hasMatch(input)){
output = input.toLowerCase().split('').map((e) => lookupChart[e]??'').join('');
}
if(numeric.hasMatch(input)){
output = input.toLowerCase().split('').map((e) => lookupInt[e]??'').join('');
}
return output;
}
uj5u.com熱心網友回復:
您可以將所有字串放入映射中,然后在拆分字串后運行 for in 回圈""
對于前。-
Map<String, dynamic> myMap= {
'A' : 1,
'B' : 2,
'C' : 3,
'D' : 4,
"E" : 5,
"/" : " ",
};
String test = "abcedadd/abcde";
List<dynamic>chars =[];
// Convert your string into list by call split method and run for in loop on every element
for (var element in test.split("")) {
// Check if key is preset in map then return map value else you can set any default value like i put 0
chars.add(myMap.containsKey(element.toUpperCase()) ? myMap[element.toUpperCase()] : 0 );
}
// Convert your list into string by joining by space use can use any value here
print(chars.join(" "));
結果將是 1 2 3 5 4 1 4 4 1 2 3 4
uj5u.com熱心網友回復:
另一種方法可能是
//setting things up
String theInput = "Hello world";
String theUCString = theInput.toUpperCase();
String theOutput = "";
Map<String, dynamic> charsMap = Map();
for (int i = 0; i <= 25; i ) {
String char = String.fromCharCode(i 65);
charsMap[char] = (i 1).toString();
}
charsMap[" "] = "/";
//now with that...
theUCString.runes.forEach((c) {
// if the character key doesn't exist it will return null, which we'll make an empty string
theOutput = " ${charsMap[String.fromCharCode(c)] ?? ""}";
});
print(theOutput);
uj5u.com熱心網友回復:
嘗試這樣的事情
String s = "hello";
for (var c in s.split('')) {
print(c.codeUnitAt(0) - 'a'.codeUnitAt(0));
}
List<int> numbers = [7, 4, 11, 11, 14];
for (var n in numbers) {
print(String.fromCharCode(n 'a'.codeUnitAt(0)));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/495137.html
