# ----------------------------------------------------------------
#   REPLACE  method
#
#  Substitutes part of a string by another string
#  Beware:
# ----------------------------------------------------------------

# Example: Sometimes you want to replace or manipulate
# separators between items in the string

s = "neon, lead,tin, carbon, iron,tin,gold, lead,zinc, molybdenum"
# note irregular spaces after the commas

# Replace commas by semicolons
s2 = s.replace( ",", ";" )
print( s2 )
print()


# Add spaces, so that each semicolon is followed by EXACTLY ONE space
# It takes a little trick
#    1. Add space after *each* semicolon
#       == replace semicolon by semicolon + space
s3 = s2.replace( ";", "; " )
print( s3 )
print()
#    2. substitute each double space by single space
s3 = s3.replace( "  ", " " )
print( s3 )
print()


# CAUTION: replace is NOT all-powerful,
# it neglects overlapping identical copies of substrings.

s = "ababababab"
s2 = s.replace("abab", "bcde")
print( s )
print( s2 )

# Note that there are THREE substrings abab in abababab.
# Only two were replaced.

s = "bob_x_bobob_x_bob"
# Substitute each 'bob' by  'kayak'
# you should get 'kayak_ kayakayak_x_kayak'
s2 = s.replace("bob", "kayak")
print( s )
print( "incorrect replacement:", s2 )
print()



# ----------------------------------------------------------------
#   COUNT  method
#
#  Counts occurences of a string in another string
#  Beware:  Not correctly!
# ----------------------------------------------------------------


s = "Hello world!"
print( s, "Count subbstrings o and or: ",  s.count("o"), s.count("or") )
print()

# CAUTION: count is NOT all-powerful,
# it neglects overlapping identical copies of substrings.

s = "BEWARE! count occurences of 'a' and 'aa' in:   'aaa'"
print( "String s = ", "\"" + s + "\"" )
print( "Counts of a, aa, aaa in  s:" )
print( s.count("a"), s.count("aa"), s.count("aaa") )

# Fix the counting problem with a specific individually written function
def countOccurrences( shortStr, longStr ):
    # maximally slow, brute force approach, for simplicity
    # advanced faster methods would take too much space here
    counter = 0
    for start in range( len(longStr) ):
        if shortStr == longStr[start: start+len(shortStr)] :
            counter += 1
    return counter

print( "Counts of a, aa, aaa in  s  correctly:  " )
print( countOccurrences("a", s), countOccurrences("aa", s), countOccurrences("aaa", s) )
print()

# ----------------------------------------------------------------
#   SPLIT and JOIN   methods
#
# ----------------------------------------------------------------


s = "a bc,   def ghij, klmno pqrs, tuv wx, y"
split1 = s.split(" ")
split2 = s.split(",")
print( s )
print( split1 )
print( split2 )
print()

# -------------------------------------------------------------
# join() glues together all elements of a list
# using another specified string as a "glue"
# into a single string:


statement = "Examples of cities are: "
cities = ["Prague", "Madrid", "Athens", "Stockholm"]
print( cities )
joined = " and ".join(cities)
print( statement + joined + "." )
joined = ", ".join(cities)
print( statement + joined + "." )
joined = " xyz ".join(cities)
print( statement + joined + "." )




