I'm trying to make a function that maps letters of the alphabet to other letters and I'm wondering if there is a simple way to do this or do I need to make a dictionary for each individual letter
Not sure what language but you could convert the character to it's unicode value and do some arithmetic function on to it e.g. in JS
var letter = "a";
var code = letter.charCodeAt(0);
// should do some checking to make sure you stay in letter range
code = code + 4;
return String.fromCharCode(code);
Related
I have example:
let stringToCheck: String = "42"
let numbers: CharacterSet = CharacterSet.decimalDigits
let stringIsANumber: Bool = stringToCheck.rangeOfCharacter(from: numbers.inverted) == nil
and i have two question
How the function inverted works? what does it do?
what range does rangeOfCharacter return?
inverted means the opposite. For example, if you have only characters a and b and c and a character set consisting of a, its inversion is b and c. So your decimalDigits characters set, inverted, means everything that is not a decimal digit.
A range is a contiguous stretch of something, specified numerically. For example, if you have the string "abc", the range of "bc" is "the second and third characters". The range of something that isn't there at all can be expressed as nil.
So the code you have shown looks for a character that is not a digit in the original string, and if it fails to find one (so that the range is nil), it says that the string is entirely a number.
I'm trying to built an Android application in MIT App Inventor 2.
This is my design
This is my code blocks
My purpose is; when I click somewhere on the color wheel; getting the coordinates of the place that I clicked (black ball) and get its RGB values.
It works perfectly on phone screen, it shows the values. But the problem is; when I try to import the rgb values to Firebase, the values are like in this format in this picture
As you see, the text formats in their boxes are like: "\"101\""
But I want: 101 only. Because I will get the values to my NodeMCU ESP8266 for blink a RGB LED. I will insert these values to analogWrite(pin,value) function.
Where is my fault in MIT App Inventor Block screen? Is there any solution in there? Or can you give me suggestion about it for ESP8266 code part (like split the text or something) ?
You can add this line
String b_fir = Firebase.getString("B");
String str_b_fir = getStringPartByNr(b_fir, '"', 1);
int int_b_fir = str_b_fir.toInt();
You can add this function under the loop
String getStringPartByNr(String data, char separator, int index)
{
// spliting a string and return the part nr index
// split by separator
int stringData = 0; //variable to count data part nr
String dataPart = ""; //variable to hole the return text
for(int i = 0; i<data.length()-1; i++) { //Walk through the text one letter at a time
if(data[i]==separator) {
//Count the number of times separator character appears in the text
stringData++;
}else if(stringData==index) {
//get the text when separator is the rignt one
dataPart.concat(data[i]);
}else if(stringData>index) {
//return text and stop if the next separator appears - to save CPU-time
return dataPart;
break;
}
}
//return text if this is the last part
return dataPart;
}
Even after #user3441734 solved most of my problems 🙇, there are a few emoji that I can't seem to render properly when converting from a [String:String] to String.
Here's some Playground-ready code to illustrate the problem:
var u = ""
u = "1f468-1f468-1f467-1f467" // 👨👨👧👧
//u = "1f918-1f3ff" // 🤘🏿
//u = "1f468-2764-1f48b-1f468" // 👨❤💋👨 (broken)
//u = "1f3c7-1f3fb" // 🏇🏻 (broken)
let unicodeArray = u.characters.split("-")
.map(String.init)
.map {String(UnicodeScalar(Int($0,radix: 16) ?? 0))}
if let last = unicodeArray.last {
let separator: String
switch (unicodeArray.first, last) {
// Failed attempt to get tone applied to jockey
case let (horse_racing, _) where horse_racing == "\u{1f3c7}":
separator = "\u{200d}"
case let (_, tone) where "\u{1f3fb}"..."\u{1f3ff}" ~= tone:
separator = ""
case let (_, regionalIndicatorSymbol) where "\u{1f1e6}"..."\u{1f1ff}" ~= regionalIndicatorSymbol:
separator = ""
default:
separator = "\u{200d}"
}
print(unicodeArray.joinWithSeparator(separator))
}
Uncomment each assignment to u in turn to see the problem in action. The 3rd and 4th values should render like so:
and
Thoughts…
It turns out that a long-press on the race horse fails to show skin tones on iOS, so let's assume that's just an oversight, perhaps related to the near-impossibility of judging the jockey's skin tone at standard emoji sizes. I still can't figure out the problem with u = "1f468-2764-1f48b-1f468"
Apologies if this question comes out at all unclear. Chrome and Safari have different behaviors w.r.t these combo-emoji, so only the linked images are guaranteed to appear to you the way I see them on my end. 😢
These emoji are all either skin-tone renderings or tokens of same-sex affection. Is there some sort of bizarre latent racism & homophobia lurking in the system?! 😱 (Cue the conspiracy theories.)
Note that my attempt to use the zero-width joiner, u{200d} didn't help.
So, bug in Apple & Chrome's handling of certain emoji, or is there yet another idiosyncrasy of the standard that I've missed?
There's no conspiracy, the bugs are in your code.
The first character can be produced with:
U+1F468 U+200D U+2764 U+FE0F U+200D U+1F48B U+200D U+1F468
Note the ZERO WIDTH JOINER (U+200D) between each character, and the VARIATION SELECTOR-16 selector (U+FE0F) on the HEAVY BLACK HEART (U+2764) to ensure the emoji presentation style is used.
Refer to this table for a complete list of implemented multi-person groupings.
U+1F3C7 HORSE RACING is not an emoji modifier base, and so it does not support skin tone modifiers.
I have read this,but it can only work well in English for it just use white-space and something like NewlineCharacterSet as separator.
I want to add a left arrow and a right arrow in the accessory input view to move the cursor in UITextView by words.
And I am wondering how to support that feature for some Asian languages like Chinese
PS:I will added an example that CFStringTokenizer failed to work with when there are both English Characters and Chinese characters
test string:
Happy Christmas! Text view test 云存储容器测试开心 yeap
the expected boundaries:
Happy/ Christmas!/ Text/ view/ test/ 云/存储/容器/测试/开心/ yeap/
the boundaries show in reality:
Happy/ Christmas!/ Text/ view/ test/ 云存储容器测试开心/ yeap/
I don't speak Chinese, but according to the documentation,
CFStringTokenizer is able to find word boundaries in many languages,
including Asian languages.
The following code shows how to advance from one word ("world" at position 6)
to the next word ("This" at position 13):
// Test string.
NSString *string = #"Hello world. This is great.";
// Create tokenizer
CFStringTokenizerRef tokenizer = CFStringTokenizerCreate(NULL,
(__bridge CFStringRef)(string),
CFRangeMake(0, [string length]),
kCFStringTokenizerUnitWordBoundary,
CFLocaleCopyCurrent());
// Start with a position that is inside the word "world".
CFIndex position = 6;
// Goto current token ("world")
CFStringTokenizerTokenType tokenType;
tokenType = CFStringTokenizerGoToTokenAtIndex(tokenizer, position);
if (tokenType != kCFStringTokenizerTokenNone) {
// Advance to next "normal" token:
tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer);
while (tokenType != kCFStringTokenizerTokenNone && tokenType != kCFStringTokenizerTokenNormal) {
tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer);
}
if (tokenType != kCFStringTokenizerTokenNone) {
// Get the location of next token in the string:
CFRange range = CFStringTokenizerGetCurrentTokenRange(tokenizer);
position = range.location;
NSLog(#"%ld", position);
// Output: 13 = position of the word "This"
}
}
There is no CFStringTokenizerAdvanceToPreviousToken() function, so to move to
the previous word you have to start at the beginning of the string and advance forward.
Finnally I use UITextInputTokenizer to realized the function
My string is 'Hllo'.
I want to put inside it 'e' after the 'H' by its position, this case, position number 2.
local str = 'Hllo'
str = str:gsub('()',{[2]='e'})
You can simply cut contents until position you want to place your character on, then add the character and finally concat the characters on and after position.
src = "Hllo"
result = string.sub(src, 1, string.find(src, "H")) .. "e" .. string.sub(src, string.find(src, "H")+1)
The first part of code gets position of 'H' andf cuts the start (in this case 'H' only).
Second part adds character you want to insert. Third part adds every character after 'H' in source string to result.
you can try this out
$arr = str_split('hllo',1);
$result=$arr[0].'e'.$arr[1].$arr[2].$arr[3]