מחרוזות של תווים strings
DESCRIPTION
מחרוזות של תווים Strings. hellokita. hello. מחרוזות - Strings. Dim s As String. s = “hello”. s = s & “kita” או s = s + “kita”. פעולות על מחרוזות. מחזירה אורך המחרוזת. Len. Module Module1 Sub Main() Dim word As String word = Console.ReadLine - PowerPoint PPT PresentationTRANSCRIPT
מחרוזות של תווים
Strings
מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5
Strings - מחרוזות
Dim s As String
s = “hello”
s = s & “kita”
או
s = s + “kita”
hellohellokita
מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5
מחזירה אורך המחרוזת
Module Module1 Sub Main() Dim word As String word = Console.ReadLine Console.WriteLine(Len(word)) Console.WriteLine(word.Length()) End SubEnd Module
מחרוזותפעולות על
Len
מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5
מחזירה תת-מחרוזת משמאל
a = “hello kita”x = Left (a,2)
מחרוזותפעולות על
Left
heאיזה כמה תויםמחרוזת
מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5
מחזירה תת-מחרוזת מימין
a = “hello kita”x = Right (a,6)
מחרוזותפעולות על
Right
o kitaאיזה כמה תויםמחרוזת
מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5
מחזירה תת-מחרוזת
a = “hello kita”x = Mid (a,2,3)
מחרוזותפעולות על
Mid
ellהחל מתו-כמה תוים
מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5
תרגול קטן
Len (s)
Left (s,6)
Right (s,7)
Mid (s,6,8)
s = "arur haman baruch mordechai"
27
arur h
rdechai
haman ba
Mid (Right(s,9),2,3) ord
Right (s,9) & Mid (s,11,7) mordechai baruch
Left (s,10) = Mid (s,1,11)? לא
מחזירה אינדקס של תת מחרוזת במחרוזת
a = “hello kita”x = a.IndexOf("kit",0)
מחרוזותפעולות על
IndexOf
תת 6מחרוזת
החל מתו-
מחזירה אינדקס של תת מחרוזת במחרוזת
str1 = "I am happy"str2 = str1.Substring(5, 2)
מחרוזותפעולות על
Substring
haהחל מתו-כמה תוים
מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5
זה חוקי!
"דוד" > "שלמה"?
דוגמאModule Module1 Sub Main() Dim word As String word = Console.ReadLine If (word < "avi") Then Console.WriteLine("Bigger") Else Console.WriteLine("Smaller") End If word = word + "A" Console.WriteLine(word) word = "B" & word Console.WriteLine(word) End SubEnd Module
שימוש בסיסי במחרוזתModule Module1
Sub Main() Dim x As String x = Console.ReadLine Console.WriteLine("The Length is " & x.Length()) Console.WriteLine("The first letter is " & x(0)) Console.WriteLine("The second letter is " & x(1)) Console.WriteLine("The third letter is " & x(2)) Console.WriteLine("What will this do??? " & x(2000)) End Sub
End Module
שימוש בסיסי במחרוזתModule Module1
Sub Main() Dim x As String x = Console.ReadLine Console.WriteLine("The first letter is " & x(0)) If (x(0) = "A") Then Console.WriteLine("Yeah!") End If If (x(1) = " ") Then Console.WriteLine("Space in second position") End If End Sub
End Module
פעולות בסיסיות במחזרותModule Module1
Sub Main() Dim word As String word = Console.ReadLine 'word(0) = "B" ' Won't work! word = word.Replace("a", "b") 'word.Replace("a", "b") also won't work Console.WriteLine("The word now is " & word) word = word.Remove(0, 2) 'takes out first 2 letters Console.WriteLine("The word now is " & word) word = word.Insert(0, "B2") 'add string at position Console.WriteLine("The word now is " & word) End Sub
End Module
דוגמא של לולאה במחרוזתModule Module1
Sub Main() Dim x As String
Dim i, j As Integer x = Console.ReadLine
Console.WriteLine("The Length is " & x.Length()) For i = 0 To x.Length() - 1
For j = 0 To i Console.Write(x(j))
Next Console.WriteLine()
Next End Sub
End Module
פונקציות עם מחרוזותModule Module1
Function Len (ByVal x As String) As Integer Return x.Length()
End Function
Sub Main() Dim x As String
x = Console.ReadLine Dim y As Integer = Len (x)
Console.WriteLine("The length was " & y) End Sub
End Module
פונקציה יותר מסובכתModule Module1
Function Change(ByVal x As String) As String Dim i As Integer
For i = 0 To x.Length() - 1 If x(i) = "a" Or x(i) = "e" Or x(i) = "i" Then
x = x.Remove(i, 1) 'Takes out that letter Console.WriteLine("The word is now " & x)
x = x.Insert(i, "Z") 'Puts something else there End If
Next Return x
End Function
Sub Main() Dim word As String
word = Console.ReadLine Console.WriteLine("The Word is " & Change(word))
End SubEnd Module
REFפונקציה יותר מסובכת עם Module Module1 Sub Change(ByRef x As String) Dim i As Integer For i = 0 To x.Length() - 1 If x(i) = "a" Or x(i) = "e" Or x(i) = "i" Then x = x.Remove(i, 1) 'Takes out that letter Console.WriteLine("The word is now " & x) x = x.Insert(i, "Z") 'Puts something else there End If Next End Sub
Sub Main() Dim word As String word = Console.ReadLine Change(word) Console.WriteLine("The Word is " & word) End SubEnd Module
1פונקציות בוליאנית – דוגמא
Module Module1 Function Compare(ByVal word1 As String, ByVal word2 As String) As Boolean Dim i As Integer If (word1.Length << word2.Length) Then Return False Else For i = 0 To word1.Length() - 1 If word1(i) << word2(i) Then Return False End If Next End If Return True End Function
...המשך בשקף הבא
פונקציות בוליאנית (שימוש)
המשך משקף קודם...
Sub Main() Dim a, b As String a = Console.ReadLine b = Console.ReadLine If (a = b) Then Console.WriteLine("They are the same") End If If (Compare(a, b)) Then Console.WriteLine("They are still the same") End If End Sub
End Module
2פונקציות בוליאנית – דוגמא Module Module1 Function Flip(ByVal word1 As String) As Boolean Dim i As Integer For i = 0 To word1.Length() - 1 If word1(i) << word1(word1.Length() - 1 - i) Then Return False End If Next Return True End Function
Sub Main() Dim a As String a = Console.ReadLine If (Flip(a)) Then Console.WriteLine("It is a palindrome") Else Console.WriteLine("It isn't") End If End SubEnd Module