Skip to main content
July 17, 2011
Answered

SPECIFYING THE NUMBER OF IDENTICAL CHARACTERS WHEN SEARCHING A STRING

  • July 17, 2011
  • 2 replies
  • 1255 views

Hey everyone,

The built-in function indexOf() searches for a character "." in the string "1.2.3." if the value == 1.

It searches nothing if the value == 0 or 2 or any number greater than 2.

var txt:String = "1.2.3.";

var output:String = String(this);


if (txt.indexOf(".") == 1)

{

trace(output);     //[object MainTimeline]

}

The must be a simple method for specifying the number of characters "." when searching the string "1.2.3.".

For example if there is 2 characters "." in the string "1.2.", then run through a statement but if it does not contain 2 characters "." then do nothing.

There is a built-in function called contains but it is mainly used in XML.

indexOf() looks like unstable javascript codes, one of those framework that unixers hate.

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

You don't have to use regular expressions to figure how many dots are in the string. Use split and read length:

var txt:String = "1.2.3.";

trace(txt.split(".").length);

2 replies

Ned Murphy
Legend
July 17, 2011

I realize you reject the idea of using regular expressions for some reason, though I don't know why because they do provide the easiest way of searching strings for things.  Here is one way to find out how many "." characters there are in your string...

trace(txt.replace(/[^\.]*/g, "").length);

July 17, 2011

Yes right.

It would be great if there was some built-in flash alternative to RegExp though.

Even if I like XHTML and CSS, I reject them in flash and I still prefer to use the text format function.

Many people believe that XHTML and CSS has deficiency and that their place are not in flash and that it looks more like duplication while some people think they make flash more powerful.

Many people still say that flash is just a carbon coby because of the some similarities between as2 and javascript.

Ned Murphy
Legend
July 17, 2011

Why have an alternative when the built-in RegExp class does the job better than anything else?   Is it that you find them too hard to work with?  If you want a one line solution and prefer to use the older built-in methods you can do so by creating your own custom class to do what you want... searching a string character by character for the one you wish to isolate and counting them.

In any case, you have a solution to counting the number of "." characters.

"Many" people say what?  I doubt many people say anything like that at all.

Ned Murphy
Legend
July 17, 2011

I am not clear on what you are trying to say... "The built-in function indexOf() searches for a character "." in the string "1.2.3." if the value == 1.  It searches nothing if the value == 0 or 2 or any number greater than 2."

Can you clarify what that all means?

July 17, 2011

//code 1

var txt:String = "1.2.3.";

var output:String = String(this);

if (txt.indexOf(".") == 0)

{

trace(txt);

}


//code 2

var txt:String = "1.2.3.";

var output:String = String(this);

if (txt.indexOf(".") == 1)

{

trace(txt);     // 1.2.3.

}


//code 3

var txt:String = "1.2.3.";

var output:String = String(this);

if (txt.indexOf(".") == 2)

{

trace(txt);

}

The problem is that code 2 does not specify the how many "." there are.

We want to specify the number of characters when searching a string so that it becomes possible to set an if else statement.

Translating french to english is not difficult but complicated when you look at the excessive french grammar.

Ned Murphy
Legend
July 17, 2011

That still doesn't explain what your statement is trying to say, but it almost appears as if you are expecting the indexOf to return something other than what it does return.  As it says in the help documents, indexOf returns the index of the first occurence of the string that matches the string you specify.  So if your string is 1.2.3. then the first occurence is at index = 1.  It is not returning the number of occurences.

For your 3 different cases, only the middle one satisfies the conditional because there is not a "." character at index 0 and index 2 of the txt string (I should say, the first occurence is not at indexes 0 or 2).