Is it possible to uppercase the first character of each word using regex?
I'm going to be using this in VB.net (SSIS)
Is it possible to uppercase the first character of each word using regex?
I'm going to be using this in VB.net (SSIS)
You could do that, however this is a pretty common function in most programming languages. For example, the function is ucwords($word) in PHP.
Take a look at Capitalize First Letter of Each Word and at C# Uppercase Words in String, I posted in two languages, Perl and C#, because you haven't specified the language that you're using.
Not in "pure" regex, but most platform-specific implementations have a way to do it:
For example, in python:
import re
re.compile(r'\b\w').sub(lambda x: x.group(0).upper(), 'hello')
In this case we pass a callable lambda to the sub() method (rather than a replacement string) that will return the matching string upper cased. Most languages have some equivalent where you pass a callable as the 'replacement'.
In VB.NET you can pass your 'replacement' lambda as Function (x) x.Value(0).ToString().ToUpper()
No , but you could user a simple script, it depends on what language are you using (or is it shell?)
EDIT: VB.NET code added below
Dim input As String = "The quick brown fox jumps over the lazy dog"
Dim pattern As String = "\b(\w|['-])+\b"
' With lambda support:
Dim result As String = Regex.Replace(input, pattern, _
Function (m) m.Value(0).ToString().ToUpper() & m.Value.Substring(1))
If you can't use lambdas then use a MatchEvaluator instead:
Dim evaluator As MatchEvaluator = AddressOf TitleCase
Dim result As String = Regex.Replace(input, pattern, evaluator)
Public Function TitleCase(ByVal m As Match) As String
Return m.Value(0).ToString().ToUpper() & m.Value.Substring(1)
End Function
It's not really Title case in the sense of the MS Word formatting, but close enough.
string input = "The quick brown fox jumps over the lazy dog";
string pattern = @"\b(\w|['-])+\b";
string result = Regex.Replace(input, pattern,
m => m.Value[0].ToString().ToUpper() + m.Value.Substring(1));
This nicely handles one letter words, as Substring won't throw an error on something such as "A" in the input.
.NET has builtin support for this. See TextInfo.ToTitleCase for documentation.
My code contains some extension methods for C#. I assume VB.NET has those too, but I do not know VB.NET well enough to convert them myself.
public static class StringExtensions {
public static string ToTitleCase(this string value) {
return value.ToTitleCase(CultureInfo.InvariantCulture);
}
public static string ToTitleCase(this string value, CultureInfo culture) {
return value.ToTitleCase(culture.TextInfo);
}
public static string ToTitleCase(this string value, TextInfo textInfo) {
return textInfo.ToTitleCase(value);
}
}
Why not just use the inbuilt TextInfo.ToTitleCase() method already in the .NET Framework?
string capitalized = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase("this string should be capitalized!");