Accessing a string sequentially
BlitzMax Forums/BlitzMax Beginners Area/Accessing a string sequentially
| ||
Hi all, Probably a daft question, but... I can access a string like so... Strict Local a:String = "abcde" For Local b:Int = 0 Until a.length Print a[b] + ":" + Chr(a[b]) NextHow do I write to a string? For Local b:Int = 0 Until a.length a[b] = (65 + b) 'Doesn't work Next |
| ||
This seems to work:Strict Local a:String = "abcde" Local c:String For Local b:Int = 0 Until a.length c = c + Chr(65 + b) 'Doesn't work Print c Next |
| ||
Thanks, but not really what I'm after. I want to manipulate the original string, if at all possible. I could use... Strict Local a:String = "abcde" Local c:String For Local b:Int = 0 Until a.length c :+ Chr(a[b] - 32) Next a = c Print aJust seems needlessly messy. |
| ||
You probably have to cast the new character as a string. The a[] array is a string pointer, pointing to a string. It probably only accepts strings as content. Try For Local b:Int = 0 Until a.length a[b] = String(Chr(65 + b)) 'Doesn't work Next I dont know if that works or not |
| ||
Strict Local a:String[] = ["a","b","c","d","e"] Local letter:String = "" For letter = EachIn a Print letter Next |
| ||
you can't treat a string quite like you can an array.stringy:String="something" Print Stringy StrInsert(stringy, "test", 4) Print Stringy StrReplaceIndex(stringy, "-", 4) StrReplaceIndex(stringy, "-", 5) StrReplaceIndex(stringy, "-", 6) StrReplaceIndex(stringy, "-", 7) Print stringy StrFillRange(stringy, "*", 4, 7) Print Stringy StrFillRange(stringy, ".", 4, 7) 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 Function StrFillRange(SourceStr:String Var, ReplaceString:String, StartIndex:Int, EndIndex:Int) For Local i = StartIndex To EndIndex StrReplaceIndex(SourceStr, ReplaceString, i) Next End Function |
| ||
I guess I can't do it then. Thanks for the alternatives everyone. |