Monday, 7 October 2019

Split a string by capital letters in asp.net c#

/////////////// c#

///////// Method 1.

public string String_SplitWithUpperCase(string Str)
        {

            var r = new System.Text.RegularExpressions.Regex(@"
                (?<=[A-Z])(?=[A-Z][a-z]) |
                 (?<=[^A-Z])(?=[A-Z]) |
                 (?<=[A-Za-z])(?=[^A-Za-z])", System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);

            return r.Replace(Str, " ");
        }


///////// Method 2.

public string String_SplitWithUpperCase(string Str)
        {
var r =System.Text.RegularExpressions.Regex.Matches(Str, @"([A-Z][a-z]+)")
                      .Cast<System.Text.RegularExpressions.Match>()
                      .Select(m => m.Value);


            string result = string.Join(" ", r);
        }

///////// Method 3.

public string String_SplitWithUpperCase(string Str)
        {
var r = new System.Text.StringBuilder();
            foreach (var ch in Str)
            {
                if (char.IsUpper(ch) && r.Length > 0)
                {
                    r.Append(' ');
                }
                r.Append(ch);
            }
            return r.ToString();

        }

/////////////// Call function

     public void getstring()
        {

          string result=  String_SplitWithUpperCase("ILoveMyIndia");
        }



///////////////////// RESULT

I Love My India

No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...