Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrates how to count characters in .NET for Twitter status updates
using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace TwitterWordCount
{
class Program
{
static void Main()
{
const string sample = "There are two links in this text, one of which is secure. http://www.bn.com https://www.dougv.com/2015/08/posting-status-updates-to-twitter-via-linqtotwitter-part-2-plain-text-tweets";
var sampleResult = GetTwitterLength(sample);
Console.WriteLine("The tweet length of the sample string is {0}", sampleResult);
Console.ReadLine();
}
private static string CleanForTwitter(string input) {
return Regex.Replace(input.Trim(), @"\s+", " ");
}
private static int GetTwitterLength(string input)
{
//remove leading and extra whitespace
var tmp = CleanForTwitter(input);
//normalize, and we'll be dealing with codepoint counts
var info = new StringInfo(input.Normalize(NormalizationForm.FormC));
var count = info.LengthInTextElements;
//most links should follow this pattern
var regex = new Regex(@"(http)?(s?)(://)?[a-zA-Z0-9\-]+\.+[a-z]{2,13}[\.\?=&%/\w-#]*");
var matches = regex.Matches(input); //there are 2 regex matches in the sample
foreach (Match match in matches)
{
//subtract link length
count -= match.Length;
//then add 22 for each url found, representing its t.co shortened length
count += 22;
//add one more char if original URL was https
if (string.IsNullOrWhiteSpace(match.Groups[2].Value.Trim())) continue;
count += 1;
}
return count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.