I have a string:
string = "Hello.world, have a nice day"
Is there a way to split the string with point or comma as delimeter, but only retain the delimeter into the array? (whitespace is separator but is not retain)
['Hello','.','world',',','have','a','nice','day']
which delimeter for regex.split(delimeter)
would be preferable?
and this is my code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arr As String() = Regex.Split(TextBox1.Text, "[.|\,]")
For Each i As String In arr
Console.WriteLine(i)
Next
End Sub
sorry for bad english
If you want to split by a dot or a comma, you can update your character class to [.,]
because the character class would match any of the listed characters.
The character class [.|\,]
can for example also be written as [.,|]
and note that you don't have to escape the comma .
The use a capture group to keep the delimiters.
Your final pattern would look like ([.,])
See the vb.net demo
For example:
Dim s As String = "Hello.world, have a nice day"
Dim arr As String() = Regex.Split(s, "([.,])")
For Each i As String In arr
Console.WriteLine(i)
Next
Result:
Hello
.
world
,
have a nice day