1 / 23

מבוא למדעי המחשב לתעשייה וניהול

הרצאה 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 ;)" Console .WriteLine (s) End Sub End Module.

lotte
Télécharger la présentation

מבוא למדעי המחשב לתעשייה וניהול

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. הרצאה 9 מבוא למדעי המחשב לתעשייה וניהול

  2. שרשור (חיבור) של מחרוזות (& או +) ModuleModule1 Sub Main() Dim s AsString s = "Hello to you" s = s & " and you too" Console.WriteLine(s) s = s + " and you three ;)" Console.WriteLine(s) EndSub EndModule

  3. פונקציות על מחרוזות • Length • Remove • Insert • Replace • IndexOf • LastIndexOf • SubString • Chars

  4. אורך של מחרוזות Length • Length מחזירה אורך של מחרוזת ModuleModule1 Sub Main() Dim x AsInteger DimstrAsString str = Console.ReadLine() x = str.Length() Console.WriteLine("The length of the string is: " & x) Console.WriteLine("and also " & str.Length()) EndSub EndModule

  5. הסרה ממחרוזות Remove • Remove(start, count ) מסירה count תווים החל מהמקום start • ומחזירה את המחרוזת החדשה ModuleModule1 Sub Main() DimstrAsString = "This is my string" Dim str2 AsString str2 = str.Remove(0, 8) Console.WriteLine(str2) EndSub EndModule

  6. הוספה למחרוזות Insert • Insert(start, str) מוסיפה את המחרוזת str החל מהמקום start • ומחזירה את המחרוזת החדשה ModuleModule1 Sub Main() DimstrAsString = "This is my string" Dim str2 AsString str2 = str.Insert(11, "nice ") Console.WriteLine(str2) Dim str3 AsString = "very " str2 = str2.Insert(11, str3) Console.WriteLine(str2) EndSub EndModule

  7. החלפה במחרוזות Replace • Replace(str1, str2) מחליפה את המחרוזת str1 במחרוזת str2 • Replace(ch1, ch2) מחליפה את התו ch1 בתו ch2 • בכל מקרה מחזירה את המחרוזת החדשה ModuleModule1 Sub Main() DimstrAsString = "This is my string" Dim str2 AsString str2 = str.Replace("my", "your") Console.WriteLine(str2) str2 = str.Replace("s", "X") Console.WriteLine(str2) EndSub EndModule

  8. החלפה במחרוזות IndexOf • IndexOf(str) מחזירה מיקום של המחרוזת (או התו) str במחרוזת • מיקום מתחיל מ 0, אם לא נמצא מחזיר -1 • IndexOf(str, start)מחזירה מיקום של המחרוזת (או התו) str במחרוזת • מיקום מתחיל מ start, אם לא נמצא מחזיר -1 ModuleModule1 Sub Main() Dim s AsString s = "This is a nice string" Dim place AsInteger = 0 place = s.IndexOf("i") While place <> -1 Console.WriteLine("i is at place " & place) place = s.IndexOf("i", place + 1) EndWhile EndSub EndModule

  9. החלפה במחרוזות LastIndexOf • LastIndexOf(str) מחזירה מיקום מסוף המחרוזת (או התו) str • מיקום מתחיל מסוף המחרוזת, אם לא נמצא מחזיר -1 • LastIndexOf(str, end)מחזירה מיקום מסוף המחרוזת (או תו) str • מיקום מתחיל מ end, אם לא נמצא מחזיר -1 ModuleModule1 Sub Main() Dim s AsString s = "This is a nice string" Dim place AsInteger = 0 place = s.LastIndexOf("i") While place <> -1 Console.WriteLine("i is at place " & place) place = s.LastIndexOf("i", place - 1) EndWhile EndSub EndModule

  10. חלק ממחרוזת SubString • SubString(start, count )מייצרת מחרוזת של count תווים החל מהמקום start • ומחזירה את המחרוזת החדשה ModuleModule1 Sub Main() DimstrAsString Console.WriteLine("Please enter a string") str = Console.ReadLine() Dim p1 AsInteger = str.IndexOf("a") str = str.Substring(p1 + 1, 5) Console.WriteLine("the five letters after a are:" & str) EndSub EndModule

  11. שליפת תו ממחרוזת Chars ModuleModule1 Sub Main() DimmyStringAsString = "ABCDE" DimmyCharAsChar myChar = myString.Chars(3)'value of myChar is D. myChar= myString(2) 'גם זה עובד , value is C EndSub EndModule

  12. דוגמא ModuleModule1 Sub Main() Dim x AsString x = Console.ReadLine Console.WriteLine("The first letter is " & x(0)) If (x(0) = "A") Then Console.WriteLine("Yeah!") EndIf If (x(1) = " ") Then Console.WriteLine("Space in second position") EndIf EndSub EndModule

  13. השוואת מחרוזות • למחרוזות יש סדר • הסדר הוא כמו סדר במילון • apple < banana • Abc < Aef • אותיות גדולות (כולן) לפני האותיות הקטנות • לכן Banana < apple • ולכן ניתן להשוות בין מחרוזות...

  14. השוואת מחרוזות ModuleModule1 Sub Main() DimfirstStrAsString = Console.ReadLine() DimsecondStrAsString = Console.ReadLine() IffirstStr < secondStrThen Console.WriteLine("{0} is before {1} ", firstStr, secondStr) ElseIffirstStr > secondStrThen Console.WriteLine("{1} is before {0} ", firstStr, secondStr) Else Console.WriteLine("{0} is same as {1} ", firstStr, secondStr) EndIf EndSub EndModule

  15. דוגמא ModuleModule1 Sub Main() Dim name AsString = "AmitYoav Cohen" Console.WriteLine("The entire name is '{0}'", name) Dim p1 AsInteger = name.IndexOf(" ") Dim p2 AsInteger = 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) EndIf EndSub'Main EndModule

  16. דוגמא נוספת ModuleModule1 Sub Main() Dim x AsString Dimi, j AsInteger x = Console.ReadLine Console.WriteLine("The Length is " & x.Length()) Fori = 0 Tox.Length() - 1 For j = 0 Toi Console.Write(x(j)) Next Console.WriteLine() Next EndSub EndModule

  17. דוגמא לשימוש בפונקציה עם מחרוזת ModuleModule1 Functionmult(ByVal x AsString, ByVal n AsInteger) DimstrAsString = "" Fori = 1 To n str += x + " " Next Returnstr EndFunction Sub Main() Dim x AsString x = Console.ReadLine Dim y AsString = mult(x, 3) Console.WriteLine("The multiplied string is " & y) EndSub EndModule

  18. עוד פונקציה עם מחרוזת ModuleModule1 Function Change(ByVal x AsString) AsString DimiAsInteger Fori = 0 Tox.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 EndIf Next Return x EndFunction Sub Main() DimstrAsString str = Console.ReadLine Console.WriteLine("after change: " & Change(str)) EndSub EndModule

  19. שימוש ב ByRef ModuleModule1 Sub Change(ByRef x AsString) DimiAsInteger Fori = 0 Tox.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 EndIf Next EndSub Sub Main() DimstrAsString str = Console.ReadLine Change(str) Console.WriteLine("after change: " & str) EndSub EndModule

  20. פונקציה בוליאנית ומחרוזת – חלק 1 – מהעושה הפונקציה? ModuleModule1 FunctionMyC (ByVal str1 AsString, ByVal str2 AsString) AsBoolean DimiAsInteger If (str1.Length <> str2.Length) Then ReturnFalse Else Fori = 0 To str1.Length() - 1 If str1(i) <> str2(i) Then ReturnFalse EndIf Next EndIf ReturnTrue EndFunction המשך בשקף הבא...

  21. פונקציה בוליאנית ומחרוזת – חלק 2 המשך... Sub Main() Dim a, b AsString a = Console.ReadLine b = Console.ReadLine If (a = b) Then Console.WriteLine("They are the same") EndIf If (MyC (a, b)) Then Console.WriteLine("They are still the same") EndIf EndSub EndModule

  22. עוד דוגמא – חלק 1 ModuleModule1 Function Flip(ByVal word1 AsString) AsBoolean DimiAsInteger Fori = 0 To word1.Length() - 1 If word1(i) <> word1(word1.Length() - 1 - i) Then ReturnFalse EndIf Next ReturnTrue EndFunction המשך בשקף הבא...

  23. עוד דוגמא – חלק 2 המשך... Sub Main() Dim a, b AsString a = Console.ReadLine If (Flip(a)) Then Console.WriteLine("It is a palindrome") Else Console.WriteLine("It isn't") EndIf EndSub EndModule

More Related