All rights eserved. In this article follow up on the last article and talk about how to break long strings into shorter strings, and we look at a new object in NSBasic, the Date Object. cmb.AddItem <widget name>.caption This is a shortcut for something you probably did (if you're just starting out): The ComboBox event handler uses the same technique to display the item number of the item in the list that the user chose: cmb.ListIndex If you don't realize it, the above sample can be used as a basis for a simple calculator. We'll come back to this example from time to time as we learn more! Dissecting Strings We described how to set up strings via a DIM statement and how to assign them to variable and how to concatenate strings using & into longer strings last time. This time we talk about how to chop a string into pieces! Let's say you have a string that someone typed in. A first name, a space, and a last name, and you want to make your program friendlier and just use the person's first name when you display a message: Serg<space>Koren You could have them enter just their first name in a field, and have a separate field for the last name. But sometimes this is too much work for the user. So lets say we get the full string somehow. But how do you just get the first name? Or just the last name? Or maybe the space in the middle? It's not very hard, since NSBasic provides a rich set of Functions that let you do just that. Let's start simply and assume you know the length of the substring you want to extract, and that the substring starts at the very left of our parent string. Just use the LEFT function. LEFT takes two parameters, the string you want to chop up, and the number of characters from the left you want to take. What you get is a substring with those restrictions. So if we know the first name is 4 characters long: Again, remember that a function returns a result and uses parentheses around its parameters. That's not hard is it? Now what about the last name? How do we get the rightmost part of a string if we know the length of the substring? Just use the RIGHT function the same way! Assuming the above snippet of code exists: You could even put it back together! Important note: RIGHT, LEFT, and the other string manipulation functions we will discuss don't destroy the original string! They just make copies of parts of them! would still show "Serg Koren", it hasn't been destroyed by LEFT and RIGHT! Now the last function MID which returns a substring from the MIDdle. MID again takes the string to chop up, but its next parameter is the starting character (a number) and then an optional length parameter to extract. For example, if we wanted to extract the space from the name we could do: Not very exciting, but it works. How do we know? Well we can figure out how many characters "Space" has. We can get the lenght of a string (the number of characters in it) by using the LENgth function: Try the above samples of code on your PocketPC, changing the lengths or the starting position. I mentioned the third parameter in MID is optional. What happens if you leave it out in the above code? You should also read the manual pages on LEFT, RIGHT, MID, and LEN. You'll be using these a lot! Ignore LEFTB, RIGHTB, LENB, MIDB for the time being. We'll go over these in a future article. The last string function I want to talk about is REPLACE. REPLACE replaces parts of a string with another string. It's like an editor's FIND/REPLACE function. For example, if we want to change all of the "e"s in my name to "x"s we could use REPLACE. If you check the manual, you see it has a bunch of parameters, but you'll only have to worry about the first three most of the time. 'Target' is our main string we want to modify (FirstLast in my example), 'find' is the string we want to replace (e in my example) and 'source' is the character we want to replace find with (x). That's it for strings; or at least the things you'll deal with most. Date Object Date Objects are a new feature introduced in the latest NSBasic release. It displays a calendar widget and lets the user select a date from the calendar. Normally, it displays a date in a field. When the user taps on the date, it displays the calendar. It's useful for getting a date from the user without him having to type a date in or having to pull up another type of calendar. Creating it is easy (you should have this down in your sleep). Max and Min allow you to specify a range to limit your calendar to a date range. This is useful if say you want to limit the calendar selection to the current fiscal quarter, or this: There are no new Methods to discuss for the Date Object, and the only new Event we can handle is something called "dropDown" which gets triggered (calls our "MyDateObject_dropDown" handler) when the user taps the the field to display the calendar. For Next Time This is a simple exercize to give you a break from the last one. Write an app that has:
ARTICLE 8--DISSECTING STRINGS AND THE DATE OBJECT
'----------code starts here'' Article 7 assignment''----------Routines to create the UI' set up our 5 checkboxesSUB CreateCheckbox1ADDOBJECT "CheckBox","Ch1",10,10,30,20Ch1.Caption = "1"END SUBSUB CreateCheckbox1ADDOBJECT "CheckBox","Ch2",10,30,30,20Ch2.Caption = "2"END SUBSUB CreateCheckbox2ADDOBJECT "CheckBox","Ch3",10,50,30,20Ch3.Caption = "3"END SUBSUB CreateCheckbox3ADDOBJECT "CheckBox","Ch4",10,70,30,20Ch4.Caption = "4"END SUBSUB CreateCheckbox4ADDOBJECT "CheckBox","Ch5",10,90,30,20Ch5.Caption = "5"END SUB'----------setup our combo boxSUB CreateComboBoxADDOBJECT "ComboBox","Cmb",10,110,60,80END SUB'----------set up our command buttonsSUB CreatePlusButton ADDOBJECT "CommandButton","Plus",10,140,20,20Plus.Caption = "+"END SUBSUB CreateMinusButton ADDOBJECT "CommandButton","Minus",10,170,20,20Minus.Caption = "-"END SUBSUB CreateClearButton ADDOBJECT "CommandButton","Clear",10,200,50,20Clear.Caption = "Clear"END SUBSUB CreateTotalButton ADDOBJECT "CommandButton","Total",10,230,50,20Total.Caption = "Total"END SUB
'----------event handler for checkboxes...' just put the number (which is the caption) into comboboxSUB Ch1_ClickCmb.AddItem Ch1.Caption ' just move the caption of this checkbox into the last slot in the ComboboxEND SUB SUB Ch2_ClickCmb.AddItem Ch2.CaptionEND SUB SUB Ch3_ClickCmb.AddItem Ch3.CaptionEND SUB SUB Ch4_ClickCmb.AddItem Ch4.CaptionEND SUB SUB Ch5_ClickCmb.AddItem Ch5.CaptionEND SUB '----------event handlers for buttonsSUB Plus_ClickCmb,AddItem Plus.Caption ' just move the caption of this button into the last slot of the ComboboxEND SUBSUB Minus_ClickCmb,AddItem Minus.Caption END SUBSUB Clear_ClickCmb.Clear ' just clear all the items in the ComboBox outEND SUBSUB Total_ClickMSGBOX "There are " & Cmb.ListCount & " items in the Combobox." ' see explanation belowEND SUBSUB Cmb_Change ' our ComboBox event handler...see explanation belowMSGBOX "You chose " & cmb.ListIndex & "." ' note the period at the endEND SUB'----------main routines' set up our user interface widgetsSUB SetupUICreateCheckbox1CreateCheckbox2CreateCheckbox3CreateCheckbox4CreateCheckbox5CreateComboBoxCreatePlustButtonCreateMinusButtonCreateClearButtonCreateTotalButtonEND SUB
' our main routine that runs everythingSUB MainSetupUI ' create the user interfaceEND SUBMain ' our only IMMEDIATE command'----------code ends here
DIM X
X = <widget name>.caption
cmb.AddItem X
MSGBOX "There are " & Cmb.ListCount & " items in the Combobox."
DIM FirstLast
FirstLast = "Serg Koren"
We have a string! We know how to add stuff to it such as
Prompt = FirstLast & " how are you?"
DIM First, FirstLast,FirstLength
FirstLength = 4
FirstLast = "Serg Koren"
First = LEFT(FirstLast,FirstLength)
MSGBOX First & ", how are you?"
DIM Last,LastLength
LastLength = 5
Last = RIGHT(FirstLast,LastLength)
MSGBOX "The last name is: " & Last
DIM NewFirstLast
NewFirstLast = First & " " & Last
MSGBOX NewFirstLast
MSGBOX FirstLast
DIM Space, StartPos, Length
StartPos = 5 ' start at the 5th character of FirstLast (the space)
Length = 1 ' extract only 1 character (just the space)
Space = MID(FirstLast,StartPos,Length) ' get the space
MSGBOX Space ' display the space
DIM SpaceLeng
SpaceLen = LEN(Space)
MSGBOX "Space has " & SpaceLen & " characters in it."
MSGBOX "FirstLast has " & LEN(FirstLast) & " characters in it."
REPLACE(target,find,source)
FirstLast = REPLACE(FirstLast,"e","x")
MSGBOX FirstLast
You can also do things like
FirstLast = REPLACE(FirstLast,"er","eeeeeeeer")
ADDOBJECT "Date","MyDateObject",10,10,100,20
MyDateObject.Min = "07/01/1999"
MyDateObject.Max = "12/01/1999"
It should:
- save the date the user taps on in a variable called TheDate
- Display the message: "This is the i-th day of the j-th month." whenever the user taps the command button.
Note that i and j above are numbers you can get from TheDate.
Next time I'll tackle a powerful and simplifying concept known as arrays as well as the INPUTBOX object! See you then...


