Skip to main content
Known Participant
April 24, 2015
Answered

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

  • April 24, 2015
  • 2 replies
  • 764 views

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?

This topic has been closed for replies.
Correct answer

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

2 replies

Colin Holgate
Inspiring
April 24, 2015

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);

blogtomAuthor
Known Participant
April 24, 2015

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

Colin Holgate
Inspiring
April 24, 2015

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);

Correct answer
April 24, 2015

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

blogtomAuthor
Known Participant
April 24, 2015

Many thanks! You saved my weekend!

cheers