Highlighted

How to find out what word doesn't start with a "#"?

Explorer ,
Apr 24, 2015

Copy link to clipboard

Copied

Hi,

I need to find out the position in the text for the first word that doesn't contain a leading hashtag (#) - starting from the end of the text.

This is a demo text:

var demoText:String = "hello world #dontremoveme here #1remove #2removeme #3removeüäüö

The position would be after the word here.

How do do that?

For quick and simple code, you can use String methods to get the character index of the "#". Below is an example that gets the index of the space character that is directly in front of the second "#". This code example assumes you are wanting to create a new String that has everything up to the second "#" character.

var demoText:String = "hello world #dontremoveme here #1remove #2removeme #3remove";

var index1:int = demoText.indexOf("#");// find first instance of "#" in the String

var index2:int = demoText.indexOf("#", index1 + 1) - 1;// find the index of the space character right before the second instance of "#"

var newText:String = demoText.substring(0, index2);// newText = "hello world #dontremoveme here"

To explain line 3, you are searching for the index of the "#" character starting the search from the index of the first "#" + 1. The "+ 1" is to have the search start after the first "#" character. If you don't have that, you would keep finding the same index value as "index1". The "- 1" at the end is to decrease the index2 value so it will give you index of the space character directly in front of the "#". Line 4 creates a new String starting from the beginning of the original String up to, but not including, the space character in front of the second "#".

If you needed something more complicated than just removing everything after "here" then I would need a different example.

Edit: Here is a link to the AS3 API for the String class. String - Adobe ActionScript® 3 (AS3 ) API Reference

TOPICS
Development

Views

293

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to find out what word doesn't start with a "#"?

Explorer ,
Apr 24, 2015

Copy link to clipboard

Copied

Hi,

I need to find out the position in the text for the first word that doesn't contain a leading hashtag (#) - starting from the end of the text.

This is a demo text:

var demoText:String = "hello world #dontremoveme here #1remove #2removeme #3removeüäüö

The position would be after the word here.

How do do that?

For quick and simple code, you can use String methods to get the character index of the "#". Below is an example that gets the index of the space character that is directly in front of the second "#". This code example assumes you are wanting to create a new String that has everything up to the second "#" character.

var demoText:String = "hello world #dontremoveme here #1remove #2removeme #3remove";

var index1:int = demoText.indexOf("#");// find first instance of "#" in the String

var index2:int = demoText.indexOf("#", index1 + 1) - 1;// find the index of the space character right before the second instance of "#"

var newText:String = demoText.substring(0, index2);// newText = "hello world #dontremoveme here"

To explain line 3, you are searching for the index of the "#" character starting the search from the index of the first "#" + 1. The "+ 1" is to have the search start after the first "#" character. If you don't have that, you would keep finding the same index value as "index1". The "- 1" at the end is to decrease the index2 value so it will give you index of the space character directly in front of the "#". Line 4 creates a new String starting from the beginning of the original String up to, but not including, the space character in front of the second "#".

If you needed something more complicated than just removing everything after "here" then I would need a different example.

Edit: Here is a link to the AS3 API for the String class. String - Adobe ActionScript® 3 (AS3 ) API Reference

TOPICS
Development

Views

294

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 24, 2015 0
Engaged ,
Apr 24, 2015

Copy link to clipboard

Copied

For quick and simple code, you can use String methods to get the character index of the "#". Below is an example that gets the index of the space character that is directly in front of the second "#". This code example assumes you are wanting to create a new String that has everything up to the second "#" character.

var demoText:String = "hello world #dontremoveme here #1remove #2removeme #3remove";

var index1:int = demoText.indexOf("#");// find first instance of "#" in the String

var index2:int = demoText.indexOf("#", index1 + 1) - 1;// find the index of the space character right before the second instance of "#"

var newText:String = demoText.substring(0, index2);// newText = "hello world #dontremoveme here"

To explain line 3, you are searching for the index of the "#" character starting the search from the index of the first "#" + 1. The "+ 1" is to have the search start after the first "#" character. If you don't have that, you would keep finding the same index value as "index1". The "- 1" at the end is to decrease the index2 value so it will give you index of the space character directly in front of the "#". Line 4 creates a new String starting from the beginning of the original String up to, but not including, the space character in front of the second "#".

If you needed something more complicated than just removing everything after "here" then I would need a different example.

Edit: Here is a link to the AS3 API for the String class. String - Adobe ActionScript® 3 (AS3 ) API Reference

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2015 1
Explorer ,
Apr 24, 2015

Copy link to clipboard

Copied

Many thanks! You saved my weekend!

cheers

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2015 0
Adobe Community Professional ,
Apr 24, 2015

Copy link to clipboard

Copied

Try this, though I'm sure there will be some problem cases:

var demoText:String = "hello world #dontremoveme here #1remove #2removeme #3removeüäüö"

while (demoText.lastIndexOf(" ")==demoText.lastIndexOf(" #")){

  demoText = demoText.slice(0,demoText.lastIndexOf(" "));

}

trace(demoText);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2015 1
Explorer ,
Apr 24, 2015

Copy link to clipboard

Copied

Hi, Thanks it works great until the first hashtag is in a new line, e.g.;

var demoText:String = "hello world #dontremoveme here

#1remove #2removeme #3removeüäüö"

Any ideas how to make it work for that too?

Many thanks

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2015 0
Adobe Community Professional ,
Apr 24, 2015

Copy link to clipboard

Copied

There may be more problem cases, but try this:

var demoText:String = "hello world #dontremoveme here"

//var demoText:String = "hello world #dontremoveme here #1remove #2removeme #3removeüäüö"

if(demoText.split(" #").length > 1){

  while (demoText.lastIndexOf(" ")==demoText.lastIndexOf(" #")){

  demoText = demoText.slice(0,demoText.lastIndexOf(" "));

  }

}

trace(demoText);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2015 0
Adobe Community Professional ,
Apr 24, 2015

Copy link to clipboard

Copied

I think I meant:

if(demoText.split(" #").length > 2){

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2015 0