diff --git a/src/Collections-Strings-Tests/StringTest.class.st b/src/Collections-Strings-Tests/StringTest.class.st index 470f90eb97c..dd6779468c7 100644 --- a/src/Collections-Strings-Tests/StringTest.class.st +++ b/src/Collections-Strings-Tests/StringTest.class.st @@ -1260,6 +1260,24 @@ StringTest >> testFormatFailures [ ] +{ #category : #test } +StringTest >> testHasSubstringAt [ + + "Test empty strings" + self assert: ('' hasSubstring: '' at: 1). + self assert: ('test' hasSubstring: '' at: 1). + self deny: ('' hasSubstring: 'test' at: 1). + "Test out of bounds indexes" + self deny: ('test' hasSubstring: 't' at: 0). + self deny: ('test' hasSubstring: 't' at: 10). + "Test matching substring" + self assert: ('test' hasSubstring: 'st' at: 3). + "Test partially matching substring" + self deny: ('test' hasSubstring: 'sty' at: 3). + + +] + { #category : #tests } StringTest >> testHasWideCharacterFromTo [ diff --git a/src/Collections-Strings/String.class.st b/src/Collections-Strings/String.class.st index d8c95477d83..c3028c4703f 100644 --- a/src/Collections-Strings/String.class.st +++ b/src/Collections-Strings/String.class.st @@ -1601,12 +1601,14 @@ String >> hasSubstring: str at: index [ "Answer true if the receiver contains the substring str exactly at index, false otherwise." | pos | + + (index < 1) ifTrue: [ ^ false ]. pos := index - 1. - + ^ str size <= (self size - pos) and: - [ str allSatisfy: [ :char | + [ str noneSatisfy: [ :char | pos := pos + 1. - (self at: pos) = char ] ] + (self at: pos) ~= char ] ] ] { #category : #testing }