I have the data with the pattern of 8 char in my string. Those data are form from digits. Example: 00000001, 00004235, 081035670. Is that any methods in vb i can use to remove all the left padding zero from each of the strings?
You want String.TrimStart, it removes the leading character (you could also use an array and remove a set of characters).
Dim lines as List(Of String) = From {"00000001", "00004235", "081035670"}
For Each str as String in lines
Console.WriteLine(str.TrimStart("0"c)
Next
Just convert it to Integer and convert back to string
Dim data As String = "00000001"
data = Convert.ToInt32(data).ToString()
MessageBox.Show(data)
UPDATED:
String.TrimStart() is better solution (answered by jmoreno)
Related
is there any way to remove the element in char array I converted from a String?
Dim myString As String = "Hello"
Dim charArray As Char() = myString.ToCharArray
Your help would be much appreciated.. Thanks
Arrays are fixed-length in .NET. You can set an element to Nothing but you cannot remove that element. You can use a collection instead of an array and then you can add, insert and remove items at will, but your example isn't necessarily the best case for that sort of thing, e.g.
Dim str = "Hello"
Dim chars = New List(Of Char)(str)
You can then call Remove or RemoveAt on that List to remove a Char. You can then create a new String if desired, e.g.
chars.RemoveAt(2)
str = New String(chars.ToArray())
Console.WriteLine(str)
That will display "Helo".
I have this string: 71892378917238978
I want to do this: 71892-37891723-8978
I was trying this, but how i can do it multiple times?
String.Insert(6, "-")
We could start with this idea. Create an array of the substrings that you want to exist between the dash symbol. In your expected string you have 71892-37891723-8978, so the first block is composed of 5 char and the second block is 8 char.
Using the string.Substring method we could extract these substrings parts and store them in an array. Finally we could use string.Join to rebuild the string with the expected separator
Dim s As String = "71892378917238978"
' Define the substring blocks
Dim blocks As Integer() = New Integer() {5,8}
' Define the array that will contain the substrings
Dim substrings(blocks.Length) As String
' Get the blocks
For x As Integer = 0 To blocks.Length - 1
substrings(x) = s.Substring(0,blocks(x))
s = s.Substring(blocks(x))
Next
' Join together with the last part not included in the substrings
Dim result = String.Join("-", substrings) & s
Console.WriteLine(result)
You could generalize this code putting everything in a method where you could pass the string, the blocks and the separator.
This question already has answers here:
How do I convert Unicode escape sequences to Unicode characters in a .NET string?
(5 answers)
Closed 3 years ago.
I am trying to parse a string (returned by a web server), which contains non-standard (as far as I can tell) unicode Id's such as "\Ud83c" or "\U293c", as well as plain text. I need to display this string, emojis in tact, to the user in a datagrid view.
btw, I am blind so please excuse any formatting errors :(
full example of what my code is parsing: "Castle: \Ud83d\Udc40Jerusal\U00e9m.Miles"
the code I wrote which is failing miserably:
Public Function ParseUnicodeId(LNKText As String) As String
Dim workingarray() As String
Dim CurString As String
Dim finalString As String
finalString = ""
' split at \ char
workingarray = Split(LNKText, chr(92))
For Each CurString In workingarray
If CurString <> "" Then
' remove leading U so number can be converted to hex
CurString = Right(CurString, Len(CurString) - 1)
' attempt to cut off right most chars until number can be converted to text as there is nothign separating end of Unicode chars and start of plain text
Do While IsNumeric(CurString) = False
If CurString = "" Then
Exit Do
End If
CurString = Left(CurString, Len(CurString) - 1)
Loop
If CurString.StartsWith("U", StringComparison.InvariantCultureIgnoreCase) Then
CurString = CurString.Substring(1)
End If
' convert result from above to hex
Dim numeric = Int32.Parse(CurString, NumberStyles.HexNumber)
' convert to bytes
Dim bytes = BitConverter.GetBytes(numeric)
' convert resulting bytes to a real char for display
finalString = finalString & Encoding.Unicode.GetString(bytes)
End If
Next
ParseUnicodeId = finalString
End Function
I tried to do this all kinds of ways; but can't seem to get it right. My code currently returns empty strings, although my guess is that is because of some of the more recent changes I have made to cut off the leading U or to try and chop off one char at a time. If I take those bits out and just pass it something like "Ud83c", it works perfectly; its only when plain text is mixed in that it fails, but I can't seem to come up with a way to separate the two and re-combine at the end.
You can use Regex.Unescape() to convert the unicode escaped char (\uXXXX) to a string.
If you receive \U instead of \u, you also need to perform that substitution, since \U is not recognized as a valid escape sequence.
Dim input as String = "Castle: \Ud83d\Udc40Jerusal\U00e9m.Miles"
Dim result As String = Regex.Unescape(input.Replace("\U", "\u")).
This prints (it may depend on the Font used):
Castle: 👀Jerusalém.Miles
As a note, you might also have used the wrong encoding when you decoded the input stream.
I have many strings that have numbers at the end. The numbers can be of any size, for example:
myvar123
mysecondvar3
mythirdvar219107
The strings can have numbers even inside the name, not only at the end.
for example:
my2varable123
some123variable9480395
I would need to replace any number at the END with a placeholder. (NOT those inside the varname.)
for example:
my2varable123 should become: my2variable%placeholder%
some123variable9480395 should become: some123variable%placeholder%
The only way that comes to my mind is to go through the string using .right() and remove the char if it is numeric until I find the first non numeric char. Then in the end append the placeholder, but it looks like a lot of work for a rather simply problem.
Is there a better way to do this?
You could use a Regex for this.
Dim str As String = "some123variable9480395"
Dim pattern As String = "\d+$"
Dim replacement As String = "%placeholder%"
Dim rgx As Regex = New Regex(pattern)
Dim result As String = rgx.Replace(str, replacement)
i have two example
"hcg.com.ph?C402-10A-2012-06132017-22"
"hcg.com.?C3032-1B-2012-06132017-1"
output should be
hcg.com.ph?C402-10A-2012-06132017
hcg.com.?C3032-1B-2012-06132017
but i got
hcg.com.ph?C402 and hcg.com.?C3032
Dim FinalSplt() As String
Dim ItemBaseCode As String
FinalSplt = value.ToString.Split("-")
ItemBaseCode = FinalSplt(0)
How to split in the last dash?
Here is some code that uses Substring and LastIndexOf.
'test input
Dim si() As String = {"hcg.com.ph?C402-10A-2012-06132017-22", "hcg.com.?C3032-1B-2012-06132017-1"}
'use to verify
Dim sv() As String = {"hcg.com.ph?C402-10A-2012-06132017", "hcg.com.?C3032-1B-2012-06132017"}
For x As Integer = 0 To si.Length - 1
Dim s As String = si(x).Substring(0, si(x).LastIndexOf("-"c))
'verify
If s = sv(x) Then
Stop 'verified
End If
Next
Ok, without actually writing code I can see you need to split the string more efficiently.
Firstly, strip the quotes.
Secondly, split the string based on the ? mark.
Take the second string in the array, and split that based on the - mark.
Now you have an array of all the portions, join this array with all except the last element.
Join the new string with the original first part.
Add the quotes back if needed.