Chopping up a string

  • bayes
  • Newbie
  • Newbie
  • bayes
  • Posts: 7

Post 3+ Months Ago

Quite a simple thing to do really, but Im not getting the results I need.

I've just made a wee bit of code to split up a string, using a "," as the delimiter - only problem is that the String.Split() function in flash only seems to return a series of bits rather than the specific one I want.

For example:

Code: [ Select ]
function splitHas(){
    for (splitCount=1;splitCount<5;splitCount++){
        my_str = parseArray[2].has;
        split1 = my_str.split(",",1);
        split2 = my_str.split(",",2);
        split3 = my_str.split(",",3);
        split4 = my_str.split(",",4);
    }
}
  1. function splitHas(){
  2.     for (splitCount=1;splitCount<5;splitCount++){
  3.         my_str = parseArray[2].has;
  4.         split1 = my_str.split(",",1);
  5.         split2 = my_str.split(",",2);
  6.         split3 = my_str.split(",",3);
  7.         split4 = my_str.split(",",4);
  8.     }
  9. }


Using this:

Code: [ Select ]
scaly_skin,mouth,cold_blood

Just gives me:

Code: [ Select ]
scaly_skin
scaly_skin,mouth
scaly_skin,mouth,cold_blood
  1. scaly_skin
  2. scaly_skin,mouth
  3. scaly_skin,mouth,cold_blood


I want to be able to strip out the specific section, eg - the 2nd section (mouth) and just stick that in a variable. Is that possible with the String.Split() function or should I be using something else?
  • stinger
  • Graduate
  • Graduate
  • stinger
  • Posts: 157
  • Loc: San Jose, CA

Post 3+ Months Ago

Hello,

It seems that what you are doing is correct. Just missing a small variable.

Code: [ Select ]
function splitHas(){
  for (splitCount=1;splitCount<5;splitCount++){
   my_str = parseArray[2].has;
   split1 = my_str.split(",",1);
   split2 = my_str.split(",",2);
   split3 = my_str.split(",",3);
   split4 = my_str.split(",",4);
  }
}
  1. function splitHas(){
  2.   for (splitCount=1;splitCount<5;splitCount++){
  3.    my_str = parseArray[2].has;
  4.    split1 = my_str.split(",",1);
  5.    split2 = my_str.split(",",2);
  6.    split3 = my_str.split(",",3);
  7.    split4 = my_str.split(",",4);
  8.   }
  9. }


should be function
Code: [ Select ]
splitHas(passedVar){

my_str = passedVar.split(",");
   split1 = my_str[0];
   split2 = my_str[1];
   split3 = my_str[2];
   split4 = my_str[3];
  }
}
  1. splitHas(passedVar){
  2. my_str = passedVar.split(",");
  3.    split1 = my_str[0];
  4.    split2 = my_str[1];
  5.    split3 = my_str[2];
  6.    split4 = my_str[3];
  7.   }
  8. }


I changed your variable so that you can use this function multiple times, just by sending a new variable in the function call. You may want to send 2 variables, 1 being what needs splitting, and the other being the new array that will be created.
  • UNFLUX
  • Genius
  • Genius
  • User avatar
  • Posts: 6375
  • Loc: twitter.com/unflux

Post 3+ Months Ago

will you guys please use the code tag when posting? thanks. :)
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

String.split() takes a string value splits a string where the delimiter is and each of those items becomes an item in an array. You can then choose the correct array position for the word you want.

For example...
Code: [ Select ]
myString = "scaly_skin,mouth,cold_blood";
splitString = myString.split(",");
trace(splitString[0]);//returns scaly_skin
trace(splitString[1]);//returns mouth
trace(splitString[2]);//returns cold_blooded
  1. myString = "scaly_skin,mouth,cold_blood";
  2. splitString = myString.split(",");
  3. trace(splitString[0]);//returns scaly_skin
  4. trace(splitString[1]);//returns mouth
  5. trace(splitString[2]);//returns cold_blooded


Is this what you are trying to do?
  • Ami
  • Newbie
  • Newbie
  • Ami
  • Posts: 6
  • Loc: Malaysia

Post 3+ Months Ago

bayes , i think wat i do is slightly some same with u , just want to ask is that u use the external txt file for the array??

guys, help me to see my code . im using an external txt file to keep my array.
Code: [ Select ]
sola=5x+67x-3&solb=2x=9&solc=x=4.5

n at the button's action i put this :
  1. sola=5x+67x-3&solb=2x=9&solc=x=4.5
  2. n at the button's action i put this :


on(release){
loadText = new LoadVars();
loadText.load("dataold.txt");
loadText.onLoad = function() {
sola.text = this.sola;
solb.text = this.solb;
solc.text = this.solc;
};
}

the sola , solb, solc is a dynamic text name in flash.

from the code it gives me all the answer together .but wat i want is the answer come out 1 by 1 after clicking the button.

first time click the button come out 5x+67x-3 , second time click the same button again come out 2x=9 n lastly come out x=4.5.

so how im going to split the array to show the answer 1 by 1??

help please...
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Bayes,
Lostinbeta's post should pretty much sum it up :P I just like to expand :D

Using a prototype could give you an string section search like so,
Code: [ Select ]
String.prototype.getSectionByDelimiter = function(section,delimiter) {
    var tmp = this.split(delimiter);
    return tmp[section];
}
  1. String.prototype.getSectionByDelimiter = function(section,delimiter) {
  2.     var tmp = this.split(delimiter);
  3.     return tmp[section];
  4. }


used like so,

Code: [ Select ]
myStr = "a,b,c,d,e";
varToFill = myStr.getSectionByDelimiter(2,",");

trace(varToFill); // returns c
  1. myStr = "a,b,c,d,e";
  2. varToFill = myStr.getSectionByDelimiter(2,",");
  3. trace(varToFill); // returns c


edit : I just noticed the age of this topic :P
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

You could load in your values like this...

Code: [ Select ]
loadText = new LoadVars();
loadText.onLoad = function() {
    equations = [this.sola, this.solb, this.solc];
    sola.text = equations[0];
};
loadText.load("dataold.txt");
  1. loadText = new LoadVars();
  2. loadText.onLoad = function() {
  3.     equations = [this.sola, this.solb, this.solc];
  4.     sola.text = equations[0];
  5. };
  6. loadText.load("dataold.txt");


That will load them in an array by default and show the first one in the first textbox.

The rest is just incrementing the array place value to show the next one up (and targeting the correct textfield).
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

So used with on(release) could look somthing like this ?

Code: [ Select ]
on(release){
    if(equations == undefined){
        loadText = new LoadVars();
        loadText.onLoad = function() {
            equations = [this.sola, this.solb, this.solc];
            sola.text = equations[0];
        };
        loadText.load("dataold.txt");
    }else if(solb.text == ""){
        solb.text = equations[1];
    }else if(solc.text == ""){
        solc.text = equations[2];
    }
}
  1. on(release){
  2.     if(equations == undefined){
  3.         loadText = new LoadVars();
  4.         loadText.onLoad = function() {
  5.             equations = [this.sola, this.solb, this.solc];
  6.             sola.text = equations[0];
  7.         };
  8.         loadText.load("dataold.txt");
  9.     }else if(solb.text == ""){
  10.         solb.text = equations[1];
  11.     }else if(solc.text == ""){
  12.         solc.text = equations[2];
  13.     }
  14. }
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Well you don't need to call the LoadVars() in the onrelease all the time, once the data is loaded, you can reuse it however you want. So the loadVars code should be contained on a frame.

As for the rest of it... the if/else method is one way you could do it. There are a few others, but they are all kind of blurred together in my head at the moment (sorta kinda hungover).
  • Ami
  • Newbie
  • Newbie
  • Ami
  • Posts: 6
  • Loc: Malaysia

Post 3+ Months Ago

lost in beta & joebert :

hi, i already tyr urs code n put in check button's action. Both of them just can load the first answer .

The another question is y when i test the fla file got the word like (_level0.sola,_level0.solb._level0.solc) in the targeted dynamic text before i click the check button.


what is the problem? i had try another code like this :
[code]click_array=new array();
click_array=["","5x+6=7x-3","2x=9","x=4.5"];

[code]on(release){
textBox.text = click_array[i];
i++;
}

it can let me show the answer 1 by 1 , but it cannot stop the answer n the answer is in the same dynamic text not in the targeted dynamic text as above code.


can help me find out how to correct it ?

thanks

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 3 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.