Sample code:
string input = "i have the car which is very fast";
int minLength = 2;
string regexPattern = string.Format(@"^w|w(?=w{{{0}}})", minLength);
string output = Regex.Replace(input, regexPattern, m => m.Value.ToUpperInvariant());
UPDATE (for the cases where you have multiple sentences in single string).
string input = "i have the car which is very fast. me is slow.";
int minLength = 2;
string regexPattern = string.Format(@"(?<=(^|.)s*)w|w(?=w{{{0}}})", minLength);
string output = Regex.Replace(input, regexPattern, m => m.Value.ToUpperInvariant());
Output:
I Have The Car Which is Very Fast. Me is Slow.
You may wish to handle !
, ?
and other symbols, then you can use the following. You can add as many sentence terminating symbols as you wish.
string input = "i have the car which is very fast! me is slow.";
int minLength = 2;
string regexPattern = string.Format(@"(?<=(^|[.!?])s*)w|w(?=w{{{0}}})", minLength);
string output = Regex.Replace(input, regexPattern, m => m.Value.ToUpperInvariant());
UPDATE (2) - convert e-marketing
to E-Marketing
(consider -
as valid word symbol):
string input = "i have the car which is very fast! me is slow. it is very nice to learn e-marketing these days.";
int minLength = 2;
string regexPattern = string.Format(@"(?<=(^|[.!?])s*)w|w(?=[-w]{{{0}}})", minLength);
string output = Regex.Replace(input, regexPattern, m => m.Value.ToUpperInvariant());
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…