Reputation: 103
Is there any way to split and then join a string in a textbox?
Example:
I had a textbox named Textbox1 that has a value of "001-2012-0116"
then I want to split it by this ("-") and then join the resulting 3 strings
Then the result will become "00120120116"
then I want to get the result and put it in a resultNumber
whose datatype is string.
To summarize it :
Textbox1(Value) = "001-2012-0116"
Dim resultNumber as String
resultNumber(Value) = "00120120116"
Upvotes: 4
Views: 255
Reputation: 8004
I know this has been answered, but sometimes I prefer to use the RegularExpression
Class.
In your case this is how it can be accomplished...
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 your doing a simple replace on a single string, so the answer that you accepted would be more suitable in this case. Regular expressions in my opinion are great when dealing with patterns and such.
Upvotes: 0
Reputation: 89295
As others suggest, split isn't the best way to go in this case. You'll need to use String.Join()
after splitting to get that expected result :
Dim resultNumber = String.Join("", "001-2012-0116".Split("-"))
So it is kind of double work. Will be more concise to simply replace dash (-
) with empty string :
Dim resultNumber = "001-2012-0116".Replace("-", "");
Upvotes: 3
Reputation: 5879
You want to use String.Replace()
in this situation:
resultNumber = Textbox1.Value.ToString().Replace("-", String.Empty)
Obviously this keeps everything as a String
, but if you want to use the string as numeric (as they are numeric) then you would need to convert it to an Integer
.
If you want to split the value into an array of strings then you can use the following, but you're not really wanting this result, but you asked about splitting strings...
Dim result As String() = words.Split("-")
Upvotes: 0
Reputation: 1447
You can try this approach as inspiration:
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
Upvotes: 0