How to edit a string via slice

BlitzMax Forums/BlitzMax Beginners Area/How to edit a string via slice

Shagwana(Posted 2005) [#1]
What im try to do, is edit a string via slice access - is it possible?

Function fiddle:String(orgstring:String)
  Local modstring:String=orgstring

  modstring[4]=byte(100)   'How the write to a sliced string?

  Return modstring
  End Function

'main program
stringy:String="something"
Print stringy
stringy=fiddle(stringy)
Print stringy
End

It says "Compile Error - Expression must be a varible"
Well I guess im doing something wrong, so what is it?


Dreamora(Posted 2005) [#2]
Strings are no arrays in BM

You access them using left / right / mid to do such operations


Shagwana(Posted 2005) [#3]
Hum, so using [] i cant do what i want then?


Perturbatio(Posted 2005) [#4]
You can't access an index of a string directly, but I did discover something:
stringy:String="something"
Print stringy
Print stringy[0..4]
stringy = Stringy[0..4] + "test" + Stringy[4..9] 'you can concatenate slices and strings interchangably apparently
Print stringy


*EDIT*

Although the EndIndex appears to change and become a length parameter instead, I don't know if this is intended functionality or not.


Shagwana(Posted 2005) [#5]
Strict
Function fiddle:String(orgstring:String)

  Return orgstring[..3]+Chr(45)+orgstring[5..]   'A solution
 
  End Function

'main program
Local stringy:String="something"
Print stringy
stringy=fiddle(stringy)
Print stringy
End

After a little fiddling i belive I have the answer (as above) Thanks guys.

However this does seam a little wastefull as its creating a new string for the edit operation - and all i wish to do was change the value of a byte(char) within the string.


Perturbatio(Posted 2005) [#6]
use var then:

stringy:String ="something"
StrInsert(stringy, "test", 4)
Print Stringy
StrReplaceIndex(stringy, "-", 4)
Print stringy

Rem
	Inserts inString into the SourceStr at the specified index and expands the string to accommodate the change
EndRem
Function StrInsert(SourceStr:String Var, inString:String, Index:Int)
	SourceStr = SourceStr[..Index] + inString + SourceStr[Index..]
End Function



Function StrReplaceIndex(SourceStr:String Var, replaceString:String, Index:Int)
	SourceStr = SourceStr[..Index] + replaceString + SourceStr[Index+1..]
End Function