Jump to content

Remove word from string


Wurdy
 Share

You are about to reply to a thread that has been inactive for 1916 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

The function is llDeleteSubString: http://wiki.secondlife.com/wiki/LlDeleteSubString.

llSubStringIndex will return the location of your word in the string: http://wiki.secondlife.com/wiki/LlSubStringIndex.

llStringLength could be used to determine the length of your word (unless you prefer to hard code it): http://wiki.secondlife.com/wiki/LlStringLength.

So,

integer index = llSubStringIndex (original_string, my_word);
integer my_word_length = llStringLength (my_word);
modifed_string = llDeleteSubString (original_string, index, index + my_word_length);

Note: not tested inworld. And you may want to consider the possibility that your word isn't part of the original string: llSubStringIndex will return -1 in this case, and that should be caught before it's fed into llDeleteSubString, where it has a special meaning.

Edited by KT Kingsley
  • Like 1
Link to comment
Share on other sites

How about casting the sentence into a list, using

list temp = llParseString2List("The quick brown fox jumped over the lazy dog.",[" "],["."]);

Then you find the index of the item you want to remove using

integer index = llListFindList(temp, ["lazy"];

Then, if you find it, remove it with temp = llDeleteSubList(temp, index, index);

And finally use llDumpList2String(temp, " "); to turn the modified list  back into a string.

Not tested in world, either, but that's the approach I usually take to this kind of problem.

 

Edited by Innula Zenovka
  • Like 2
Link to comment
Share on other sites

I can see two reasons why Innula's method is preferable to mine. Firstly, my method would leave a double space at the place in the string where the word was removed, and secondly, my method won't distinguish between the word alone and another longer word that includes the word in question.

Link to comment
Share on other sites

33 minutes ago, KT Kingsley said:

I can see two reasons why Innula's method is preferable to mine. Firstly, my method would leave a double space at the place in the string where the word was removed, and secondly, my method won't distinguish between the word alone and another longer word that includes the word in question.

My method won't find the short word inside a longer word, either.

If you want to do that (e.g. find "big" in "biggest") you would need to loop through list temp, doing something like this:

		string strLookFor = "big";
		integer max = llGetListLength(temp)-1;
		do{
			string s = llList2String(temp,max);//examine each word in turn
			if(~llSubStringIndex(s,strLookFor)){//if you find the string you're looking for in the word (or, of course, if the word is the string you're looking for
				temp = llDeleteSubList(temp, max, max);
			}
		}
		while(--max >-1);//you have to count backwards through the list in case there are two instances of the string you want find in the words, otherwise the count messes up when you delete something

 

Link to comment
Share on other sites

2 hours ago, Innula Zenovka said:

How about casting the sentence into a list, using

list temp = llParseString2List("The quick brown fox jumped over the lazy dog.",[" "],["."]);

One limitation of this is the fact that you'll have to try and account for every type of character that's probably going to be attached to the word. Period, comma, question/exclamation, brackets, etc. (And the function can only take a limited amount, so you'd have to do it in multiple batches.) It really depends on what the exact use-case is (or expected input), for the best advice.

  • Like 1
Link to comment
Share on other sites

8 hours ago, Wurdy said:

Given a string, you need to remove a certain word from it, is it possible to implement it?

I did not find the delete function in the string specification

I tried that and gave up. The issue was that you end up chasing your tail trying to cover what Wulfie stated.  Converting the string to upper or lower case is easy, the other stuff is a nightmare. If it is in a list you wrote then that is easy, as stated above but i suspect you are talking about chat messages.

Link to comment
Share on other sites

That can be tricky, depending on the details of the use case.

If you want to find single words (find "big" but not "biggest") you need to add spaces to your search string. Will not work for 1st and last word and if characters like".,-" or parenthesis are attached to the word.
So you need to replace all special characters with spaces and add a space at the begin and end of the original string and convert it to lower case too.

Then you can find the position of your word. Subtract 1 due to the added space at the beginning of the search string.

Then you can remove the word from the original string. You need to think about the spaces. If the word had a leading and trailing space you need to remove one. In case of special characters like "-" or parenthesis something might not look good now.

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1916 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...