Sub Process_Globals
Private smileyBmp As Bitmap
End Sub
Sub Globals
Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
smileyBmp = LoadBitmap(File.DirAssets, "smiley.png")
End If
Activity.LoadLayout("1")
Label1.Text = ReplaceSmilies("hi :) this is text message with smiley sample ;) on how to use csbuilder to replace smiley chars with images :-) and so on")
End Sub
Sub ReplaceSmilies(s As String) As CSBuilder
Dim rb As RegexBuilder
rb.Initialize
For Each smiley As String In Array(":)", ";)", ":-)")
If rb.Pattern <> "" Then rb.AppendOr
rb.StartNonCapture.AppendEscaped(smiley).EndNonCapture
Next
Return MarkPattern(s, rb.Pattern, 0)
End Sub
Sub MarkPattern(Input As String, Pattern As String, GroupNumber As Int) As CSBuilder
Dim cs As CSBuilder
cs.Initialize
Dim lastMatchEnd As Int = 0
Dim m As Matcher = Regex.Matcher(Pattern, Input)
Do While m.Find
Dim currentStart As Int = m.GetStart(GroupNumber)
cs.Append(Input.SubString2(lastMatchEnd, currentStart))
lastMatchEnd = m.GetEnd(GroupNumber)
'apply styling here
Log(m.Group(GroupNumber))
cs.Image(smileyBmp, 30dip, 30dip, True)
Loop
If lastMatchEnd < Input.Length Then cs.Append(Input.SubString(lastMatchEnd))
Return cs
End Sub