Get string after second delimiter VB NET - string

I am trying to get the part of the string after the second /.
For instance if I have the string "25/S17/874"
I would like to get "874"
Thanks

you can split the string by "/" and the call back from the array.
Example:
Dim string_sample As String
Dim string_arr As String()
string_sample = "25/S17/874"
string_arr = string_sample.Split("/")
Dim string_Result As String
string_Result = string_arr(2)
MsgBox(string_Result)
the string_Result is "874"

Dim result As String = "25/S17/874".Split("/"c)(2)
Obviously, some validation, error trapping, etc might be required if the string content is prone to variation.

Related

Indent a string by 4 spaces (Add Tab to string)

I am trying to add indentation to a string, essentially adding 4 spaces in front of each line in the string. The string that I want to add the indentation to is called StringToIndent.
Public Class ModifyPage
Private Sub Button_Test_Click(sender As Object, e As RoutedEventArgs) Handles Button_Test.Click
Dim StringToIndent As String = ("This is the first row
This is the second row
This is the third and final row in MyString")
Dim MySecondString As String = "This is a string in one line."
Dim BothStringsTogether = StringToIndent & Environment.NewLine & MySecondString
Debug.Write(BothStringsTogether)
End Sub
End Class
The current output:
This is the first row
This is the second row
This is the third and final row in MyString
This is a string in one line.
I want the final code (that is indented) to output:
This is the first row
This is the second row
This is the third and final row in MyString
This is a string in one line.
How can this be achieved through code? Is there a formatting option that allows me to add indentation? A method that doesn't require me to loop through a string and adding four spaces for each line would be preferable.
Edit: A way to achieve the expected output is to replace the new line with a new line and then add the indent. However, there must be a more elegant way of doing it?
Code:
Dim StringToIndent As String = ("This is the first row
This is the second row
This is the third and final row in MyString")
Dim indentAmount = 4
Dim indent = New String(" "c, indentAmount)
StringToIndent = indent & StringToIndent.Replace(Environment.NewLine, Environment.NewLine & indent)
Debug.Write(StringToIndent)
Maybe something like:
Dim res as String
Dim parts As String() = StringToIndent.Split(ControlChars.CrLf.ToCharArray)
For Each part As String In parts
res.Append(" ") & part & vbCrLf
Next
In C# you can mark the String as a verbatim string literal by prefixing the literal with the # symbol.
In VB.NET we don't have this option. Instead, a workaround would be to create an XML literal and get the value. Here is an example:
Dim input As String = <element> This is the first row
This is the second row
This is the third and final row in MyString
This is a string in one line.
</element>.Value().ToString()
Debugger.WriteLine(input.ToString())
If the value is not static, e.g. you're getting it from somewhere, then you're forced to iterate through the String in some form. You can either Replace like in your example, do a Split and Join (similar to your example), or you'll need to manually iterate.
The manual iteration could look more elegant using LINQ, but you don't gain anything from it.
The bottom line is that if your String is static then you can use the XML literal example I provided, otherwise if the String is dynamic then your solution is perfectly appropriate.
UPDATE
As Andrew Morton pointed out, multiple line String literals have existed since Visual Studio 2017. The following would produce the same outcome as my XML literal example:
Dim input As String = " This is the first row
This is the second row
This is a string in one line"
Debugger.WriteLine(input)
Using an interpolated string indicated by the $ preceeding the string and the vb constants.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s = $"{vbTab}This Is the first row{vbCrLf}{vbTab}This Is the second row{vbCrLf}{vbTab}{vbTab}This Is the third And final row in MyString{vbCrLf}This Is a string in one line."
Debug.Print(s)
End Sub

How to compare a FieldGetText with a number?

I have a field that it stores a currency data with the name pdcPrimasVAFVigor.
I can get the value with the FieldGetText method about NotesUIDocument like this:
strPrimasVaf = uidoc.Fieldgettext("pdcPrimasVAFVigor")
And then I check the value and the type:
Msgbox "strPrimasVaf: " + Typename(strPrimasVaf)
This messagebox retrievme:
strPrimasVaf: 122.00 € STRING
If I want to compare strPrimasVaf with any number but the comparision doens't work, then I have tried to convert the data but I always get the next error:
Type Mismatch in method CoerStrToNum...
But, what have I tried?
I have tried to convert the string value before to compare like this:
Dim intPrimasVaf As Integer
intPrimasVaf = CInt(strPrimasVaf) ' Error
Dim dblPrimasVaf As Double
dblPrimasVaf = CInt(strPrimasVaf) ' Error
Dim curPrimasVaf As Currency
curPrimasVaf = CInt(strPrimasVaf) ' Error
Include I have try to change the format like this:
Dim x As Variant
x = Format(strPrimasVaf, "General Number")
Any advice or solution within using FieldGetText?
My advice: work with the backend document object.
Add
Dim doc as Notesdocument
Dim dblPrimasVaf As Double
Set doc=uidoc.document
dblPrimasVaf=doc.pdcPrimasVAFVigor(0)
No conversion needed.
Remove the € from the string with something like:
strPrimasVaf = uidoc.Fieldgettext("pdcPrimasVAFVigor")
strPrimasVaf=cdbl(fulltrim(join(split(strPrimasVaf,"€"),"")))

Searching for a word and not a string?

I want to check a file for a particular word the way I have found posted on various forums is to use the following code...
Dim content = My.Computer.FileSystem.ReadAllText(filePath)
If content.Contains("stringToSearch") Then
'Do your stuff
End If
Which is okay until you discover that it will search and match compound words and the likes. For instance If I search for the string light in a file and it's not there but instead the word lightning is, it will still register as having found a match... Is there a way to find and exact word using VB.net?
As mentioned by Andrew Morton, Regex makes this kind of thing very easy. For instance, if you made a function like this:
Public Function ContainsWord(input As String, word As String) As Boolean
Return Regex.IsMatch(input, $"\b{word}\b")
End Function
You could use it like this:
Dim content = My.Computer.FileSystem.ReadAllText(filePath)
If ContainsWord(content, "stringToSearch") Then
'Do your stuff
End If
If you wanted to, you could even make it an extension method on the String type, by putting it in a Module and adding the ExtensionAttribute, like this:
<Extension>
Private Function ContainsWord(input As String, word As String) As Boolean
Return Regex.IsMatch(input, $"\b{word}\b")
End Function
And then you could call it like this:
Dim content = My.Computer.FileSystem.ReadAllText(filePath)
If content.ContainsWord("stringToSearch") Then
'Do your stuff
End If
Another method, using Regex.Matches, which allows to search for a collection of words and returns a Dictionary(Of String, Integer()).
The Dictionary Key represent the matched word, the Value, as an Array of Integers, all the positions inside the File where the word was found.
The extension method requires 2 parameters:
- the path of the file to search
- a boolean value, used to specify whether the search should be case sensitive.
Proposed as an extension method of IEnumerable(Of String):
Dim fileName As String = "[File Path]"
Dim searchWords As String() = {"light", "lighting", "clip", "clipper", "somethingelse"}
Dim result = searchWords.FindWords(fileName, False)
Print a result of the matches found:
result.ToList().ForEach(
Sub(w)
Console.WriteLine($"Word: {w.Key} Positions: {String.Join(", ", w.Value)}")
End Sub)
Extension method:
Imports System.IO
Imports System.Runtime.CompilerServices
Imports System.Text
Imports System.Text.RegularExpressions
Module modIEnumerableExtensions
<Extension()>
Public Function FindWords(words As IEnumerable(Of String),
fileName As String,
caseSentive As Boolean) As Dictionary(Of String, Integer())
Dim pattern As StringBuilder = New StringBuilder()
pattern.Append(String.Concat(words.Select(Function(w) $"\b{w}\b|")))
Dim options As RegexOptions = RegexOptions.Compiled Or
If(caseSentive, RegexOptions.Multiline, RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Dim regx As New Regex(pattern.ToString().TrimEnd("|"c), options)
Dim matches As MatchCollection = regx.Matches(File.ReadAllText(fileName))
Dim groups = matches.OfType(Of Match).
GroupBy(Function(g) g.Value).
ToDictionary(Function(g) g.Key, Function(g) g.Select(Function(m) m.Index).ToArray())
Return groups
End Function
End Module
The shortest and fastest way to do this is using ReadLines with LINQ queries, specialy when you are working with a large files.
Dim myword As String = "Book"
Dim reg = New Regex("\b" & myword & "\b", RegexOptions.IgnoreCase)
Dim res = From line In File.ReadLines(largeFileName)
Where reg.IsMatch(line)
If your file containts "Book", "Books", "Book." and "Book," the results will be:
Book
Book,
Book.
And you can working with results as following
TextBox1.Text = resLines.Count
Or
TextBox1.Text = resLines(0)
Edited to make it consering "." and "," etc.

How do I search an online document for specific string and it's value?

Example, Online document: [removed link as no longer needed]
Which outputs:
Value1=1
Value2=5
The rest of this document would have random text to ignore.
I would like to search for Value1 and Value2, then output it's value [I need this to be expandable if I decide to add new information in the future]
[the output may be longer than one character, and might be text rather than a number]
Dim Value1Result as Integer = [value from online file] '1 in this case
Dim Value2Result as Integer = [value from online file] '5 in this case
Edit: Added logic to strip the version number. Very basic but as long as the format doesn't change it should work. You'll need to handle parsing to int, double, etc if you ever use "1.2" or whatever for a version.
If I understand your question correctly, you just need to download the file, store it in a local variable, and then do something with it. Comment if this is not the case and I will adjust.
I would do this by creating a WebClient, downloading the data, converting it to a string, and then operating on it. I did not add any headers - dropbox doesn't require it, but something to keep in mind for production... Small example below:
Dim bResult() As Byte
Dim sUrl As String = "https://dl.dropboxusercontent.com/s/bus2q71wsn9txuz/Test.txt?dl=0"
Using client As New WebClient
bResult = client.DownloadData(sUrl)
End Using
Dim retData As String = Encoding.UTF8.GetString(bResult)
Dim retList As List(Of String) = retData.Split(Environment.NewLine).ToList()
Dim sMin = retList(0).Split("=").Last()
Dim sNew = retList(1).Split("=").Last()

Check numeric field lotusscript

I have a field on a form called "fin_Paiement". What I want to do is: the field value to accept only numbers and points and to replace any other character by a point. I did it for alphabetic values with replace funciton but it doesn't work.
I tried this :
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim db As NotesDatabase
Set uidoc = workspace.CurrentDocument
Set doc = uidoc.Document
doc.fin_Paiement = Replace(doc.fin_Paiement_Montant(0), "*[a-z,A-Z]*", ".")
I will appreciate your help! Thanks
Create a function which changes all non-digits to dots
Function ToDigitsAndDots(orig As String) As String
Dim i As Integer
Dim char As String
For i=1 To Len(orig)
char = Mid(orig, i, 1)
If Not char Like "#" Then char = "."
JustDigitsAndDots = JustDigitsAndDots & char
Next
End Function
and change your code line to
doc.fin_Paiement = ToDigitsAndDots(doc.fin_Paiement_Montant(0))
LotusScript's Replace method doesn't support regular expressions. You would have to make the second parameter an array of all letters, for example:
doc.fin_Paiement = Replace(
Cstr(doc.fin_Paiement_Montant(0)),
Split("a, b, c, d, e, f, g, h, i, j, you get the idea...", ", "),
"."
)
Note, I haven't tested this but in theory it should do the trick.
Please relieve me of an awful doubt : your field is of type "Number", isn't it ? In which case, no code is needed, it accepts only numbers, and the decimal separator is specified in its properties. By default, the locale settings for a field are those of the Notes client, which, by default, are those of the operating system, so normally not something you should worry about.
Anyway, this is the kind of thing you typically don't do in LotusScript !
Most straightforward is to use #functions in one of the field's events.
Check your Designer Help, here is some hint , and you may want to review this.

Resources