Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 559785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:16:28+00:00 2026-05-13T12:16:28+00:00

I have a string with multiple sentences. How do I Capitalize the first letter

  • 0

I have a string with multiple sentences. How do I Capitalize the first letter of first word in every sentence. Something like paragraph formatting in word.

eg .”this is some code. the code is in C#. ”
The ouput must be “This is some code. The code is in C#”.

one way would be to split the string based on ‘.’ and then capitalize the first letter and then rejoin.

Is there a better solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:16:29+00:00Added an answer on May 13, 2026 at 12:16 pm

    In my opinion, when it comes to potentially complex rules-based string matching and replacing – you can’t get much better than a Regex-based solution (despite the fact that they are so hard to read!). This offers the best performance and memory efficiency, in my opinion – you’ll be surprised at just how fast this’ll be.

    I’d use the Regex.Replace overload that accepts an input string, regex pattern and a MatchEvaluator delegate. A MatchEvaluator is a function that accepts a Match object as input and returns a string replacement.

    Here’s the code:

    public static string Capitalise(string input)
    {
      //now the first character
      return Regex.Replace(input, @"(?<=(^|[.;:])\s*)[a-z]",
        (match) => { return match.Value.ToUpper(); });
    }
    

    The regex uses the (?<=) construct (zero-width positive lookbehind) to restrict captures only to a-z characters preceded by the start of the string, or the punctuation marks you want. In the [.;:] bit you can add the extra ones you want (e.g. [.;:?."] to add ? and ” characters.

    This means, also, that your MatchEvaluator doesn’t have to do any unnecessary string joining (which you want to avoid for performance reasons).

    All the other stuff mentioned by one of the other answerers about using the RegexOptions.Compiled is also relevant from a performance point of view. The static Regex.Replace method does offer very similar performance benefits, though (there’s just an additional dictionary lookup).

    Like I say – I’ll be surprised if any of the other non-regex solutions here will work better and be as fast.

    EDIT

    Have put this solution up against Ahmad’s as he quite rightly pointed out that a look-around might be less efficient than doing it his way.

    Here’s the crude benchmark I did:

    public string LowerCaseLipsum
    {
      get
      {
        //went to lipsum.com and generated 10 paragraphs of lipsum
        //which I then initialised into the backing field with @"[lipsumtext]".ToLower()
        return _lowerCaseLipsum;
      }
     }
     [TestMethod]
     public void CapitaliseAhmadsWay()
     {
       List<string> results = new List<string>();
       DateTime start = DateTime.Now;
       Regex r = new Regex(@"(^|\p{P}\s+)(\w+)", RegexOptions.Compiled);
       for (int f = 0; f < 1000; f++)
       {
         results.Add(r.Replace(LowerCaseLipsum, m => m.Groups[1].Value
                          + m.Groups[2].Value.Substring(0, 1).ToUpper()
                               + m.Groups[2].Value.Substring(1)));
       }
       TimeSpan duration = DateTime.Now - start;
       Console.WriteLine("Operation took {0} seconds", duration.TotalSeconds);
     }
    
     [TestMethod]
     public void CapitaliseLookAroundWay()
     {
       List<string> results = new List<string>();
       DateTime start = DateTime.Now;
       Regex r = new Regex(@"(?<=(^|[.;:])\s*)[a-z]", RegexOptions.Compiled);
       for (int f = 0; f < 1000; f++)
       {
         results.Add(r.Replace(LowerCaseLipsum, m => m.Value.ToUpper()));
       }
       TimeSpan duration = DateTime.Now - start;
       Console.WriteLine("Operation took {0} seconds", duration.TotalSeconds);
     }
    

    In a release build, the my solution was about 12% faster than the Ahmad’s (1.48 seconds as opposed to 1.68 seconds).

    Interestingly, however, if it was done through the static Regex.Replace method, both were about 80% slower, and my solution was slower than Ahmad’s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.