Upload
lotte
View
47
Download
4
Embed Size (px)
DESCRIPTION
הרצאה 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. - PowerPoint PPT Presentation
Citation preview
המחשב למדעי מבואוניהול לתעשייה
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
מחרוזות על פונקציותLengthRemoveInsertReplaceIndexOfLastIndexOfSubStringChars
מחרוזות של 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) Console.WriteLine(str2)
End SubEnd Module
במחרוזות ReplaceהחלפהReplace(str1, str2) המחרוזת את str2במחרוזת str1מחליפהReplace(ch1, ch2) התו את ch2בתו ch1מחליפה
החדשה המחרוזת את מחזירה מקרה בכל
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") Console.WriteLine(str2)
End SubEnd 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 SubEnd 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 SubEnd 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 If
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()
Next
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
מחרוזת עם פונקציה עוד
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 Sub
End Module
ב ByRefשימוש
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
Return False
End If
Next
End If
Return True
End Function
... הבא בשקף המשך
חלק – ומחרוזת בוליאנית ?1פונקציה הפונקציה – מהעושה
המשך...
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 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
... הבא בשקף המשך
חלק – דוגמא 1עוד
המשך...
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
חלק – דוגמא 2עוד