28 Apr 2011

Version 1.0.2.1019

DataVoila News Comments Off

I’m pleased to announce the release of DataVoila Version 1.0.2.1019. This update is free for all registered users of the previous 1.x versions. To upgrade, just download and install the setup file from here.

25 Mar 2011

Working with Strings in DataVoila/C#

Tips and Tricks Comments Off

In this post, I have compiled a list of code examples for the most common string operations in C#.

Create a String

// Assign "Hello World" to a new variable
string helloWorld = "Hello World";
 
// Backslashes (and other special characters) must be escaped
string filePath1 = "C:\\Folder\\File1.txt";
 
// Instead, you can also use a verbatim string literal that is
// prefixed with @ and does not support escape sequences
string filePath2 = @"C:\Folder\File2.txt";

Concatenate Strings

string hello = "Hello";
string world = "World";
 
string helloWorld = hello + " " + world;
Console.WriteLine(helloWorld);  // "Hello World"

Check if a String Starts or Ends With a SubString

string sample = "DataVoila rocks!";
 
if (sample.StartsWith("DataVoila"))
    Console.WriteLine("String starts with substring.");
 
if (sample.EndsWith("!"))
    Console.WriteLine("String ends with substring.");
 
if (sample.StartsWith("DATAVOILA", StringComparison.InvariantCultureIgnoreCase))
    Console.WriteLine("String starts with substring, ignoring case.");

Search a String

string sample = "DataVoila rocks!";
string searchString = "rock";
 
if (sample.Contains(searchString))
{
    Console.WriteLine("Search string found at index {0}.",
        sample.IndexOf(searchString));
}
else
{
    Console.WriteLine("Search string NOT found.");
}

Extract a Substring From a String

string sample = "The quick brown fox jumps over the lazy dog";
 
Console.WriteLine(sample.Substring(0, 3));  // "The"
Console.WriteLine(sample.Substring(35));  // "lazy dog"

Insert a Substring to a String

string sample = "Hello World";
 
Console.WriteLine(sample.Insert(5, ", Lovely"));  // "Hello, Lovely World"

Remove a Substring From a String

string sample = "Hello, Lovely World";
 
Console.WriteLine(sample.Remove(5, 8));  // "Hello World"

Replace a Substring in a String

string sample = "Hello, Lovely World";
 
Console.WriteLine(sample.Replace("Lovely", "Cruel"));  // "Hello, Cruel World"

Split a String

string sample = "One,Two,,Three";
char[] delimiter = { ',' };
 
string[] parts;
parts = sample.Split(delimiter);  // "One", "Two", "", "Three"
//parts = sample.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);  // "One", "Two", "Three"
//parts = sample.Split(delimiter, 2);  // "One", "Two,,Three"
 
foreach (string part in parts)
{
    Console.WriteLine(part);
}

Join Strings

string[] parts = new String[] { "One", "Two", "Three" };
 
Console.WriteLine(string.Join("|", parts));  // "One|Two|Three"

Convert the Case of a String

string sample = "Hello World";
 
Console.WriteLine(sample.ToLower());  // "hello world";
Console.WriteLine(sample.ToUpper());  // "HELLO WORLD";

Convert a String to TitleCase

string sample = "hello, lovely world";
 
System.Globalization.TextInfo textInfo =
    System.Globalization.CultureInfo.CurrentCulture.TextInfo;
Console.WriteLine(textInfo.ToTitleCase(sample));  // Hello, Lovely World

Trim a String

string sample = "  Hello World!  ";
 
Console.WriteLine(sample.Trim());  // "Hello World!"
Console.WriteLine(sample.TrimStart());  // "Hello World!  "
Console.WriteLine(sample.TrimEnd());  // "  Hello World!"
Console.WriteLine(sample.Trim(new char[] { ' ', '!' }));  // "Hello World"

Pad a String

string sample = "42";
 
Console.WriteLine(sample.PadLeft(5, '0'));  // "00042"
Console.WriteLine(sample.PadRight(4, '.'));  // "42.."

Convert a String to a Numeric Value

string sample1 = "42";
string sample2 = "3.14159";
 
// Use culture-independent NumberFormatInfo to ensure that "." is recognized
// as decimal separator regardless of current locale settings
IFormatProvider formatProvider = System.Globalization.NumberFormatInfo.InvariantInfo;
 
int number1 = Convert.ToInt32(sample1);
double number2 = Convert.ToDouble(sample2, formatProvider);
 
Console.WriteLine("{0} converts to integer value {1}.", sample1, number1);
Console.WriteLine("{0} converts to double value {1}.", sample2, number2);

Iterate Through a String

string sample = "Hello World";
 
// Use a "for" loop to iterate through the string
for (int i = 0; i < sample.Length; i++)
{
    Console.WriteLine(sample[i]);
}
 
// Or make a "foreach" loop through the characters
foreach (char c in sample)
{
    Console.WriteLine(c);
}

Fill a String With Repeating Characters

string sample = new string('*', 10);
 
Console.WriteLine(sample);  // "**********"

Reverse a String

string sample = "Hello World";
 
char[] charArray = sample.ToCharArray();
Array.Reverse(charArray);
string reversedSample = new string(charArray);
 
Console.WriteLine(reversedSample);  // "dlroW olleH"

Append a Newline Character

string sample = "Hello" + Environment.NewLine + "World";
 
Console.WriteLine(sample);

Check If a String is Blank

string sample = "";
 
if (string.IsNullOrEmpty(sample))
    Console.WriteLine("String is null or empty.");

Check If a String Contains a Numeric Value

string sample = "3.14159";
 
double number;
bool isNumber = Double.TryParse(sample,
    System.Globalization.NumberStyles.Any,
    System.Globalization.NumberFormatInfo.InvariantInfo, out number);   
 
if (isNumber)
    Console.WriteLine("'{0}' can be converted to {1}.", sample, number);
else
    Console.WriteLine("'{0}' cannot be converted to a Double value.", sample);

Check If a String Contains Only Alphanumeric Characters

string sample = "Hello123";
 
bool isAlphaNum =
    !string.IsNullOrEmpty(sample) &&
    (sample.ToCharArray().All(c => Char.IsLetter(c) || Char.IsNumber(c)));
 
if (isAlphaNum)
    Console.WriteLine("String contains only alphanumeric characters.");
else
    Console.WriteLine("String contains non-alphanumeric characters.");

Use StringBuilder For Fast String Concatenation

StringBuilder sb = new StringBuilder();
 
for (int i = 0; i < 100000; i++)
{
    sb.Append(i.ToString());
}
 
string sample = sb.ToString();
Console.WriteLine("String of length {0} created.", sample.Length);
02 Feb 2011

IntelliSense for .NET Classes

Tips and Tricks Comments Off

DataVoila’s IntelliSense feature helps you write code by showing you the available classes, the methods, and the properties available on those classes. The included descriptions are especially useful to find out what a function or parameter is for and how it should be used.

IntelliSense with SDK

For DataVoila to display these descriptions for .NET Framework classes (as in the screenshot above), the IntelliSense XML files as part the “Microsoft Windows SDK” must be installed on your computer. Without the IntelliSense files, DataVoila is still able to provide parameter info but doesn’t display any descriptions.

IntelliSense without SDK

If you have Visual Studio 2008 or 2010 on your PC, the necessary files should already be there. If not, you can easily install the SDK via the links provided below. Depending on the version of the .NET Framework you are running, you should install the matching SDK. If you don’t know which version is installed, just start with the SDK for version 4. The setup will let you know the status of the necessary framework.

Don’t get sidetracked by the SDK titles that say “for Windows Server 2008″ or “for Windows 7.” The SDKs should work with any Windows version starting with Windows XP.

DataVoila needs only the IntelliSense XML files from the SDK. To make sure these files are installed, check the appropriate option in the setup wizard:

  • SDK for .NET Framework 4: “IntelliSense and Reference Assemblies”
  • SDK for .NET Framework 3.5 (SP1): “.NET Development Tools”

IntelliSense SDK Setup

After completing the setup wizard, be sure to restart DataVoila to get access to the IntelliSense comments.

03 Jan 2011

Version 1.0.1.1017 Released

DataVoila News Comments Off

Today we released version 1.0.1.1017 of DataVoila. The full list of changes is available here. To upgrade your copy of DataVoila, please download and install the setup package from http://www.datavoila.com/download/datavoila-setup.exe.

The update is a free maintenance release for all licensed users of previous 1.x versions.

16 Dec 2010

Fun Project: Buzzword Bingo Card Generator

Miscellaneous Comments Off

Our scripting tool DataVoila is a great little helper for serious business stuff like extracting information from log files or converting CSV documents into XML format. But did you know DataVoila can be fun?

Here’s how. I have created a small script project that takes a list of buzzwords as input and uses them to generate randomly populated Bingo cards in SVG format like this one.

Buzzword Bingo Card Preview

While DataVoila’s “Input” editor contains the buzzwords to show on the Bingo cards, the “Output” editor holds the template SVG code.

Buzzword Bingo Input-Output

Included in the SVG code is a placeholder comment <!– Placeholder –> that gets replaced by SVG <text> elements when generating the output files.

In DataVoila’s script editor, you can easily configure the output directory to which the generated files are saved. It’s also possible to set the number of Bingo cards to create.

Buzzword Bingo Code 1

By measuring the width of each buzzword and adjusting the font size accordingly, the script makes sure that the text fits into a square’s bounds. Bingo!

If you have Inkscape installed, you can optionally uncomment the code lines that make DataVoila run inkscape.exe to convert a generated SVG file into a PDF document like this one.

Buzzword Bingo Code 2

Download

The project file can be downloaded here. I have also added it to DataVoila’s project library. Have fun!

04 Nov 2010

DataVoila Version 1.0 Released

DataVoila News Comments Off

After successfully completing the beta test phase, we are very pleased to announce the final release version of DataVoila 1.0, an easy-to-use scripting tool for automating all kinds of data manipulation.

We hope you enjoy using DataVoila as much as we’ve enjoyed creating it! If you have any comments or questions, we’d love to hear them.

To download your copy of DataVoila 1.0, please click here.