Ignore decimal numbers/values while using Split() in C#/Java


Suppose you have a string you wanted to split using the period character. However, this particular string has a decimal value or number in it. For example: “Windows Phone 8.1 now has Cortana, a Siri-like feature for the platform.”

If we were to split like this: string.Split(‘.’); this would also split at the decimal number, resulting in an incomplete sentence. In C# and Java, there exists a special pattern to ignore decimal numbers in strings using Regex (regular expressions).

I asked a question on this topic here for more information.
 
In C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions; //Needed to use Regex!

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Windows Phone 8.1 now has 'Cortana', a Siri-like feature for the platform.";
            Regex reg = new Regex("(?<!\\d)\\.(?!\\d)|(?<=\\d)\\.(?!\\d)|(?<!\\d)\\.(?=\\d)");

            string[] output = reg.Split(input);
            Console.WriteLine(output[0]);
            Console.ReadLine();
        }
    }
}

In Java

//be sure to import "java.util.*;" or else Regex will not work!
String input = "Windows Phone 8.1 now has 'Cortana', a Siri-like feature for the platform.";
String pattern = "(?<!\\d)\\.(?!\\d)|(?<=\\d)\\.(?!\\d)|(?<!\\d)\\.(?=\\d)";
Pattern pt = Pattern.compile(pattern);
String[] sentences = pt.split(input);
System.out.println(sentences[0]);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s