slice last few characters - actionscript

How do I remove the last 4 characters in a string?
'abcdefghijklmnopqrstuvwxyz'
to
'abcdefghijklmnopqrstuv'
Answer: slice(0,-4)

yourString = yourString.substring( 0, yourString.length - 4 ) would also work, if you need more practice typing and want your cpu to work harder.

Related

How to add leading zeroes to format?

I need a print function that writes Loop i / round, where both values i and round have to have at least four digits (12 -> 0012). It must have the format-method too.
I found the paste formatC and other ways to add leading zeroes but I can't make them work for this case..
It needs to be like this:
print('Loop {} / {}'.format('i', 'round'))
But with four digit numbers as I said.
number_str = str(a_number)
zero_filled_number = number_str.zfill(4)
a_number is 12.
zero_filled_number will give you 0012
Use print("{:02d}".format(1)) as found in this answer.

How can I replace a character in of the middle of a string with a random character?

I am checking the values of a string that is a unique identifier for a third party service that has some strict rules about the identifier, if a duplicate is generated I need to catch it and replace a character to make it unique. The Rules: It must be a string, it must be <= 21 characters long, the last four characters are significant and come preset and can't be altered, the first 15 characters are significant come preset and can't be altered, so I only have two characters that I can alter, and finally another third party system sets the string and will gladly duplicate them if the circumstances are right. They're always right. like. always... lol
At first I thought of using str.next! but that violates the last four rule. Then I tried str.insert(-5, rand(9).to_s) That would alter one of the correct characters and make the string unique, but it violates the <=21 characters rule.
str = "abcdefghijklmnoXX_123" (I can safely alter the XX)
str.next! (makes it unique but violates last four rule)
str.insert(-5, rand(9).to_s) (alters the correct characters and makes it unique, but violates the str.length rule.
How can I replace the correct character set without altering the string length or violating any further rules? Oh, It is also preferred that I not shorten the string length if possible.
I have assumed that the characters being replaced do not have to be random, but simply different from each other and different from all of the other characters in the string. If they are for some reason to be selected randomly, further specificity is required, specifically the collection of characters from which characters are to be drawn randomly. I have a further comment on this at the end of my answer.
REQD_BEGIN = 15
REQD_END = 4
PERMITTED_CHARS = ('a'..'z').to_a.join
#=> "abcdefghijklmnopqrstuvwxyz"
str = "abcdefrqsjklmnoXX_123"
nbr_replacements = str.size - REQD_BEGIN - REQD_END
#=> 2
available_chars =
PERMITTED_CHARS.delete(str[0,REQD_BEGIN].downcase +
str[-REQD_END, REQD_END].downcase)
#=> "ghiptuvwxyz"
str[0, REQD_BEGIN] + available_chars[0, nbr_replacements] +
str[-REQD_END, REQD_END]
#=> "abcdefrqsjklmnogh_123"
This does not modify ("mutate) str. To mutate the string, change the last line to:
s[REQD_BEGIN, nbr_replacements] = available_chars[0, nbr_replacements]
#=> "gh"
Now:
s #=> "abcdefrqsjklmnogh_123"
If the replacement characters are to be selected randomly (but satisfy the uniqueness properties set out at the onset), the constant PERMITTED_CHARS would be set equal to a string containing the characters from which a random sample would be drawn. available_chars would be computed as now, but available_chars[0, nbr_replacements] would be changed to available_chars.sample(nbr_replacements).
Clearest for me would be something like:
prefix = str[0..14]
middle = str[15..17]
suffix = str[18..-1]
unique_id = prefix + middle.next + suffix
If I understand right.

Only 2 emoji return an incorrect length when compared against a character set containing them

let myString = "â˜ēī¸"
let emoji = "😀😁😂😃😄😅😆😇😈đŸ‘ŋ😉😊â˜ēī¸đŸ˜‹đŸ˜ŒđŸ˜đŸ˜ŽđŸ˜đŸ˜đŸ˜‘đŸ˜’đŸ˜“đŸ˜”đŸ˜•đŸ˜–đŸ˜—đŸ˜˜đŸ˜™đŸ˜šđŸ˜›đŸ˜œđŸ˜đŸ˜žđŸ˜ŸđŸ˜ đŸ˜ĄđŸ˜ĸđŸ˜Ŗ😤đŸ˜ĨđŸ˜Ļ😧😨😩đŸ˜ĒđŸ˜ĢđŸ˜ŦđŸ˜­đŸ˜ŽđŸ˜¯đŸ˜°đŸ˜ąđŸ˜˛đŸ˜ŗ😴đŸ˜ĩđŸ˜ļ😷🙂🙃🙄🤔🙁☹ī¸đŸ¤’đŸ¤•đŸ¤‘đŸ¤“đŸ¤—đŸ¤đŸ¤ đŸ¤¤đŸ¤Ĩ🤧đŸ¤ĸ🤡đŸ¤Ŗ"
let characterSet = CharacterSet(charactersIn: emoji)
let range = (myString as NSString).rangeOfCharacter(from: characterSet)
(myString as NSString).substring(with: range)
(range as NSRange).location
(range as NSRange).length
(myString as NSString).length
substring == myString
This code can be ran in Playgrounds. Try changing myString to be any emoji face.
I'm using NSString and NSRange here as their values are easier to demonstrate, but this has the exact same behaviour with a Swift String or Range.
When I set myString to most of the face emojis, the range comes back as having a length of 2, and the substring can be used appropriately elsewhere. With only 2 face emojis - the "smiling face" emoji and "frowning face" emoji, the range comes back as a length of 1. In all cases, the length of the string comes back as 2. The substring with the given range of 1 is incomplete, and you can see that comparing it back to myString, as an example of comparing it to itself, gives a result of false. The result for the range of those 2 emojis should be 2.
Interestingly, looking at the unicode spec, those 2 emojis have vastly differently unicode values to their neighbours.
This seems like it may be an iOS bug. I can't think of anything I could be personally doing incorrectly here, as it works with all other emoji.
Hardly an answer but to much to fit into a comment so bear with me :)
I don't know if you've already seen this but I think your problem is addressed in the Platform State of the Union talk from WWDC 2017 (https://developer.apple.com/videos/play/wwdc2017/102/) in the section about what is new in Swift 4.
If you look at the video at about the 23 minutes 12 seconds mark you'll see Ted Kremenek talk about how they've fixed separating unicode characters out as expected in Swift 4 using Unicode 9 Grapheme Braking.
Also, have a look at this question and answer.
Yes...Don't ask me in detail what all this means, but it seems as if they're working on it :)

How can i show degree of expression in UILabel? (Swift)

I have a label which shows an expression:
(x+y)
But I want to show it in label like this:
(x+y)^2
(But with degree, I can't do it here, because I have too low reputation to insert images)
So, I want to show expression's degree in UIlabel.
Is it possible with single UILabel?
You can use Unicode characters of superscript two \u00B2, it it's always \u followed by the character code.
NSString *equation = [NSString stringWithFormat:#"(x+y)%#", #"\u00B2"];
Swift:
var equation = NSString(format:"(x+y)%#", "\u{00B2}") as String
Result:
http://unicode-table.com/en/
Strings and Characters (Apple iOS Developer Library )
Strings in Swift
I think you are looking for powers e.g. (x + y)⁚.
For this, You have to use unicodes.
you can take list of unicodes character list from here;
http://www.fileformat.info/info/unicode/category/No/list.htm
In code, you will use;
print("(x+y)\u{00B2}");

Isolating/removing Characters from string using rails

I am using ruby on rails
I have
article.id = 509969989168Q000475601
I would like the output to be
article.id = 68Q000475601
basically want to get rid of all before it gets to 68Q
the numbers in front of the 68Q can be various length
is there a way to remove up to "68Q"
it will always be 68Q and Q is always the only Letter
is there a way to say remove all characters from 2 digits before "Q"
I'd use:
article.id[/68Q.*/]
Which will return everything from 68Q to the end of the string.
article.id.match(/68Q.+\z/)[0]
You can do this easily with the split method:
'68Q' + article.id.split('68Q')[1]
This splits the string into an array based on the delimiter you give it, then takes the second element of that array. For what it's worth though, #theTinMan's solution is far more elegant.

Resources