in vb6, you could use the trim$ function as in the following code.
dim s1 as string, s2 as string
s1 = " hello world "
s2 = trim$(s1)
in vb7, use the trim method of the system.string class, instead.
imports system
dim s1 as string, s2 as string
s1 = " hello world "
s2 = s1.trim()
lcase$ and ucase$
in vb6, the lcase$ returns a new string in lower case and the ucase$ function returns a new string in upper case. these two functions are complemented by the tolower() and toupper methods of the string class. as an example,
lcase$("hello world")
is the same as
"hello world".tolower()
and
ucase$("hello world")
returns the same result as
"hello world".toupper()
replace
in vb6, the replace function is used to replace part of a string with a substring. for example, the following vb6 code will result in "kingkong."
dim s as string
s = replace("dingdong", "d", "k") ' s = "kingkong"
the string class has the replace function that does a similar task. for example, the following vb7 code does the same thing.
dim s as string = "dingdong".replace("d"c, "k"c)
note that the replace method of the string class accepts two char arguments, not string arguments.
string comparison
you can still use the string manipulation functions:
if s1 = s2 then
which is the same as writing the following:
if s1.equals(s2) then
the stringbuilder class
the system.string class represents an immutable string of characters, just like string in vb6. this means the value of a string variable cannot be modified. if you try to modify a string, a new string object is created instead. remember that object creation is an expensive operation. the .net framework also provides the system.text.stringbuilder class that is more efficient to work with. for more information, see the .net framework reference.
dates
forget the date and time statements in vb6. vb7 now uses the datetime structure in the system namespace to represent a date and time value. this structure has a large number of methods and properties that are more than sufficient to work with to get any date and time value. the most important property is probably now, which retrieves the current date and time. and then there are day, date, month and year properties that you can use to display the date in any format. for example, the following code displays the current date and time in the "dd/mm/yyyy" format.
imports system
module module1
sub main()
dim now as datetime
now = datetime.now
console.writeline(now.day & "/" & _
now.month & "/" & now.year)
end sub
end module
when using datetime, you will probably use its ticks property a lot to retrieve the 100-nanosecond tick count for this instance. this property can replace the timer function in vb6 when you want to benchmark an operation, as in the following code.
imports system
dim t1 as long, t2 as long
t1 = datetime.now.ticks
' operation to be measured here
t2 = datetime.now.ticks
system.console.writeline(t2 - t1)
the datetime structure is also convenient to use for some "date-time mathematics." for example, you can add x hour or y minutes to an instance of datetime using the addhours and addminutes methods.
object
as mentioned above, the object class in vb7 represents the .net framework object class. this class is the root of every other type in the .net framework. in vb7, when you create a new class, that new class implicitly inherits from object. of particular importance are the gettype and tostring methods. the gettype method enables you to obtain the type of the object, and the tostring method returns a string that represents the current object.