מחרוזות של תווים strings

21
ללללללל לל לללללStrings

Upload: lesa

Post on 21-Mar-2016

48 views

Category:

Documents


5 download

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 Presentation

TRANSCRIPT

Page 1: מחרוזות של תווים Strings

מחרוזות של תווים

Strings

Page 2: מחרוזות של תווים Strings

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5

Strings - מחרוזות

Dim s As String

s = “hello”

s = s & “kita”

או

s = s + “kita”

hellohellokita

Page 3: מחרוזות של תווים Strings

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5

מחזירה אורך המחרוזת

Module Module1 Sub Main() Dim word As String word = Console.ReadLine Console.WriteLine(Len(word)) Console.WriteLine(word.Length()) End SubEnd Module

מחרוזותפעולות על

Len

Page 4: מחרוזות של תווים Strings

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5

מחזירה תת-מחרוזת משמאל

a = “hello kita”x = Left (a,2)

מחרוזותפעולות על

Left

heאיזה כמה תויםמחרוזת

Page 5: מחרוזות של תווים Strings

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5

מחזירה תת-מחרוזת מימין

a = “hello kita”x = Right (a,6)

מחרוזותפעולות על

Right

o kitaאיזה כמה תויםמחרוזת

Page 6: מחרוזות של תווים Strings

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5

מחזירה תת-מחרוזת

a = “hello kita”x = Mid (a,2,3)

מחרוזותפעולות על

Mid

ellהחל מתו-כמה תוים

Page 7: מחרוזות של תווים Strings

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו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)? לא

Page 8: מחרוזות של תווים Strings

מחזירה אינדקס של תת מחרוזת במחרוזת

a = “hello kita”x = a.IndexOf("kit",0)

מחרוזותפעולות על

IndexOf

תת 6מחרוזת

החל מתו-

Page 9: מחרוזות של תווים Strings

מחזירה אינדקס של תת מחרוזת במחרוזת

str1 = "I am happy"str2 = str1.Substring(5, 2)

מחרוזותפעולות על

Substring

haהחל מתו-כמה תוים

Page 10: מחרוזות של תווים Strings

מבוא לתכנות למנע"ס - שבוע מספר - מאיר קומר - סמסטר ב' - תשס"ו5

זה חוקי!

"דוד" > "שלמה"?

Page 11: מחרוזות של תווים Strings

דוגמא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

Page 12: מחרוזות של תווים Strings

שימוש בסיסי במחרוזת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

Page 13: מחרוזות של תווים Strings

שימוש בסיסי במחרוזת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

Page 14: מחרוזות של תווים Strings

פעולות בסיסיות במחזרות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

Page 15: מחרוזות של תווים Strings

דוגמא של לולאה במחרוזת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

Page 16: מחרוזות של תווים Strings

פונקציות עם מחרוזות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

Page 17: מחרוזות של תווים Strings

פונקציה יותר מסובכת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

Page 18: מחרוזות של תווים Strings

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

Page 19: מחרוזות של תווים Strings

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

...המשך בשקף הבא

Page 20: מחרוזות של תווים Strings

פונקציות בוליאנית (שימוש)

המשך משקף קודם...

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

Page 21: מחרוזות של תווים Strings

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