How to remove multiple occurences of text?

Blitz3D Forums/Blitz3D Programming/How to remove multiple occurences of text?

Guy Fawkes(Posted 2013) [#1]
Hi all. I'm still working on my chat code, and now the problem is, I don't know how to remove slashes only if more than 1 slash occurs in a row.

Basically, I want to retrieve slash commands while keeping all other "/" attempts as text.



Thank you all so kindly!


skidracer(Posted 2013) [#2]
so a command is any line that begins with a slash and any command that begins with a slash is text?

what is a line that doesn't start with a slash

are you removing the slash from the text using mid$ command

the Blitz3D manual implies mid$ uses indexes starting at 1 which makes me unsure how they work, hmmm...


Guy Fawkes(Posted 2013) [#3]
Yea, I simply want to allow the user to use slash commands. The only problem is, what if they try to do more than 1 or put it before or after the text? I want THEN to turn the "/"'s into text so it doesn't somehow activate another command such as "/" is NOT a command... Etc...


Midimaster(Posted 2013) [#4]
you can test if the first character is a slash and if there is no following slash in this line:

"/com" would be a command
"/com /xxx" would be no command

t$="/com"
If Left(t)="/" and Instr(t,"/",2)=0
     ; is a command
Else
     ; is no command
Endif


Or do you want that "/com /xxx" should be a command followed by a text? Then use this:

t$="/com /xxx"
If Left(t)="/" 
     space%=Instr(t," ",2)-1
     If space<0 Then space =Len(t)

     part$=Left(t,space)
     If Instr(part,"/",2)=0
          rest$=Mid(t,space+2,-1)
          ; part is a command
          ; rest is a parameter
     Else
          ; part is no command
Else
          ; t is no command
Endif



Kryzon(Posted 2013) [#5]
This needs to be a progressive work on the string of text.
You begin with the full string that the user has typed, which may or may not have slash-commands in between the words.

When you find a slash, you read the command name (you can establish a fixed length for the name of all commands, like 3 letters long), and if that text matches any of the commands available, then use it as a command. Else, use it as plain text.

I haven't tested the following, but this is how I would start:
Function ProcessString( txt$ )

	Local normalWords$
	Local command$
	Local parameters$

	Local tempString$	=	txt
	Local tempIndex		=	1
	
	While tempIndex <> 0
	
		tempIndex = Instr( tempString, "/" ) ;Find the next slash in the string.
		
		Select tempIndex
		
			Case 0
				;There are no slash characters in the string at all. Render it entirely.
				Text( ..., tempString )
			
			Case 1
				;The string starts with a slash character. Verify it if defines a valid command.

				command   = Mid( tempString, 1 + 1, 3 ) ;Grab all the three characters after (+1) the slash character (1).

				;Verify if it's a valid command. 
				;To make things easier, you can establish that all commands are written with 3 letters.
				Select command
					
					Case "col"
						;CHANGE COLOR command.
						;To read the parameters of the command (as in, "/colFF009A"), you can grab the specific length of text for parameters of color. That is, "RRGGBB" = 6 characters.
						
						parameters = Mid( tempString, 1 + 3 + 1, 6 ) ;Start from the first character after (+1) the slash character (1) AND the 3 command letters (+3), and grab the next 6 characters as parameters. We know it's six because this is the "col" command.
						
						;------------------------
						ChangeColor( parameters ) ;Use the command.
						;------------------------
			
						;Update the string being processed to remove this command that was processed.

						tempString = Mid( tempString, 1 + 3 + 6 + 1 ) ;Start from the first character after (+1) the slash (1), the 3-letter command (+3) and the 6-letter parameters (+6).

					Case "bld"
						;BOLD TEXT command.
						;No parameters to read.
						;This turns bold text mode on or off.
						
						;------------------------
						If IsBold Then
							IsBold = False
							SetFont( ... )
						Else
							IsBold = True
							SetFont( ... )
						EndIf
						;------------------------

						tempString = Mid( tempString, 1 + 3 + 1 )

					Case "udl"
						;UNDERLINED TEXT command.
						;...


					Default 
						;Important. 
						;This Default block happens when the command isn't valid, so it could be another slash, 3 random characters etc.
						
						;First, verify if there's a potential command among the three characters right after (+1) this invalid slash (1).
						tempIndex = Instr( Mid( tempString, 1 + 1, 3 ), "/" )
						If tempIndex Then 
						
							;There is another slash among the three characters right after this current slash.
							normalWords = Left( tempString, tempIndex )
							Text( ..., normalWords )
							
							;Clean the string so next time this slash is verified as well.
							tempString = Mid( tempString, tempIndex )
					
						Else
						
							;There are no slashes among the three characters right after the current slash.					
							;Make sure to not read past the end of the string.
							If Len( tempString ) > 3 Then
								normalWords = Left( tempString, 1 + 3 ) ;Grab the slash character (1) and the 3 letters that don't form any valid command (+3).
							Else 
								normalWords = Left( tempString, Len( tempString ) )
							EndIf 

							Text( ..., normalWords ) ;Print this as text.
							
							tempString = Mid( tempString, 1 + 3 + 1 ) ;Remove these four characters from the string.			
			
						EndIf												

				End Select
	
			Default
				;The index of the next slash is bigger than one, so there are normal words before the next potential command.
				normalWords = Left( tempString, tempIndex - 1 )
				tempString  = Right( tempString, tempIndex )

				;Print the text in normalWords.
				Text( ..., normalWords )
		
		End Select  				
	
	Wend

End Function
An example string to be used with this: "This is some /colFFFF00yellow text, followed by /colFFFFFF/bldsome bold words./bld"


Guy Fawkes(Posted 2013) [#6]
Thanks, guys.

So like THIS, Midimaster?



Thanks again!


Midimaster(Posted 2013) [#7]
It depends on your need, whether this code is already enough for you. This code has the rule:

"test the first word in the senctence. If it contains only one slash, then it is a command."

The variable part$ contains the command. so I would add:

Global command$
....
Function Strip_Multiple_Slashes(txt$)
	command=""
	If Left(txt$, 1)="/"
		space%=Instr(txt$," ",2)-1
	  	If space<0 Then space =Len(txt$)
		part$=Left(txt$,space)
		If Instr(part,"/",2)=0
			txt$=Mid(t,space+2,-1)
			command=part
		EndIf
	EndIf
	Return txt$
End Function



Guy Fawkes(Posted 2013) [#8]
What I need is to be able to keep only 1 "/" before the text, and make all other "/"'s regular text, whether or not it is like this:

"//e"

or:

"/e/"

or:

"./e"

or:

"e/."

or:

"e./"

etc...

I just simply want to make all other slashes that are not commands, text.

Thanks again, Midimaster! :)