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]
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 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);
I know I can find the total number of lines of a UILabel with .numberOfLines but how can I retrieve and edit the value say on line 2.
Example:
Assuming from your screen shot that each line is separated by a newline character you can split the text based on that.
Here is an example in Swift 3:
if let components = label.text?.components(separatedBy: "\n"), components.count > 1 {
let secondLine = components[2]
let editedSecondLine = secondLine + "edited"
label.text = label.text?.replacingOccurrences(of: secondLine, with: editedSecondLine)
}
You should make sure there is a value at whatever index your interested in. This example makes sure that there are more than a single component before retrieving the value.
You can then replace the second line with your edited line.
Hope that helps.
I'm learning from a book, and this is the assignment question i'm working on:
Create an app that asks for the users name and then displays the name down the side of the screen, one letter at a time.
Clarify what i'm trying trying to do: Have users name fade in one at a time vertically. Example: Adam "A" would appear after 1 second , "d" would appear after 3 seconds under the displayed A, "a" would appear after 5 seconds under the displayed d, "m" would appear after 7 seconds under the displayed a. The visuals would have a sort of domino effect.When they appear they would stay displayed on screen.
So far i'm able to get the user's name and display it side ways. Have it fade it in within 2 seconds. I'm stuck on how to get the letters to fade in one letter at a time.
function submit ()
print( "connect" )
userName = userNameField.text
display_userName = display.newText( userName, display.contentWidth-20, display.contentHeight/2 )
display_userName.rotation = 90
display_userName.alpha = 0
userNameField: removeSelf( )
greeting:removeSelf( )
submitButton:removeSelf( )
transition.fadeIn( display_userName, {time = 2000} )
Please let me know if you need to see more of my code.
You can do it in a simple way as below:
local myString = "Adam" -- Create your string
local positionCount = 0 -- initialize a variable to determine letter position
local function displayData()
positionCount = positionCount + 1
if(positionCount<=string.len(myString))then
-- if positionCount is less than or equal to letters in 'myString'
local letter = string.sub(myString, positionCount, positionCount) -- get the current letter
local letterLabel = display.newText(letter,20,20*positionCount,nil,20) -- place the letter
letterLabel.alpha = 0;
-- display the label and update the function after the completion of transition
transition.to(letterLabel,{time=1000,alpha=1,onComplete=displayData})
end
end
displayData()
Keep Coding.................... :)
Here is the code snippet for storing every character in a table.
Initialise a variable:
check =0;
Here splitWord is an table to store each character of string. and variable "yourStringForOneLetter" is your string variable for splitting. "string.sub" will split string into words using for loop.
if(check==wordSize) then
check=1
end
local wordSize = string.len(yourStringForOneLetter)
splitWord = {}
for i=check, check do
splitWord[i] = string.sub(yourStringForOneLetter, i, i)
check= check +1;
end
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