擅长:python、mysql、java
<p>严格地说,你可能更适合一个函数</p>
<pre><code>def trim_after_repeat(source, delimiter, count = 3):
# This breaks the string into a list of strings based on your delimiter
pieces = source.split(delimiter)
# This sets items_wanted to match the count you want
items_wanted = pieces[0:count]
# This puts the items_wanted back together
return delimiter.join(items_wanted)
string_to_trim = 'John Lasseter , Pete Docter , Andrew Stanton , Joe Ranft ,'
print(trim_after_repeat(string_to_trim), ' , ')
# 'John Lasseter , Pete Docter , Andrew Stanton'
</code></pre>