With the help of the String.Characters iterator it is possible to output all single characters correctly, even if they consist of composite characters (some emojis, Gujarati etc.) and thus String.Length > 1 is. This is already very helpful, but also already the tricky part, because how can I now find out when the iterator has reached its end?
What do I want to do? I need to read String.Length of the last letter of a String. I can add the length of all previous characters within the loop so that I have the correct value for String.Middle to read the last character. That’s not the point, I just need to know when the last pass of the loop is reached.
That’s how I do it at the moment. Any other suggestion?
Var sentence As String = "Hello World - હેલો વર્લ્ડ 👨🏻🦰"
Var index, length As UInteger
length = sentence.Length
For Each character As String In sentence.Characters
index = index + character.Length
If index = length Then
' Last character.
End if
Next
Of course, but even for that I need to know the exact length of the last character beforehand. And with dynamic user input, I can never know this beforehand. Therefore I have to iterate through all characters using String.Characters.
My only concern in this thread is whether there is also an alternative way to mine from above that works well.
You could use a traditional For… loop to iterate over the characters by index:
Var sentence As String = "Hello World - હેલો વર્લ્ડ 👨🏻🦰"
Var length As Integer = sentence.Length - 1
var currentCharacter as String
For index as Integer = 0 to length
currentCharacter = sentence.Middle(index, 1)
If index = length Then
' Last character.
End if
Next
Thank you for your reply @Anthony_G_Cyphers. Unfortunately I couldn’t use your way because your code doesn’t consider composite characters. Try this and look at the logs This is the reason why String.Characters is so important. It looks like this is really the only possible way, unless Kem comes up with RegEx now
Var sentence As String = "Hello World - હેલો વર્લ્ડ 👨🏻🦰"
Var length As Integer = sentence.Length
Var currentCharacter As String
For index As Integer = 0 To length
currentCharacter = sentence.Middle(index, 1)
System.DebugLog(currentCharacter)
If index = length Then
' Last character.
End If
Next
This is true, however, changing the Xojo framework from String.Middle/Left/Right to respect composed characters would break code. This was discussed at length in the feedback case for the introduction of String.Characters at the time, so I can live with it even if it amounts to a bit of extra work.
Public Function StringToCompoundArray(s As String) As String()
Var result() As String
For Each char As String In s.Characters
result.Add(char)
Next
Return result
End Function
// Test function
Var sentence As String = "End હેલો વર્લ્ડ 👨🏻🦰"
Var compound() As String = StringToCompoundArray(sentence)
Var msg As String
For i As Integer = 0 to compound.LastIndex
msg = msg + "["+compound(i)+"] length:"+compound(i).Length.ToString+EndOfLine
Next
// compound.LastIndex is where the last compound char is
// Now you have access to any of its parts and lengths
MessageBox msg