2014-07-05 73 views
0

我有一个字符串test_string像“你好,二是一些””,我想第7位和12之间的字符复制在我使用的编程语言:蟒蛇复制串定位

test_string = 'Hello, two is a number' 
new_string = string_copy(test_string, 7, 12) 

new_string的值将是“两我”。我的例子可能有点愚蠢,但在Python不函数存在等于string_copy?

回答

0

这是一个内置的功能,这就是所谓的切片

'Hello, two is a number'[7:12] 

test_string = 'Hello, two is a number' 
new_string = test_string[7:12] 

你看起来虽然已经算错,因为其结果将是“两个I”(没有前导空格)。

编辑:或者,如零比雷埃夫斯指出,您的语言使用1 - 基于索引的对比到Python

又见https://docs.python.org/3/tutorial/introduction.html#lists

+0

这应该是'“你好,二是一些” [ 6:12]'...问题假设基于1的索引和关闭[间隔](http://en.wikipedia.org/wiki/Interval_%28mathematics%29#Terminology)(这是合理的,只是不是Python如何做它),而Python的切片是[基于零](http://en.wikipedia.org/wiki/Zero-based_numbering)和半开放。 –

+1

感谢您的答案,您的方法就像一个魅力。我检查了确实使用基于1的索引的GML(Game Maker Language)文档。 –