Skip to content

Commit ebd6687

Browse files
binkkatalpatil-ashutosh
authored andcommitted
ADD: Check for special characters in a string
1 parent d1b0162 commit ebd6687

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

regex/rstring/string.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ func SplitString(n int, strs ...string) []string {
4141

4242
return split
4343
}
44+
45+
// ContainsSpecialChars checks for special characters.
46+
func ContainsSpecialChars(s string) bool {
47+
isStringAlphabetic := regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString
48+
49+
return !isStringAlphabetic(s)
50+
}

regex/rstring/string_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,28 @@ func ExampleSplitString_other() {
7777
fmt.Println(rstring.SplitString(-1, "mango is fruit"))
7878
// Output: [mango is fruit]
7979
}
80+
81+
func TestContainsSpecialChars(t *testing.T) {
82+
type test struct {
83+
input string
84+
result bool
85+
}
86+
87+
tests := []test{{input: "abc", result: false}}
88+
chars := "!/*-+_@&$#%"
89+
90+
for _, v := range chars {
91+
tests = append(tests, test{input: string(v), result: true})
92+
}
93+
94+
for _, tc := range tests {
95+
tc := tc
96+
t.Run(tc.input, func(t *testing.T) {
97+
result := rstring.ContainsSpecialChars(tc.input)
98+
if result != tc.result {
99+
t.Errorf("ContainsSpecialChars(%s) Failed: expected %+v, actual %+v",
100+
tc.input, tc.result, result)
101+
}
102+
})
103+
}
104+
}

0 commit comments

Comments
 (0)