מבוא למדעי המחשב לתעשייה וניהול הרצאה 9
שרשור (חיבור) של מחרוזות (& או +) Module Module1 Sub Main() Dim s As String s = "Hello to you" s = s & " and you too" Console.WriteLine(s) s = s + " and you three ;)" End Sub End Module
פונקציות על מחרוזות Length Remove Insert Replace IndexOf LastIndexOf SubString Chars
אורך של מחרוזות Length Length מחזירה אורך של מחרוזת Module Module1 Sub Main() Dim x As Integer Dim str As String str = Console.ReadLine() x = str.Length() Console.WriteLine("The length of the string is: " & x) Console.WriteLine("and also " & str.Length()) End Sub End Module
הסרה ממחרוזות Remove Remove(start, count ) מסירה count תווים החל מהמקום start ומחזירה את המחרוזת החדשה Module Module1 Sub Main() Dim str As String = "This is my string" Dim str2 As String str2 = str.Remove(0, 8) Console.WriteLine(str2) End Sub End Module
הוספה למחרוזות Insert Insert(start, str) מוסיפה את המחרוזת str החל מהמקום start ומחזירה את המחרוזת החדשה Module Module1 Sub Main() Dim str As String = "This is my string" Dim str2 As String str2 = str.Insert(11, "nice ") Console.WriteLine(str2) Dim str3 As String = "very " str2 = str2.Insert(11, str3) End Sub End Module
החלפה במחרוזות Replace Replace(str1, str2) מחליפה את המחרוזת str1 במחרוזת str2 Replace(ch1, ch2) מחליפה את התו ch1 בתו ch2 בכל מקרה מחזירה את המחרוזת החדשה Module Module1 Sub Main() Dim str As String = "This is my string" Dim str2 As String str2 = str.Replace("my", "your") Console.WriteLine(str2) str2 = str.Replace("s", "X") End Sub End Module
החלפה במחרוזות IndexOf IndexOf(str) מחזירה מיקום של המחרוזת (או התו) str במחרוזת מיקום מתחיל מ 0, אם לא נמצא מחזיר -1 IndexOf(str, start) מחזירה מיקום של המחרוזת (או התו) str במחרוזת מיקום מתחיל מ start, אם לא נמצא מחזיר -1 Module Module1 Sub Main() Dim s As String s = "This is a nice string" Dim place As Integer = 0 place = s.IndexOf("i") While place <> -1 Console.WriteLine("i is at place " & place) place = s.IndexOf("i", place + 1) End While End Sub End Module
החלפה במחרוזות LastIndexOf LastIndexOf(str) מחזירה מיקום מסוף המחרוזת (או התו) str מיקום מתחיל מסוף המחרוזת, אם לא נמצא מחזיר -1 LastIndexOf(str, end) מחזירה מיקום מסוף המחרוזת (או תו) str מיקום מתחיל מ end, אם לא נמצא מחזיר -1 Module Module1 Sub Main() Dim s As String s = "This is a nice string" Dim place As Integer = 0 place = s.LastIndexOf("i") While place <> -1 Console.WriteLine("i is at place " & place) place = s.LastIndexOf("i", place - 1) End While End Sub End Module
חלק ממחרוזת SubString SubString(start, count ) מייצרת מחרוזת של count תווים החל מהמקום start ומחזירה את המחרוזת החדשה Module Module1 Sub Main() Dim str As String Console.WriteLine("Please enter a string") str = Console.ReadLine() Dim p1 As Integer = str.IndexOf("a") str = str.Substring(p1 + 1, 5) Console.WriteLine("the five letters after a are:" & str) End Sub End Module
שליפת תו ממחרוזת Chars Module Module1 Sub Main() Dim myString As String = "ABCDE" Dim myChar As Char myChar = myString.Chars(3) 'value of myChar is D. myChar = myString(2) 'גם זה עובד , value is C 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 Sub End Module
השוואת מחרוזות למחרוזות יש סדר הסדר הוא כמו סדר במילון apple < banana Abc < Aef אותיות גדולות (כולן) לפני האותיות הקטנות לכן Banana < apple ולכן ניתן להשוות בין מחרוזות...
השוואת מחרוזות Module Module1 Sub Main() Dim firstStr As String = Console.ReadLine() Dim secondStr As String = Console.ReadLine() If firstStr < secondStr Then Console.WriteLine("{0} is before {1} ", firstStr, secondStr) ElseIf firstStr > secondStr Then Console.WriteLine("{1} is before {0} ", firstStr, secondStr) Else Console.WriteLine("{0} is same as {1} ", firstStr, secondStr) End If End Sub End Module
דוגמא Module Module1 Sub Main() Dim name As String = "Amit Yoav Cohen" Console.WriteLine("The entire name is '{0}'", name) Dim p1 As Integer = name.IndexOf(" ") Dim p2 As Integer = name.IndexOf(" ", p1 + 1) If p1 <> p2 And p1 >= 0 Then ' remove the middle name name = name.Remove(p1 + 1, p2 - p1) Console.WriteLine("After removing middle: " & name) End If End Sub 'Main 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() End Sub End Module
דוגמא לשימוש בפונקציה עם מחרוזת Module Module1 Function mult(ByVal x As String, ByVal n As Integer) Dim str As String = "" For i = 1 To n str += x + " " Next Return str End Function Sub Main() Dim x As String x = Console.ReadLine Dim y As String = mult(x, 3) Console.WriteLine("The multiplied string is " & 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) = " " Or x(i) = "," Then x = x.Remove(i, 1) 'Takes out sign x = x.Insert(i, ";") 'Puts something else there End If Next Return x End Function Sub Main() Dim str As String str = Console.ReadLine Console.WriteLine("after change: " & Change(str)) End Sub End Module
שימוש ב ByRef Module Module1 Sub Change(ByRef x As String) Dim i As Integer For i = 0 To x.Length() - 1 If x(i) = " " Or x(i) = "," Then x = x.Remove(i, 1) 'Takes out sign x = x.Insert(i, ";") 'Puts something else there End If Next End Sub Sub Main() Dim str As String str = Console.ReadLine Change(str) Console.WriteLine("after change: " & str) End Module
פונקציה בוליאנית ומחרוזת – חלק 1 – מהעושה הפונקציה? Module Module1 Function MyC (ByVal str1 As String, ByVal str2 As String) As Boolean Dim i As Integer If (str1.Length <> str2.Length) Then Return False Else For i = 0 To str1.Length() - 1 If str1(i) <> str2(i) Then End If Next Return True End Function המשך בשקף הבא...
פונקציה בוליאנית ומחרוזת – חלק 2 המשך... 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 (MyC (a, b)) Then Console.WriteLine("They are still the same") End Sub End Module
עוד דוגמא – חלק 1 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 המשך בשקף הבא...
עוד דוגמא – חלק 2 המשך... Sub Main() Dim a, b As String a = Console.ReadLine If (Flip(a)) Then Console.WriteLine("It is a palindrome") Else Console.WriteLine("It isn't") End If End Sub End Module