String & Date Functions and Methods

VB.NET દરેક text job ને 2 spellings આપે છે: classic functions (Mid, InStr, 1 થી counting) અને .NET methods (Substring, IndexOf, 0 થી counting), plus dates માટે Now, DateAdd અને DateDiff.

11 min read · 10 cards · 2 checks

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


Theory

એક job, બે spellings

ShopKeeper એ item names ને capitals માં print કરવા જોઈએ છે. તમે 2 seniors ને ask કરો છો અને 2 answers મેળવો છો:

UCase(item) તે senior કહે છે જેણે classic VB શીખ્યું હતું.

item.ToUpper() તે કહે છે જે BCA403 ના Java થી આવ્યો હતો.

બંને correct VB.NET છે. language એ તેનું classic function family retain કર્યું AND .NET method family gain કર્યું, કારણ કે String નીચે System.String છે. catch: 2 families અલગ રીતે count કરે છે, અને એ difference એ છે જ્યાં exam hide કરે છે.

At a glance

Same job, બંને 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

counting mismatch, precisely

s = "ShopKeeper" (10 characters) લો.

Classic functions S ને position 1 તરીકે treat કરે છે: Mid(s, 5, 6) 5th character (K) થી start થાય છે અને "Keeper" return કરે છે. InStr(s, "Keep") 5 answer કરે છે, અને 0 નો અર્થ not found છે.

.NET methods S ને index 0 તરીકે treat કરે છે, exactly BCA403 ના Java જેવું: s.Substring(4, 6) પણ "Keeper" return કરે છે. s.IndexOf("Keep") 4 answer કરે છે, અને -1 નો અર્થ not found છે.

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

Practical

typed item ને cleaning, બંને 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". એક student Mid(s, 1, 4) લખે છે, બીજો s.Substring(1, 4) લખે છે. તેઓ શું મેળવે છે?

  1. બંને "Shop" મેળવે છે: calls equivalent છે
  2. Mid "Shop" આપે છે, Substring "hopK" આપે છે: Mid 1 થી count કરે છે, Substring 0 થી
  3. Mid "hopK" આપે છે, Substring "Shop" આપે છે: Mid 0 થી count કરે છે, Substring 1 થી
  4. Substring error throw કરે છે: VB.NET strings ફક્ત classic functions ને support કરે છે
Show the answer

Mid "Shop" આપે છે, Substring "hopK" આપે છે: Mid 1 થી count કરે છે, Substring 0 થી

Mid ની position 1 IS first character છે, એટલે ત્યાં થી 4 characters "Shop" spell કરે છે. Substring નો index 1 એ SECOND character (h) છે, એટલે તે "hopK" return કરે છે. Option C 2 conventions ને swap કરે છે, precise mistake જે આ question catch કરવા માટે exists કરે છે: classic functions 1-based છે, .NET methods 0-based. Option D આ lesson નો whole point forget કરે છે: VB.NET String એ System.String છે, એટલે દરેક .NET method available છે. જ્યારે marks depend કરે છે, string ને write કરો અને twice number કરો, 1 થી અને 0 થી.

Theory

Dates: bill ની બીજી column

દરેક ShopKeeper bill date ને carry કરે છે, અને 3 tools syllabus ને cover કરે છે:

  • Now એ current date AND time છે; Today ફક્ત date છે
  • DateAdd calendar arithmetic કરે છે: DateAdd(DateInterval.Day, 7, Today) એ 7-day return deadline છે
  • DateDiff gaps ને measure કરે છે: DateDiff(DateInterval.Day, billDate, Today) purchase થી days ને count કરે છે

display માટે, explicitly format કરો: Format(Today, "dd/MM/yyyy") અથવા Today.ToString("dd/MM/yyyy"), કારણ કે unformatted date જે style માં till નું Windows set છે તેમાં print થાય છે.

Think first

return-window check

bill 1 July 2026 ને made થયું હતું. customer item ને 11 July 2026 ને return કરે છે, અને Mehta Stores 7 days allow કરે છે. DateDiff(DateInterval.Day, billDate, returnDate) ને તમારા mind માં compute કરો અને return પર rule કરો.

Show the answer

DateDiff first date થી second date સુધી days ને count કરે છે: 1 July થી 11 July એ 10 days છે, અને 10 > 7, એટલે window closed છે: refused (politely). argument order ને note કરો, positive answer માટે smaller-first: DateDiff(interval, earlier, later). તેમને swap કરો અને તમે -10 મેળવો છો, અને naive check જેમ કે diff <= 7 પછી wrong APPROVE કરતું દરેક late return ને: one-line bug worth remembering, અને exam snippets માં favourite trick.

Watch out

3 not-found answers

ત્રણ different "nothing there" signals હવે તમારા head માં live કરે છે; તેમને apart રાખો:

  • InStr 0 answer કરે છે (positions 1 થી start થાય છે, એટલે 0 not-found mean કરવા માટે free છે)
  • IndexOf -1 answer કરે છે (indexes 0 થી start થાય છે, એટલે -1 free value છે; BCA403 agreed)
  • Val("abc") 0 answer કરે છે "no number found" નો અર્થ

absence માટે If InStr(...) = -1 અથવા If s.IndexOf(...) = 0 testing બંને wrong છે ways માં જે happily compile થાય છે.

Theory

project દીઠ એક family pick કરો

Professionals usually .NET methods પર settle કરે છે (તે દરેક other .NET language અને BCA403 habits ને match કરે છે), પણ આ paper ના exams classic family ને love કરે છે, એટલે તમારે બંને ને fluently read કરવું જોઈએ. ShopKeeper માટે practical rule: code માં methods, અને જ્યારે answer sheets mark કરતા કોઈ તમને Mid અને InStr hand કરે, 1 થી count કરો without flinching. next lesson code organised થાય છે: Sub, Function, અને arguments કેવી રીતે travel કરે છે.

Summary

Key takeaways

  • VB.NET strings 2 families ને answer કરે છે: classic functions (Len, UCase, Mid, InStr) અને .NET methods (Length, ToUpper, Substring, IndexOf).
  • Classic functions positions ને 1 થી count કરે છે; .NET methods 0 થી index કરે છે: Mid(s,1,4) = s.Substring(0,4).
  • Not-found: InStr 0 આપે છે, IndexOf -1 આપે છે: sentinels ને ક્યારેય mix ન કરો.
  • Now = date+time, Today = date; DateAdd date ને shift કરે છે, DateDiff(interval, earlier, later) gap ને measure કરે છે.
  • Format(d, "dd/MM/yyyy") display ને control કરે છે; unformatted dates Windows regional settings ને follow કરે છે.
  • Replace(s, old, new) text ને બંને families માં swap કરે છે.
  • Memory hook: classic people ને count કરે છે (1 થી), .NET floors ને count કરે છે (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