swift/parse: incrementing strings - string

The Swift part of the question:
So what I mean by incrementing strings is that say we start off with var string = "title" I want to be able to increment numbers to the end of that like "title1", "title2", "title3...". Should I use a for loop to do this? If so, how? Or another method?
for var i = 1; i < 6; i = i + 1 {
//increment the strings here
}
The Parse part of the question:
I want to have my objectForKey use the many different titles and numbers we will produce above so that the objectForKey will be "title1", "title2", "title3"... I would make multiple columns on Parse with names " title1, title2, title3 and the cells in the tableview would correspond to that data. So cell1 would use title1's data, cell2 will use title2's data and so on. Will it work like this?
var output1 = object.objectForKey(i) as! String

A loop in Swift is like for i in 1...5, and then you can use string interpolation to get the correct string like this:
for i in 1...5 {
let title = "title\(i)"
print(title)
}
Also read Dan's answer.

There are a few ways of looping in Swift, but you should keep in mind that and as of Swift 3,
this will no longer be one of them:
for var i = 0; i <6; i++ {
let string = "title\(i+1)"
}
source : Swift Evolution
Swift's preferred way of general looping is, as GvS stated:
for i in 1...5 {
let title = "title\(i)"
}
However, you are also welcome to use Swifts higher order functions to loop:
(1...5).forEach { i in
let title = "title \(i)"
}
or
(1...5).forEach { let title = "title \($0)" }

Related

checking for a contains in a specific string in cosmosDB graph using pythongremlin api

I have a 2 "WEEK" vertices on azure cosmosdb graph.
g.V().hasLabel('WEEK').valueMap()
output:
{
"type":["1 week|1 month|1 wk|one month|one week|one wk"]
},
{
"type":["11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks"]
}
i am trying to search CONTAINS of a STRING in the "type" property and return the vertices.
STRING = "1 week"
g.V().hasLabel('WEEK').has('type',TextP.containing('1 week')).valueMap()
output:
{
"type":["1 week|1 month|1 wk|one month|one week|one wk"]
},
{
"type":["11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks"]
}
i am getting all the vertices because "11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks" also have '1 week' in it.
my requirement is that i have to search for the contains operation but only 1st vertex should be present not the second one.
one idea can be changing the data in the "type" property and change the search string as below
g.V().hasLabel('WEEK').valueMap()
output:
{
"type":["(1 week)|(1 month)|(1 wk)|(one month)|(one week)|(one wk)"]
},
{
"type":["(11 weeks)|(11 months)|(11 wks)|(eleven months)|(eleven weeks)|(eleven wks)"]
}
STRING = "(1 week)"
g.V().hasLabel('WEEK').has('type',TextP.containing('(1 week)')).valueMap()
output:
{
"type":["(1 week)|(1 month)|(1 wk)|(one month)|(one week)|(one wk)"]
}
but this way we need to change the entire data in the "type" property and have to change the STRING as well from "1 week" to "(1 week)" (as "1 week" is received from upstream)
Please let me know any other ideas for the above scenario (doing contains is mandatory)
Thanks in advance.
You could place a | at the beginning of the data and then look for
STRING= "|1 week"
You could search for items that contain your search-string + have the same amount of characters?
Your question is kind of confusing, but something like the below should return ONLY LIST[0] being as it's only LIST[2]. It'll only return the string in the first array position which would be LIST[0]
public string[] LIST = { "1 week|1 month|1 wk|one month|one week| one wk","11 weeks|11 months|11 wks|eleven months|eleven weeks|eleven wks"};
public string STRING = "1 Week";
public int LISTLength = LIST.Length;
for(int x = 0; x < LISTLength; x++)
{
if (LIST[x] == STRING)
{
//Your action here
}
}

How can I create a list of words that contains upper case letters in swift?

I would like to generate a list of words form a given string where each listed word contains at least an upper case letter.
Having a string like this one:
let str: String = "Apple watchOS 3 USA release date and feature rumours."
I would like to get an array like this:
var list: [String] = ["Apple", "watchOS", "USA"]
What is the best way to do this?
var list = str.componentsSeparatedByString(" ").filter{ $0.lowercaseString != $0 }
You can use built in function of String in Swift that get all words of a string. It's better than just separate by a space (" ") cause you can have some words with a point (like the example below)
let str = "Apple watchOS 3 USA release date and feature Rumours."
var list = [String]()
str.enumerateSubstringsInRange(str.startIndex..<str.endIndex, options:.ByWords) {
(substring, substringRange, enclosingRange, value) in
//add to your array if lowercase != string original
if let _subString = substring where _subString.lowercaseString != _subString {
list.append(_subString)
}
}
You can probably do something like this. I haven't tested it, just wrote it real quick.
let str = "Apple watchOS 3 USA release date and feature rumours."
let strArr = str.componentsSeparatedByString(" ")
var upperWords: Array<String> = []
for word in strArr {
if word != word.lowercaseString {
upperWords.append(word)
}
}

Using loops to go through string and check char as a dict key

so I want to kind of build a "Decrypter", I have a dictionary with the keys being the symbol, and the value the respective value for the symbol, then I have this string that the code is suppose to look into, the translate will be saved in a other string, in this case called output. This is the way I did the loop part, but is not working:
var outputText = " "
for character in textForScan{
for key in gematriaToLetters{
if (gematriaToLetters.keys == textForScan[character]){
outputText.insert(gematriaToLetters.values, atIndex: outputText.endIndex)
}
}
}
You could also consider using map:
let outputText = "".join(map(textForScan) { gematriaToLetters[String($0)] ?? String($0) })
If you don't specify a specific letter in the dictionary it returns the current letter without "converting".
I think you are looking for something like this:
for aCharacter in textForScan {
let newChar = gematrialToLetters["\(aCharacter)"]
outputText += newChar
}
print(outputText)

AS3 "Advanced" string manipulation

I'm making an air dictionary and I have a(nother) problem. The main app is ready to go and works perfectly but when I tested it I noticed that it could be better. A bit of context: the language (ancient egyptian) I'm translating from does not use punctuation so a phrase canlooklikethis. Add to that the sheer complexity of the glyph system (6000+ glyphs).
Right know my app works like this :
user choose the glyphs composing his/r word.
app transforms those glyphs to alphanumerical values (A1 - D36 - X1A, etc).
the code compares the code (say : A5AD36) to a list of xml values.
if the word is found (A5AD36 = priestess of Bast), the user gets the translation. if not, s/he gets all the possible words corresponding to the two glyphs (A5A & D36).
If the user knows the string is a word, no problem. But if s/he enters a few words, s/he'll have a few more choices than hoped (exemple : query = A1A5AD36 gets A1 - A5A - D36 - A5AD36).
What I would like to do is this:
query = A1A5AD36 //word/phrase to be translated;
varArray = [A1, A5A, D36] //variables containing the value of the glyphs.
Corresponding possible words from the xml : A1, A5A, D36, A5AD36.
Possible phrases: A1 A5A D36 / A1 A5AD36 / A1A5A D36 / A1A5AD36.
Possible phrases with only legal words: A1 A5A D36 / A1 A5AD36.
I'm not I really clear but to things simple, I'd like to get all the possible phrases containing only legal words and filter out the other ones.
(example with english : TOBREAKFAST. Legal = to break fast / to breakfast. Illegal = tobreak fast.
I've managed to get all the possible words, but not the rest. Right now, when I run my app, I have an array containing A1 - A5A - D36 - A5AD36. But I'm stuck going forward.
Does anyone have an idea ? Thank you :)
function fnSearch(e: Event): void {
var val: int = sp.length; //sp is an array filled with variables containing the code for each used glyph.
for (var i: int = 0; i < val; i++) { //repeat for every glyph use.
var X: String = ""; //variable created to compare with xml dictionary
for (var i2: int = 0; i2 < val; i2++) { // if it's the first time, use the first glyph-code, else the one after last used.
if (X == "") {
X = sp[i];
} else {
X = X + sp[i2 + i];
}
xmlresult = myXML.mot.cd; //xmlresult = alphanumerical codes corresponding to words from XMLList already imported
trad = myXML.mot.td; //same with traductions.
for (var i3: int = 0; i3 < xmlresult.length(); i3++) { //check if element X is in dictionary
var codeElement: XML = xmlresult[i3]; //variable to compare with X
var tradElement: XML = trad[i3]; //variable corresponding to codeElement
if (X == codeElement.toString()) { //if codeElement[i3] is legal, add it to array of legal words.
checkArray.push(codeElement); //checkArray is an array filled with legal words.
}
}
}
}
var iT2: int = 500 //iT2 set to unreachable value for next lines.
for (var iT: int = 0; iT < checkArray.length; iT++) { //check if the word searched by user is in the results.
if (checkArray[iT] == query) {
iT2 = iT
}
}
if (iT2 != 500) { //if complete query is found, put it on top of the array so it appears on top of the results.
var oldFirst: String = checkArray[0];
checkArray[0] = checkArray[iT2];
checkArray[iT2] = oldFirst;
}
results.visible = true; //make result list visible
loadingResults.visible = false; //loading screen
fnPossibleResults(null); //update result list.
}
I end up with an array of variables containing the glyph-codes (sp) and another with all the possible legal words (checkArray). What I don't know how to do is mix those two to make legal phrases that way :
If there was only three glyphs, I could probably find a way, but user can enter 60 glyphs max.

Swift - Finding a substring between two locations in a string

I have a string that is formatted like this: "XbfdASF;FBACasc|Piida;bfedsSA|XbbnSF;vsdfAs|"
Basiclly its an ID;ID| and then it repeats.
I have the first ID and I need to find it's partner Example: I have 'Piida' and I need to find the String that follows it after the ';' which is 'bfedsSA'
How do I do this?
The problem I am having is that the length of the IDs is dynamic so I need to get the index of '|' after the ID I have which is 'Piida' and then get the string that is between these indexes which in this case should be 'bfedsSA'.
There are many ways to do this, but the easiest is to split the string into an array using a separator.
If you know JavaScript, it's the equivalent of the .split() string method; Swift does have this functionality, but as you see there, it can get a little messy. You can extend String like this to make it a bit simpler. For completeness, I'll include it here:
import Foundation
extension String {
public func split(separator: String) -> [String] {
if separator.isEmpty {
return map(self) { String($0) }
}
if var pre = self.rangeOfString(separator) {
var parts = [self.substringToIndex(pre.startIndex)]
while let rng = self.rangeOfString(separator, range: pre.endIndex..<endIndex) {
parts.append(self.substringWithRange(pre.endIndex..<rng.startIndex))
pre = rng
}
parts.append(self.substringWithRange(pre.endIndex..<endIndex))
return parts
} else {
return [self]
}
}
}
Now, you can call .split() on strings like this:
"test".split("e") // ["t", "st"]
So, what you should do first is split up your ID string into segments by your separator, which will be |, because that's how your IDs are separated:
let ids: [String] = "XbfdASF;FBACasc|Piida;bfedsSA|XbbnSF;vsdfAs|".split("|")
Now, you have a String array of your IDs that would look like this:
["XbfdASF;FBACasc", "Piida;bfedsSA", "XbbnSF;vsdfAs"]
Your IDs are in the format ID;VALUE, so you can split them again like this:
let pair: [String] = ids[anyIndex].split(";") // ["ID", "VALUE"]
You can access the ID at index 0 of that array and the value at index 1.
Example:
let id: String = ids[1].split(";")[0]
let code: String = ids[1].split(";")[1]
println("\(id): \(code)") // Piida: bfedsSA

Resources