How to get the VideoID of a youtube video using Regex

To get the VideoID of a youtube video using Regex you can use the following snippet.

Sample Regex

^(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.[a-zA-Z]{1,3}\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\”‘>]+)

This will match the Vl1tVxfFf1A in https://www.youtube.com/watch?v=Vl1tVxfFf1A.

A more complex regex matching more youtube link variants can be found How to get the Youtube VideoId from Url in C# and VB.NET

Please rate this snippet

How to open a pdf at a specific page using AcroRd32Info in C# and VB.NET

To open a pdf at a specific page using AcroRd32Info in C# and VB.NET you can use the following snippet.

Sample C#

public static void OpenPdfAtPage(string document, int page) 
{
	object myProcess = new Process();
	object startInfo = new ProcessStartInfo();
	startInfo.FileName = "AcroRd32Info.exe";
	startInfo.Arguments = string.Format("/A \"page={0}\"{1}", page, document);
	myProcess.StartInfo = startInfo;
	myProcess.Start();
}

Sample VB.NET

Public Shared Sub OpenPdfAtPage(document As String, page As Integer)
	Dim myProcess = New Process()
	Dim startInfo = New ProcessStartInfo()
	startInfo.FileName = "AcroRd32Info.exe"
	startInfo.Arguments = String.Format("/A ""page={0}""{1}", page, document)
	myProcess.StartInfo = startInfo
	myProcess.Start()
End Sub

Please rate this snippet

How to rename a file in c# or vb.net

To rename a file in c# or vb.net you need to Move it, there is no inbuild function that would just rename a file without moving.
The Move function can not overwrite a file and will throw a IOException if the file already exists, therefore we try to delete it first if it exists.

Sample C#

const string oldName = @"C:\OldFilename.txt";
const string newName = @"C:\NewFilename.txt";
File.Delete(newName); // First delete the new file if it exists, because it wont be overwritten by Move
File.Move(oldName, newName); // Move the file and give it a new name

Sample VB.NET

Const  oldName As String = "C:\OldFilename.txt"
Const  newName As String = "C:\NewFilename.txt"
File.Delete(newName) ' First delete the new file if it exists, because it wont be overwritten by Move
File.Move(oldName, newName) ' Move the file and give it a new name

Fore more informations see File.Move Method, File.Delete Method,IOException Class

Please rate this snippet

How to replace multiple spaces with a single space in c# or vb.net

To replace multiple spaces with a single space in c# or vb.net you can use a Regular Expression

Sample c#

var inputString = "This  is    a Text with    too much     spaces   in   it!!!";
inputString = Regex.Replace(inputString, @"[ ]{2,}", @" ", RegexOptions.None);

Sample VB.NET

Dim inputString = "This  is    a Text with    too much     spaces   in   it!!!"
inputString = Regex.Replace(inputString, "[ ]{2,}", " ", RegexOptions.None)

there are more ways to do that, but i personally prefer the regex way.
A other way to archieve that would be to use a loop. The snippet below is replacing double spaces until there are no double spaces left.

 

Sample c#

var inputString = "This  is    a Text with    too much     spaces   in   it!!!";
while (inputString.Contains(@"  "))
{
    inputString = inputString.Replace(@"  ", @" ");
}

Sample VB.NET

Dim inputString = "This  is    a Text with    too much     spaces   in   it!!!"
While inputString.Contains("  ")
	inputString = inputString.Replace("  ", " ")
End While

playing with the RegexOptions can speed up or slow down the regex snippet a bit, see RegexOptions Enumeration in the MSDN

Please rate this snippet

How to replace a line break in C# and VB.NET

Replacing a line break is a simple challenge. In .NET there are plenty of ways to do that.
You can for example use String.Replace or Regex.Replace. My prefered method is String.Replace but both versions will do it.

String.Replace Method for c#

sampleString = sampleString.Replace(System.Environment.NewLine, "replacement string");

String.Replace Method for vb.net

sampleString = sampleString.Replace(System.Environment.NewLine, "replacement string")

Regex.Replace Method for c#

sampleString = Regex.Replace(sampleString, @"\r\n?|\n", "replacement string");

Regex.Replace Method for c#

sampleString = Regex.Replace(sampleString, "\r\n?|\n", "replacement string")

more informations can be found in the msdn Regex.Replace, String.Replace

Please rate this snippet