I have two strings. stringOne and stringTwo
I have code that I want to run for a variable named TXT
How can I do like...
for (stringOne and for stringTwo) {
TXT = stringOne (or) stringTwo;
...
stringOne or stringTwo = TXT;
}
You have to take those two strings in an array and use foreach to do some actions on the strings.
see this.
http://www.barklund.org/blog/2009/05/21/for-each-in-loops-in-actionscript-3/
Related
I'm a beginner in dart.
void main() {
var abf = '+37.4054-122.0999/';
var abf2;
abf2 = abf.replaceAll("+"," ");
var abf1 = abf2.split(RegExp('(?=[+-])'));
print (abf1[0]);
print (abf1[1]);
}
The above code splits abf into two values for me
I want to remove the ending '/'. I tried many split methods using other variables but it's not removing the '/' even though its removing the '+'.
It's not really clear what you're trying to do with the split.
But if you're looking the remove the / this should work:
String number = '+37.4054-122.0999/';
number = number.replaceAll("/"," ");
You can create substring from this while you like to remove last element.
String abf = '+37.4054-122.0999/';
final result = abf.substring(0, abf.length - 1);
print(result);
Dart's List class has a built-in removeLast method. Maybe you can try to split the string and then removing the last element:
String str = "str";
String newStr = str.split(''). removeLast().join('');
I'm working with a framework and have to change some things for my project.
The original is
var tree:FBQuadTree? = nil
...
if tree == nil {
tree = FBQuadTree()
}
I need always a custom number of trees. So my consideration was that I use an array for this.
var tree:[FBQuadTree?] = []
...
if tree[number] == nil {
tree[number] = FBQuadTree()
}
But I don't know how to fill my new tree array. Sure I could do smth. like this:
let element:FBQuadTree?
layersTree.append(element)
Okay, but my problem is that number of elements isn't static. So sometimes there is just one element, sometimes a lot more.
Is that possible what I want to do?
the whole thing:
var array:[FBQuadTree] = []
//fill
for i in 0..<MyDynamicSize {
array.append(//how to fill?)
}
//give new values
for i in 0..<MyDynamicSize {
array[i] = FBQuadTree()
}
If you're just trying to get an Array of a myDynamicSize* number of FBQuadTree instances, you can use init(repeating:count:).
var array = [FBQuadTree](count: myDynamicSize, repeatedValue: FBQuadTree())
Sidenote: by convention, types start with capitals, whereas variable names start with lowercase letters. So myDynamicSize, rather than MyDynamicSize
please try this
var array:[FBQuadTree?] = []
//fill
for i in 0..<MyDynamicSize {
array.append(nil)
}
I would like to store a different count of String data pairs in this format:
string a[n][2]{
{"1","2"},
{"3","4"},
{"5","6"}
{... }};
I would like to add a new pair by using the append methode or something similar... But is that possible and nice to handle with a 2D array or should I use a dictionaire?? And how could I implement that? Thanks!
You could potentially use an array of tuples to solve this:
typealias StringPair = (String, String)
var myStrings = [StringPair]()
myStrings.append(StringPair("1", "2"))
Edit: To search for a string pair, you could do:
var doesItContain1 = myStrings.contains {
$0.0 == "1"
}
You could also change your type alias to use names:
typealias StringPair = (string1: String, string2: String)
And then filter with:
var doesItContain1 = myStrings.contains {
$0.string1 == "1"
}
Edit 2: To sort:
var sorted = myStrings.sort {
return $0.string1 < $1.string1
}
I am having a large text file that will be use as an English dictionary database. (A real dictionary)
The user will type a word in a Search Bar and the app will return all the line of the text file that contains this word.
*-Each line explains the meaning of one word
-Some words are located on more than one line in my text file*
Here is my logic:
First I am looking for the word in self.entriesFromFile (which is one String of my Text File):
var range:NSRange? = self.entriesFromFile!.rangeOfString(searchText)
Then I find the Substring:
var substring:NSString = self.entriesFromFile!.substringToIndex(range!.location)
I split the Substring into lines
var substringArray:NSArray = substring.componentsSeparatedByString("\n")
Like this substringArray.count will return the current line where searchText has been found. I need the line number to identify the word in the dictionary for further functions.
I used this logic because I need it to be fast for the user, the text file contains more than 60,000 lines.
This works fine but it only return the first entry that have been found.
Could you help me find how to return all the lines number containing the searched word?
Thank you for your precious help
// I assume that searchText already has all file content
var lines = searchText.componentsSeparatedByString("\n")
var lineNumber = 0
for line in lines {
// It will fire 60.000 times
if string.rangeOfString("Some string you are searching for") != nil {
linesNumbers.append(lineNumber)
}
lineNumber++
}
Enumerate the lines and search each of them:
var lines: [String] = []
var lineNumber = 1; // or start with 0
self.entriesFromFile.enumerateLinesUsingBlock { line, stop in
if line.rangeOfString(searchedText) != nil {
lines.append(line) // or store the lineNumbers
}
lineNumber++
}
I wrote the code directly here, it may need changes to compile :P
I want a simple function that can remove part of a string, eg:
var foo="oranges";
trace(removeStrings(foo,'rang'));
I want the above output as 'oes'. Any help will be greatly appreciated.
Thanks in advance
A quick solution for removing substrings is to use split with the string that you want to remove as delimiter and then join the result:
function removeSubString(str, remove):String {
return str.split(remove).join("");
}
Another way to do this is
function removeStrings(originalString, pattern):String
{
return originalString.replace(pattern, "");
}
For more information about Strings in AS3 you can visit:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html
I should mention that the code above is not going to change your String, so if you need to use the property originalString with the new value you should use:
originalString = removeStrings(originalString, pattern);
The second thing that I should mention is that the replace method will replace the first appearance of the pattern, so if you need to replace every match of the pattern you should do something like
while(originalString.search(pattern) != -1)
{
originalString = removeStrings(originalString, pattern);
}
Hope this will help!
Ivan Marinov
I'm using by long time this snippet, which as the advantage to be available to all string objects on your movie:
String.prototype.replace = function(pattern, replacement) {
return this.split(pattern).join(replacement);
}
can be used in this way:
var str = "hello world";
var newstr = str.replace("world", "abc");
trace(newstr);
as you can see the string class have been extended with the replace method.