For example, I want to replace A = 1, B = 2, C = 3 etc and empty character = /.
What the output will be:
Enter a string: Hello world
Output: 8 5 12 12 15 / 23 15 18 12 4
and vice versa. I have looked around but i cant seem to get a satisfying answer.
CodePudding user response:
Maybe this helps:
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;
}
CodePudding user response:
you can put all character string into a map and then run a for in loop after splitting string by ""
For Ex. -
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(" "));
Result will be 1 2 3 5 4 1 4 4 1 2 3 4
CodePudding user response:
Another approach might be something like to use the String
's method runes which returns an Itterable
of integer value of the character. Looking up those converted back to String in the map would work, though if a character doesn't exist the Map will return a null, which we can replace with an epty string or some other string.
theUCString.runes.forEach(
(c) => theOutput =" ${charsMap[String.fromCharCode(c)] ?? ""}");
//setting things up
String theInput = "Hello world";
String theUCString = theInput.toUpperCase();
String theOutput = "";
Map<String, String> charsMap = Map();
// build a lookup map
for (int i = 1; i <= 26; i ) {
String char = String.fromCharCode(i 64);
charsMap[char] = (i).toString();
}
charsMap[" "] = "/";
// Now with that...
// We'll use each character from input's runes, integer values representing that UTF16 character
// If the character key doesn't exist it will return null, which we'll make an empty string
theUCString.runes.forEach(
(c) => theOutput = " ${charsMap[String.fromCharCode(c)] ?? ""}");
print("Output: $theOutput");
CodePudding user response:
Try something like this
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)));
}