How to split on vb.net?

Is there a way to split and then join a line in a text box?

Example:

I had a text box called Textbox1 that matters "001-2012-0116", then I want to split it into this ("-") and then append the resulting 3 lines

Then the result will be "00120120116"

then I want to get the result and put it in resultNumber, whose data type is a string.

Summarizing:

Textbox1(Value) = "001-2012-0116"

Dim resultNumber as String

resultNumber(Value) = "00120120116"
+4
source share
4 answers

As others show, a split is not the best way to go in this case. You will need to use it String.Join()after splitting to get the expected result:

Dim resultNumber = String.Join("", "001-2012-0116".Split("-"))

, . , (-) :

Dim resultNumber = "001-2012-0116".Replace("-", "");
+3

String.Replace() :

resultNumber = Textbox1.Value.ToString().Replace("-", String.Empty)

, String, ( ), Integer.

, , , ...

Dim result As String() = words.Split("-")
0

:

Sub Main
Dim txt as String
Dim intVal as Int64
txt =  "001-2012-0116"

If(Int64.TryParse(txt.Replace("-",""), intVal))
Console.WriteLine(intVal)
End If

End Sub
0

, , RegularExpression.

, ...

 Imports System.Text.RegularExpressions 'Need to import the namespace

 Dim resultNumber As String =  Regex.Replace(TextBox1.Text, "-", String.Empty) 'Do a simple replace on "-" 

On the other hand, you are doing a simple replacement on one line, so the answer you accepted would be more appropriate in this case. Regular expressions, in my opinion, are good when working with templates, etc.

0
source

Source: https://habr.com/ru/post/1531905/


All Articles