C# Skipwhile to VB skipWhile - c#

I have code in C#
string fileNameOnly = Path.GetFileNameWithoutExtension(sKey);
string token = fileNameOnly.Remove(fileNameOnly.LastIndexOf('_'));
string number = new string(token.SkipWhile(Char.IsLetter).ToArray());
And i want it in VB
Dim fileNameOnly As String = Path.GetFileNameWithoutExtension(sKey)
Dim token As String = fileNameOnly.Remove(fileNameOnly.LastIndexOf("_"c))
Dim number As New String(token.SkipWhile([Char].IsLetter).ToArray())
I have tried that but did not work! Is there something similar to use.
What it does is look at a file name and only use the number part of it and skip all letters and all after _.

You have to use AddressOf in VB.NET:
Dim number As New String(token.SkipWhile(AddressOf Char.IsLetter).ToArray())
You could also use Function:
Dim number As New String(token.SkipWhile(Function(c)Char.IsLetter(c)).ToArray())
In VB.NET i often use multiple lines and combine query+method syntax to avoid the ugly Function/AddressOf keywords.
Dim numberChars = From c In token
Skip While Char.IsLetter(c)
Dim numbers = New String(numberChars.ToArray())

Related

Check if string contains ANY string from the list in Visual Basic

how to check if a string "I want to eat apples", contains a string from the list of strings?
Here's the code that I'm trying to use:
Dim CONTAINER As String() = {"eat", "dog", "cat"}
If STRING1.Text.Contains(CONTAINER(0..All?)) Then
Dim ioFileT As New System.IO.StreamReader("C:\strings\RANDOMWORD.txt")
Dim linesT As New List(Of String)
Dim rndT As New Random()
Dim lineT As Integer
Dim RANDOMWORDFROMTXTFILE As String
While ioFileT.Peek <> -1
linesT.Add(ioFileT.ReadLine())
End While
lineT = rndT.Next(linesT.Count + 0)
RANDOMWORDFROMTXTFILE = (lines(line).Trim())
Console.Write(RANDOMWORDFROMTXTFILE)
End If
If substringList.Any(Function(s) myString.Contains(s)) Then
'myString contains at least one item from substringList.
End If

ToString Format

I have an String like this "00.00-0-00" and I need to transform like this "0000-0/00"
I already tried:
field.ToString("####-##/##") ' without success
String.Format(CultureInfo.InvariantCulture, "{0:####-#/##}", field) 'without success
Any help with that?
My version which work if your original format doesn't change(as other answers based too)
Dim oldString As String = "00.00-0-00"
Dim parts As String() = oldString.Split({"."c, "-"c})
Dim result As String = _
String.Format("{0}{1}-{2}/{3}", parts(0), parts(1), parts(2), parts(3)))
C# version
string original = "00.00-00-00";
string[] parts = original.Split(new char[] {'.', '-'});
string result = String.Format("{0}{1}-{2}/{3}", parts[0], parts[1], parts[2], parts[3]));
Here is how I would do it (val is input string)
string.Format(#"{0}{1}-{2}/{3}",
val.Substring(0,2),
val.Substring(3,2),
val.Substring(6,1),
val.Substring(8,2));
If you don't like string.Format you could also use concatenation (+)
If your format is always "00.00-0-00", then you can use Regex.Replace() like this:
VB.NET
Dim data As String = "00.00-0-00"
Console.WriteLine(Regex.Replace(data, "(\d+)\.(\d+)-(\d+)-(\d+)", "$1$2-$3/$4"))
C#
string data = "00.00-0-00";
Console.WriteLine(Regex.Replace(data, "(\\d+)\\.(\\d+)-(\\d+)-(\\d+)", "$1$2-$3/$4"));
Results:
0000-0/00
The Regex is capturing all of the digits into groups and you just put the groups back together with whatever delimiter you want.
If you know that your string will always be in the format, you could just do it manually. If you need to do this a lot, you could just create a function to do it.
Dim oldString As String = "00.00-0-00"
Dim newString As String = MakeNewString(oldString)
Function to call:
Function MakeNewString(oldString As String) As String
Dim noPeriod As String = Replace(oldString, ".", "")
Return Split(noPeriod, "-")(0) & "-" & Split(noPeriod, "-")(1) & "/" & Split(noPeriod, "-")(2)
End Function
In C#:
string oldString = "00.00-0-00";
string newString = MakeNewString(oldString);
C# Function:
public string MakeNewString(string oldString)
{
string noPeriod = Strings.Replace(oldString, ".", "");
return Strings.Split(noPeriod, "-")(0) + "-" + Strings.Split(noPeriod, "-")(1) + "/" + Strings.Split(noPeriod, "-")(2);
}

How to get Nth number from the specific position of a number in a string - Regex

288007 327920 374740 000368 044575 082865 680798
717374 755879 811106 855460 920577 953515 996819 ......
I have a string containing thousands of 6-digit numbers and I want to extract the Nth numbers after Nth number with the help of regular expression.
Let say I need to extract Three numbers after the 4th number then The result should be 044575 082865 680798.
another example If I need to extract 2 numbers after the 10th number then the result should be 855460 920577.
I don't know is this possible with regex, I think FOR EACH statement may be use in my case.
I am only able to extract each six digits number with the code below.
Dim NumberMatchCollection As MatchCollection = Regex.Matches("String containing numbers", "(?<!\d)\d{6}(?!\d)")
For Each NumberMatch As Match In NumberMatchCollection
Dim ItemNumber As String = NumberMatch.Value
Next
Edited:
I can not guarantee that every separator character will be a single space, a double space, a tab or something else. I can just guarantee that the number length always will be 6 which will be separated by space(s) or tab(s).
Wouldn't this be simpler using maths?
Three numbers after the 4th number, is chars (7 * 4) + (7 * 3)
To expand on my comment. This assume that the actual data are divided equaly.
If each number have 6 digits with a space in between. Then the position of the 4th number will be (6+1)*4 and if you want 3 numbers than you just need to fetch (6+1)*3 amount of characters.
Dim str As String
str = "288007 327920 374740 000368 044575 082865 680798 717374 755879 811106 855460 920577 953515 996819"
Dim startingNumber As Integer = 4
Dim amountToFetch As Integer = 3
' 7 = [size of each number] + [delimiter length]
' 7 = 6 + 1
Console.WriteLine(str.Substring(7 * startingNumber, 7 * amountToFetch))
Console.ReadLine()
If you would like a regex and c# solution, the following code does the 3 numbers after 4th number example.
var st = #"288007 327920 374740 000368 044575 082865 680798
717374 755879 811106 855460 920577 953515 996819";
var pattern = #"^(\d+\s+){4}((?<x>\d+)\s+){3}";
var matches = Regex.Matches(st,pattern,RegexOptions.Singleline);
foreach (Capture m in matches[0].Groups["x"].Captures)
Console.WriteLine("value={0}", m.Value);
(Edit: removed one group per comment below)
You can .Split() the string and use LINQ extension methods on the resulting array:
// some test data...
var rand = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 10000; i++)
{
sb.Append(i.ToString("000000") + ((rand.Next(5)==1) ? " ": "\t"));
}
string s = sb.ToString();
string portion = string.Join(" ", s.Split(new [] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries).Skip(10).Take(3));
Console.WriteLine(portion); // outputs "000011 000012 000013"
Note: for the first number you would .Skip(0).
But if your string is in the rigid format you show (asuming the variable numbers of spaces are typos, thanks #ErikE), Coenraad's method of calculating where the start of the required string is and how many characters to take would be more efficient. I'll leave it to Coenraad to expand on that answer as it would not be fair to possibly take the points.
I tried and tried to make the regex method be consistently fast, but I found it depended strongly on which numbers you want to retrieve:
For anyone wanting to test that, I put a default Chart on a Form and used this code:
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Windows.Forms.DataVisualization
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Sub DoStuff()
Dim ser1 As New Series With {.Name = "String.Split"}
Dim ser2 As New Series With {.Name = "RegEx"}
Dim sb As New StringBuilder()
For i As Integer = 1 To 10000
sb.Append(i.ToString("000000") + " ")
Next
Dim s As String = sb.ToString()
Dim sw As New Stopwatch()
Dim itemsToTake As Integer = 50
For firstItem = 1 To 9000 Step 100
sw.Restart()
Dim portion As String = String.Join(" ", s.Split({" "c}, StringSplitOptions.RemoveEmptyEntries).Skip(firstItem - 1).Take(itemsToTake))
sw.Stop()
ser1.Points.AddXY(firstItem -1, sw.ElapsedTicks)
Dim pattern = "^(?:\d+\s+){" + (firstItem - 1).ToString() + "}((\d+)\s+){" + itemsToTake.ToString() + "}"
Dim re = New Regex(pattern)
sw.Restart()
Dim matches = re.Matches(s)
Dim cs = matches(0).Groups(0).Captures
sw.Stop()
ser2.Points.AddXY(firstItem - 1, sw.ElapsedTicks)
Next
Chart1.Series.Clear()
Chart1.Series.Add(ser1)
Chart1.Series(0).ChartType = SeriesChartType.Line
Chart1.Series.Add(ser2)
Chart1.Series(1).ChartType = SeriesChartType.Line
Chart1.ChartAreas(0).AxisX.IsMarginVisible = False
Chart1.ChartAreas(0).AxisX.Title = "First item to retrieve"
Chart1.ChartAreas(0).AxisY.Title = "Time taken"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DoStuff()
End Sub
End Class

how to extract string from certain position

I am struggling to find a solution in string manipulation - I am trying to extract a certain part of the string element after the '=' character - say for ex.
dim s as string = "/mysite/secondary.aspx?id=1005"
I am trying to get the string after the "=" and just to grab the 1005. I tried indexof and split, but i am not sure where i am going wrong. Any help, please?
Here is what i did:
Dim lnk As String = "/mysite/secondary.aspx?id=1005"
Dim id As Long = lnk.IndexOf("=")
Dim part As String = lnk.Substring(id + 1, 4)
Thanks
Try the following
Dim index = s.IndexOf("="C)
Dim value = s.Substring(index + 1)
This will put "1005" into value
Dim tUriPath As String = "/mysite/secondary.aspx?id=1005"
Dim tURI As Uri = New Uri("dummy://example.com" & tUriPath)
Dim tIdValue As String = System.Web.HttpUtility.ParseQueryString(tUri.Query)("id")
Here's a very simple example. Obviously it relies on very specific conditions:
Dim afterEquals As String = s.Split("="c)(1)
You would probably want something slightly more robust (checking to make sure more than one string was returned from Split, etc.).
If you try string.Split with '=' you'll get 1005 on the first element of the array and the /mysite/secondary.aspx?id= on the 0th position.
But if this is just a regular URL coming from an http request.
You could possibly do Request.QueryString("id") and it will return 1005;
Borrowing code from Boo...
Dim tUriPath As String = "/mysite/secondary.aspx?id=1005"
Dim tURI As Uri = New Uri("dummy://example.com" & tUriPath)
Dim tIdValue As String = System.Web.HttpUtility.ParseQueryString(tUri.Query)
Dim theIntYouWant as String= System.Web.HttpUtility.ParseQueryString(tUri.Query)("id")

Regex: absolute url to relative url (C#)

I need a regex to run against strings like the one below that will convert absolute paths to relative paths under certain conditions.
<p>This website is <strong>really great</strong> and people love it <img alt="" src="http://localhost:1379/Content/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif" /></p>
Rules:
If the url contains "/Content/" I
would like to get the relative path
If the url does not contain
"/Content/", it is an external file,
and the absolute path should remain
Regex unfortunatley is not my forte, and this is too advanced for me at this point. If anyone can offer some tips I'd appreciate it.
Thanks in advance.
UPDATE:
To answer questions in the comments:
At the time the Regex is applied, All urls will begin with "http://"
This should be applied to the src attribute of both img and a tags, not to text outside of tags.
You should consider using the Uri.MakeRelativeUri method - your current algorithm depends on external files never containing "/Content/" in their path, which seems risky to me. MakeRelativeUri will determine whether a relative path can be made from the current Uri to the src or href regardless of changes you or the external file store make down the road.
Unless I'm missing the point here, if you replace
^(.*)([C|c]ontent.*)
With
/$2
You will end up with
/Content/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif
This will only happen id "content" is found, so in cae you have a URL such as:
http://localhost:1379/js/fckeditor/editor/images/smiley/msn/teeth_smile.gif
Nothing will be replaced
Hope it helps, and that i didn't miss anything.
UPDATE
Obviously considering you are using an HTML parser to find the URL inside the a href (which you should in case you're not :-))
Cheers
That is for perl, I do not know c#:
s#(<(img|a)\s[^>]*?\s(src|href)=)(["'])http://[^'"]*?(/Content/[^'"]*?)\4#$1$4$5#g
If c# has perl-like regex it will be easy to port.
This function can convert all the hyperlinks and image sources inside your HTML to absolute URLs and for sure you can modify it also for CSS files and Javascript files easily:
Private Function ConvertALLrelativeLinksToAbsoluteUri(ByVal html As String, ByVal PageURL As String)
Dim result As String = Nothing
' Getting all Href
Dim opt As New RegexOptions
Dim XpHref As New Regex("(href="".*?"")", RegexOptions.IgnoreCase)
Dim i As Integer
Dim NewSTR As String = html
For i = 0 To XpHref.Matches(html).Count - 1
Application.DoEvents()
Dim Oldurl As String = Nothing
Dim OldHREF As String = Nothing
Dim MainURL As New Uri(PageURL)
OldHREF = XpHref.Matches(html).Item(i).Value
Oldurl = OldHREF.Replace("href=", "").Replace("HREF=", "").Replace("""", "")
Dim NEWURL As New Uri(MainURL, Oldurl)
Dim NewHREF As String = "href=""" & NEWURL.AbsoluteUri & """"
NewSTR = NewSTR.Replace(OldHREF, NewHREF)
Next
html = NewSTR
Dim XpSRC As New Regex("(src="".*?"")", RegexOptions.IgnoreCase)
For i = 0 To XpSRC.Matches(html).Count - 1
Application.DoEvents()
Dim Oldurl As String = Nothing
Dim OldHREF As String = Nothing
Dim MainURL As New Uri(PageURL)
OldHREF = XpSRC.Matches(html).Item(i).Value
Oldurl = OldHREF.Replace("src=", "").Replace("src=", "").Replace("""", "")
Dim NEWURL As New Uri(MainURL, Oldurl)
Dim NewHREF As String = "src=""" & NEWURL.AbsoluteUri & """"
NewSTR = NewSTR.Replace(OldHREF, NewHREF)
Next
Return NewSTR
End Function

Categories