More String Questions

Blitz3D Forums/Blitz3D Beginners Area/More String Questions

zortzblatz(Posted 2009) [#1]
Is there anything in blitz which allows you to count how many of a specific character are within a string? For example, if is there anything I could use in Blitz to tell how many Zs are in the string "ZortZblatZ"?

Additionally, is there any way to replace a specific character within a string? like, if I wanted to replace the third characted in "ZortZblatZ" with a 'q' or something?

Thanks for your time.


Graythe(Posted 2009) [#2]
Function CountChar(StringData$,Char$)

Local NoChars
Local StartPos
Local CharTest

NoChars=Len(StringData)
StartPos=1

Repeat
CharTest=Instr(StringData,Char,StartPos)
If CharTest
CharCount=CharCount+1
StartPos=CharTest+1
If StartPos>NoChars
Exit
End If
else
exit
End If
Forever
Return CharCount

End Function


Graythe(Posted 2009) [#3]
I don't think there is a way to replace 1 char other than splitting the original string into 2 pieces, inserting the new char and then concatenating those 3 things. Are you happy with that?


zortzblatz(Posted 2009) [#4]
I am totally happy with that. How do you concatenate strings in B3D? Do you use + or . or something else?

Also that function you gave my results in an infinite loop.

Thanks for your input though!


Graythe(Posted 2009) [#5]
Sorry zor - the function didn't have a null result exit clause, I popped that in just now.

Function RepChar$(StringData$,Char$,CharPos)

If CharPos>1
Part1$=Left(StringData,CharPos-1)
End If
If CharPos<Len(StringData)
Part2$=Mid(StringData,CharPos+1,Len(StringData)-CharPos)
End If

Return Part1+Char+Part2

End Function


tigerman(Posted 2009) [#6]
Although there are no specific functions like CountOccurances, there are ways of doing this with the string functions that Blitz does provide.

For example, you can iterate through the string using InStr("ZortZblatZ", "Z", x) which will return the position of the first "Z" in the string, then check again using InStr("ZortZblatZ", "Z", <value just returned from previous call+1>) and keep going until InStr returns 0 (which means it didn't find it). Then, if you were keeping count, you would know how many you had.

Another method would be to use Len() to count the number of characters in the string, then use Replace to replace the letter you were looking for with nothing ("") then do Len() again and subtract it from the previous result.

As for your second question, use Replace(), although that would replace all occurances of the letter with another letter, not just the third character.

Or you could just use the code provided above which was written while I was dawdling about writing this :)


AJ00200(Posted 2009) [#7]
You use:
string="HELLO" + " WORLD"
Print string
string2=string + " & UNIVERSE"
Print string2



Graythe(Posted 2009) [#8]
Write a function to do it - that way you've done it for all time. This is untested

Function RepChar$(StringData$,Char$,CharPos)

If CharPos>1
	Part1$=Left(StringData,CharPos-1)
End If
If CharPos<Len(StringData)
	Part2$=Mid(StringData,CharPos+1,Len(StringData)-CharPos)
End If

Return Part1+Char+Part2

End Function 



zortzblatz(Posted 2009) [#9]
Thank all of you for your input. When I get home in about an hour, I'll try some stuff and let you know if I've managed what I'm after.


GIB3D(Posted 2009) [#10]
Is there anything in blitz which allows you to count how many of a specific character are within a string?


I just thought of something that seems faster than looping through the string.
Print "Strict on = " + CountChars("ZortZblatZz","Z")
Print "Strict off = " + CountChars("ZortZblatZz","Z",True)
WaitKey

Function CountChars(string_data$,find$,strict=False)
	Local Length1 = Len(string_data),Length2
	
	If strict = False
		string_data = Replace(Lower(string_data),Lower(find),"") ; When not using strict, it doesn't matter if the letter is uppercase or lowercase.
	Else
		string_data = Replace(string_data,find,"")
	EndIf
	
	Length2 = Len(string_data)
	
	Return (Length1-Length2)
End Function


The Replace command effects all instances of what you put in the "Find" parameter and changes it to the "To" parameter. What I did is simply keep the length of before and after I used the Replace command to see how many characters were taken out. Easy


zortzblatz(Posted 2009) [#11]
Thank all of you for all of your help. All of my problems have been resolved. Whenever I finish this project I have every intention of making it open-source, free and crediting all of you for all of your wonderful support.