String & Date Functions and Methods

VB.NET gives every text job 2 spellings: classic functions (Mid, InStr, counting from 1) and .NET methods (Substring, IndexOf, counting from 0), plus Now, DateAdd and DateDiff for dates.

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

One job, two spellings

ShopKeeper must print item names in capitals. You ask 2 seniors and get 2 answers:

UCase(item) says the senior who learned classic VB.

item.ToUpper() says the one who came from BCA403's Java.

Both are correct VB.NET. The language kept its classic function family AND gained the .NET method family, because String is System.String underneath. The catch: the 2 families count differently, and that difference is where the exam hides.

At a glance

Same job, both spellings

JobClassic function (1-based).NET method (0-based)
LengthLen(s)s.Length
Capitals / smallUCase(s), LCase(s)s.ToUpper(), s.ToLower()
SliceMid(s, 5, 6)s.Substring(4, 6)
Find positionInStr(s, "Keep"): 0 = not founds.IndexOf("Keep"): -1 = not found
EndsLeft(s, 4), Right(s, 4)s.Substring with lengths
Clean spacesTrim(s)s.Trim()

Theory

The counting mismatch, precisely

Take s = "ShopKeeper" (10 characters).

Classic functions treat the S as position 1: Mid(s, 5, 6) starts at the 5th character (K) and returns "Keeper". InStr(s, "Keep") answers 5, and 0 means not found.

.NET methods treat the S as index 0, exactly like BCA403's Java: s.Substring(4, 6) also returns "Keeper". s.IndexOf("Keep") answers 4, and -1 means not found.

Same slice, 2 coordinate systems, off by exactly 1 forever.

Practical

Cleaning a typed item, both styles

Module TextDemo
    Sub Main()
        Dim raw As String = "  masala tea  "

        ' Classic family
        Dim t As String = Trim(raw)          ' "masala tea"
        MsgBox(UCase(Left(t, 6)))            ' MASALA
        MsgBox(InStr(t, "tea"))              ' 8  (1-based)

        ' .NET method family, same results
        Dim m As String = raw.Trim()
        MsgBox(m.Substring(0, 6).ToUpper())  ' MASALA
        MsgBox(m.IndexOf("tea"))             ' 7  (0-based)

        MsgBox(Replace(t, "tea", "chai"))    ' masala chai
    End Sub
End Module

Quiz

s = "ShopKeeper". One student writes Mid(s, 1, 4), another writes s.Substring(1, 4). What do they get?

  1. Both get "Shop": the calls are equivalent
  2. Mid gives "Shop", Substring gives "hopK": Mid counts from 1, Substring from 0
  3. Mid gives "hopK", Substring gives "Shop": Mid counts from 0, Substring from 1
  4. Substring throws an error: VB.NET strings only support classic functions
Show the answer

Mid gives "Shop", Substring gives "hopK": Mid counts from 1, Substring from 0

Mid's position 1 IS the first character, so 4 characters from there spell "Shop". Substring's index 1 is the SECOND character (h), so it returns "hopK". Option C swaps the 2 conventions, the precise mistake this question exists to catch: classic functions are 1-based, .NET methods 0-based. Option D forgets the whole point of this lesson: a VB.NET String is System.String, so every .NET method is available. When marks depend on it, write the string out and number it twice, from 1 and from 0.

Theory

Dates: the bill's other column

Every ShopKeeper bill carries a date, and 3 tools cover the syllabus:

  • Now is the current date AND time; Today is just the date
  • DateAdd does calendar arithmetic: DateAdd(DateInterval.Day, 7, Today) is the 7-day return deadline
  • DateDiff measures gaps: DateDiff(DateInterval.Day, billDate, Today) counts days since purchase

For display, format explicitly: Format(Today, "dd/MM/yyyy") or Today.ToString("dd/MM/yyyy"), because an unformatted date prints in whatever style the till's Windows is set to.

Think first

The return-window check

A bill was made on 1 July 2026. The customer returns the item on 11 July 2026, and Mehta Stores allows 7 days. Compute DateDiff(DateInterval.Day, billDate, returnDate) in your head and rule on the return.

Show the answer

DateDiff counts the days from the first date to the second: 1 July to 11 July is 10 days, and 10 > 7, so the window is closed: refused (politely). Note the argument order, smaller-first for a positive answer: DateDiff(interval, earlier, later). Swap them and you get -10, and a naive check like diff <= 7 would then wrongly APPROVE every late return: a one-line bug worth remembering, and a favourite trick in exam snippets.

Watch out

The 3 not-found answers

Three different "nothing there" signals now live in your head; keep them apart:

  • InStr answers 0 (positions start at 1, so 0 is free to mean not-found)
  • IndexOf answers -1 (indexes start at 0, so -1 is the free value; BCA403 agreed)
  • Val("abc") answers 0 meaning "no number found"

Testing If InStr(...) = -1 or If s.IndexOf(...) = 0 for absence are both wrong in ways that compile happily.

Theory

Pick one family per project

Professionals usually settle on the .NET methods (they match every other .NET language and BCA403 habits), but exams in this paper love the classic family, so you must read BOTH fluently. Practical rule for ShopKeeper: methods in code, and when marking answer sheets someone hands you Mid and InStr, count from 1 without flinching. Next lesson the code gets organised: Sub, Function, and how arguments travel.

Summary

Key takeaways

  • VB.NET strings answer to 2 families: classic functions (Len, UCase, Mid, InStr) and .NET methods (Length, ToUpper, Substring, IndexOf).
  • Classic functions count positions from 1; .NET methods index from 0: Mid(s,1,4) = s.Substring(0,4).
  • Not-found: InStr gives 0, IndexOf gives -1: never mix the sentinels.
  • Now = date+time, Today = date; DateAdd shifts a date, DateDiff(interval, earlier, later) measures the gap.
  • Format(d, "dd/MM/yyyy") controls display; unformatted dates follow Windows regional settings.
  • Replace(s, old, new) swaps text in both families.
  • Memory hook: classic counts people (from 1), .NET counts floors (from 0).

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Programming in Visual Basic .NET

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

String & Date Functions and Methods · .NET Programming · Gri-Learn