VB.Net $ in end of string - vb.net

I came across the following VB.Net code:
Dim Test as String = "9999"
Dim Test2 as String = Test$
What does $ mean in a string's name, such as is used in the second line?

This is a Type Notifier, which identifies the usage of Test as a string.
See also:
http://www.aivosto.com/vbtips/stringopt.html
what is the meaning of the dollar sign after a method name in vb.net

Related

Sub routine definition what does $ mean

Okay I have never used vb script and I am trying to convert a legacy vb module to c#. The syntax is fine but man this function definition just complete threw me for a loop.
What does $ % and # mean in the declaration.
Like for example pop(name$,balance#, id%)
They are called Type Characters. From MSDN:
Visual Basic supplies a set of identifier type characters, which you can use in a declaration to specify the data type of a variable or constant.
Here is the list, reprinted from the link:
Identifier type character Data type Example
---------------------------------------------------------------------
% Integer Dim L%
& Long Dim M&
# Decimal Const W# = 37.5
! Single Dim Q!
# Double Dim X#
$ String Dim V$ = "Secret"

Inserting a String inside a String

I want to insert a word inside an existing word? Both are Strings.
For example:
Given String word:
HELLO SAMPLE SENTENCE
i want to insert the word I AM A so my output would be:
HELLO I AM A SAMPLE SENTENCE
i am inserting here basing on the word SAMPLE. So the insertion starts before the word SAMPLE. is this possible?
Based on the description of your logic (which isn't much to go on), I would use:
Dim input As String = "HELLO SAMPLE SENTENCE"
Dim iSample As Integer = input.IndexOf("SAMPLE")
Dim output As String = input.Insert(iSample, "I AM A ")
This uses the BCL function String.Insert, which simply inserts a string into another string at a particular position.
Create a function like this:
Function InsertBefore(sentence As String, find As String, textToInsert As String
Return sentence.Replace(find, textToInsert+Find)
End Function
And call it like this:
sentence = InsertBefore("HELLO SAMPLE SENTENCE", " SAMPLE ", "I AM A")
If I remember correctly, you can use the String.split() function on your string.
See DotNetPerls' page on Split here.
You can split the string into an array, then insert the line you want into the array, then join them back together using String.Join() (thanks Monty, I don't use Visual Basic that frequently anymore, I forgot that :)).
Hope this help :)

How to add only 1 " in String

this might be a super easy question but it is not. I am wondering how to put only a single " in a string
Example
Dim eg as String
eg = ""W"
It just does not seem to work. Is there any special characters for it as i need to add in codebehind for my VB.
If it's Visual Basic you need one more ":
Dim eg as String
eg = """W"
If it's C#, use the \ escape character: "\"W"
http://msdn.microsoft.com/en-us/library/267k4fw5.aspx
Just double it:
Dim eg as String
eg = """W"

Detect chinese character in a string VB.NET

Is there a way to detect a Chinese character in a string which is build like this:
dim test as string = "letters 中國的"
Now I want to substring only the Chinese characters. But my code is database driven, so I can't substring it, because the length is always different. So is there a way I can split the string, from the moment I detect a Chinese character?
I think you can use regexp like in the following example, didn't test and I haven't code using VB.net for years So syntax may be not correct.
Dim m As Match = Regex.Match(value, "[\u4e00-\u9fa5]+",
RegexOptions.IgnoreCase)
' If successful, write the group.
If (m.Success) Then
Dim key As String = m.Groups(1).Value
End If
http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx

How to write this regular expression in VB.net?

My coworker needs me to write him a regular expression for his vb.net app.
I do not know vb and he does not know regex.
The regex he needs is:
/.*web id: ?(\d+).*/i
Basically he needs to search a string for something like "web id: 345" or "web id:2534" and retrieve the ID.
He took what I gave him above and was able to put this together:
Dim strPattern As String = ".*web id: ?(\d+).*"
Dim strReplacement$ = "$1"
GetWebId$ = Regex.Replace(LCase$(strNote$), strPattern$, strReplacement$)
However I am not sure how you pass the case-insensitive flag? (his current fix for that is making the whole string lowercase first)
Also one thing I can't seem to figure out is when he runs this on a string with multiple lines, any text that is not on the same line as "web id: \d" is also being returned which i find strange.
Use the RegexOptions.IgnoreCase flag:
Regex.Replace(strNote, strPattern, strReplacement, RegexOptions.IgnoreCase)
If you are going to ignore case there should be no need to use LCase. I also find it odd that you have all those $ symbols in your variable names - they shouldn't be valid in either C# or VB.NET.
EDIT #2: I realize you may have wanted to replace the entire line that matched with the $1 replacement pattern to match the ID. If you have a need to use multiple options you can Or them together as follows:
Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
EDIT #1: you are using the wrong method to extract the ID. You have a group (\d+) to capture the ID, but you are using Regex.Replace on your match, which is why you get everything else in the text. To match the ID use the following:
Dim input As String = "foo web id:2010 bar"
Dim pattern As String = ".*web id: ?(\d+).*"
Dim m As Match = Regex.Match(input, pattern, RegexOptions.IgnoreCase)
If m.Success Then
Dim id As String = m.Groups(1).Value
Console.WriteLine("ID: " & id)
Else
Console.WriteLine("No Match!")
End If
You will notice we refer to Groups(1) which holds the value captured by the (\d+) group. Patterns with more groups may lead to confusion, especially with nested groups. In those cases you can use named groups. Here is the same code updated to use named groups:
Dim input As String = "foo web id:2010 bar"
Dim pattern As String = ".*web id: ?(?<ID>\d+).*" ' group name added '
Dim m As Match = Regex.Match(input, pattern, RegexOptions.IgnoreCase)
If m.Success Then
' refer to group by group name '
Dim id As String = m.Groups("ID").Value
Console.WriteLine("ID: " & id)
Else
Console.WriteLine("No Match!")
End If
Kind of unrelated, but this code is a collection of things you shouldn’t do in VB.NET.
You shouldn’t use the old $ suffix on string variables, and likewise you shouldn’t use old functions such as LCase$. There are equivalent functions in the framework that should be used. You can also tell your friend to always enable Option Strict while where at it. This will catch a lot of potential bugs.
Furthermore, to set the return value of a function, the … “more established” method is to use Return …, not Functionname = ….
So the “correct” code will look like this:
''// I’m assuming that `GetWebId` is the name of the function we’re in.
Function GetWebId(note As String) As String
Dim pattern As String = ".*web id: ?(\d+).*"
Dim replacement As String = "$1"
Return Regex.Replace(note.ToLower(), pattern, replacement)
End Function
See Ahmad’s solution of how to get the “ignorecase” flag into the expression.