How to edit a string via slice
BlitzMax Forums/BlitzMax Beginners Area/How to edit a string via slice
| ||
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? |
| ||
Strings are no arrays in BM You access them using left / right / mid to do such operations |
| ||
Hum, so using [] i cant do what i want then? |
| ||
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. |
| ||
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. |
| ||
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 |