Skip to main content
added 39 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
  • use a StringBuilder for building string dynamically

    Use a StringBuilder for building string dynamically

  • you can use a foreach loop for strings too

    You can use a foreach loop for strings too

      public static string SplitOnCapitalLetters2(this string inputString)
      {
          var result = new StringBuilder();
    
          foreach (var ch in inputString)
          {
              if (char.IsUpper(ch) && result.Length > 0)
              {
                  result.Append(' ');
              }
              result.Append(ch);
          }
          return result.ToString();
      }
    

x

public static string SplitOnCapitalLetters2(this string inputString)
{
    var result = new StringBuilder();

    foreach (var ch in inputString)
    {
        if (char.IsUpper(ch) && result.Length > 0)
        {
            result.Append(' ');
        }
        result.Append(ch);
    }
    return result.ToString();
}
  • use a StringBuilder for building string dynamically
  • you can use a foreach loop for strings too

x

public static string SplitOnCapitalLetters2(this string inputString)
{
    var result = new StringBuilder();

    foreach (var ch in inputString)
    {
        if (char.IsUpper(ch) && result.Length > 0)
        {
            result.Append(' ');
        }
        result.Append(ch);
    }
    return result.ToString();
}
  • Use a StringBuilder for building string dynamically

  • You can use a foreach loop for strings too

      public static string SplitOnCapitalLetters2(this string inputString)
      {
          var result = new StringBuilder();
    
          foreach (var ch in inputString)
          {
              if (char.IsUpper(ch) && result.Length > 0)
              {
                  result.Append(' ');
              }
              result.Append(ch);
          }
          return result.ToString();
      }
    
added 87 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

With regex it's virtually a one-liner:

var words = 
    Regex.Matches("SmallFireBall", @"([A-Z][a-z]+)")
    .Cast<Match>()
    .Select(m => m.Value);

var withSpaces = string.Join(" ", words);
  • Regex.Matches - searches an input string for all occurrences of a regular expression and returns all the matches. MSDN
  • [A-Z][a-z]+ - matches strings that begin with a capital letter and are followed by one or more lowercase letters

Output:

Small Fire Ball


Let's review your code anyway and optimize it a little bit. Even without regex it still can be very short.

  • use a StringBuilder for building string dynamically
  • you can use a foreach loop for strings too

x

public static string SplitOnCapitalLetters2(this string inputString)
{
    var result = new StringBuilder();

    foreach (var ch in inputString)
    {
        if (char.IsUpper(ch) && result.Length > 0)
        {
            result.Append(' ');
        }
        result.Append(ch);
    }
    return result.ToString();
}

The 3rd alternative would be only LINQ:

public static string SplitOnCapitalLetters3(this string inputString)
{
    // starts with an empty string and accumulates the new string into 'result'
    // 'next' is the next character
    return inputString.Aggregate(string.Empty, (result, next) =>
    {
        if (char.IsUpper(next) && result.Length > 0)
        {
            result += ' ';
        }
        return result + next;
    });
}

With regex it's virtually a one-liner:

var words = 
    Regex.Matches("SmallFireBall", @"([A-Z][a-z]+)")
    .Cast<Match>()
    .Select(m => m.Value);

var withSpaces = string.Join(" ", words);
  • Regex.Matches - searches an input string for all occurrences of a regular expression and returns all the matches. MSDN
  • [A-Z][a-z]+ - matches strings that begin with a capital letter and are followed by one or more lowercase letters

Output:

Small Fire Ball


Let's review your code anyway and optimize it a little bit. Even without regex it still can be very short.

  • use a StringBuilder for building string dynamically
  • you can use a foreach loop for strings too

x

public static string SplitOnCapitalLetters2(this string inputString)
{
    var result = new StringBuilder();

    foreach (var ch in inputString)
    {
        if (char.IsUpper(ch) && result.Length > 0)
        {
            result.Append(' ');
        }
        result.Append(ch);
    }
    return result.ToString();
}

The 3rd alternative would be only LINQ:

public static string SplitOnCapitalLetters3(this string inputString)
{
    return inputString.Aggregate(string.Empty, (result, next) =>
    {
        if (char.IsUpper(next) && result.Length > 0)
        {
            result += ' ';
        }
        return result + next;
    });
}

With regex it's virtually a one-liner:

var words = 
    Regex.Matches("SmallFireBall", @"([A-Z][a-z]+)")
    .Cast<Match>()
    .Select(m => m.Value);

var withSpaces = string.Join(" ", words);
  • Regex.Matches - searches an input string for all occurrences of a regular expression and returns all the matches. MSDN
  • [A-Z][a-z]+ - matches strings that begin with a capital letter and are followed by one or more lowercase letters

Output:

Small Fire Ball


Let's review your code anyway and optimize it a little bit. Even without regex it still can be very short.

  • use a StringBuilder for building string dynamically
  • you can use a foreach loop for strings too

x

public static string SplitOnCapitalLetters2(this string inputString)
{
    var result = new StringBuilder();

    foreach (var ch in inputString)
    {
        if (char.IsUpper(ch) && result.Length > 0)
        {
            result.Append(' ');
        }
        result.Append(ch);
    }
    return result.ToString();
}

The 3rd alternative would be only LINQ:

public static string SplitOnCapitalLetters3(this string inputString)
{
    // starts with an empty string and accumulates the new string into 'result'
    // 'next' is the next character
    return inputString.Aggregate(string.Empty, (result, next) =>
    {
        if (char.IsUpper(next) && result.Length > 0)
        {
            result += ' ';
        }
        return result + next;
    });
}
added 236 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

With regex it's virtually a one-liner:

var words = 
    Regex.Matches("SmallFireBall", @"([A-Z][a-z]+)")
    .Cast<Match>()
    .Select(m => m.Value);

var withSpaces = string.Join(" ", words);
  • Regex.Matches - searches an input string for all occurrences of a regular expression and returns all the matches. MSDN
  • [A-Z][a-z]+ - matches strings that begin with a capital letter and are followed by one or more lowercase letters

Output:

Small Fire Ball


Let's review your code anyway and optimize it a little bit. Even without regex it still can be very short.

  • use a StringBuilder for building string dynamically
  • you can use a foreach loop for strings too

x

public static string SplitOnCapitalLettersSplitOnCapitalLetters2(this string inputString)
{
    var result = new StringBuilder();
    
    foreach (var ch in inputString)
    {
        if (char.IsUpper(ch))
        {
            if&& (result.Length > 0)
            {
                result.Append(' ');
            }
        result.Append(ch);
    }
    return result.ToString();
}

The 3rd alternative would be only LINQ:

public static string SplitOnCapitalLetters3(this string inputString)
{
  result  return inputString.AppendAggregate(chstring.Empty, (result, next); =>
    {
    }
    if (char.IsUpper(next) && result.Length else> 0)
        {
            result.Append(ch);
  += ' ';
    }
    }
    
     return result.ToString(); + next;
    });
}

With regex it's virtually a one-liner:

var words = 
    Regex.Matches("SmallFireBall", @"([A-Z][a-z]+)")
    .Cast<Match>()
    .Select(m => m.Value);

var withSpaces = string.Join(" ", words);
  • Regex.Matches - searches an input string for all occurrences of a regular expression and returns all the matches. MSDN
  • [A-Z][a-z]+ - matches strings that begin with a capital letter and are followed by one or more lowercase letters

Output:

Small Fire Ball


Let's review your code anyway and optimize it a little bit. Even without regex it still can be very short.

  • use a StringBuilder for building string dynamically
  • you can use a foreach loop for strings too

x

public static string SplitOnCapitalLetters(string inputString)
{
    var result = new StringBuilder();
    
    foreach (var ch in inputString)
    {
        if (char.IsUpper(ch))
        {
            if (result.Length > 0)
            {
                result.Append(' ');
            }
            
            result.Append(ch);
        }
        else
        {
            result.Append(ch);
        }
    }
    
     return result.ToString();   
}

With regex it's virtually a one-liner:

var words = 
    Regex.Matches("SmallFireBall", @"([A-Z][a-z]+)")
    .Cast<Match>()
    .Select(m => m.Value);

var withSpaces = string.Join(" ", words);
  • Regex.Matches - searches an input string for all occurrences of a regular expression and returns all the matches. MSDN
  • [A-Z][a-z]+ - matches strings that begin with a capital letter and are followed by one or more lowercase letters

Output:

Small Fire Ball


Let's review your code anyway and optimize it a little bit. Even without regex it still can be very short.

  • use a StringBuilder for building string dynamically
  • you can use a foreach loop for strings too

x

public static string SplitOnCapitalLetters2(this string inputString)
{
    var result = new StringBuilder();

    foreach (var ch in inputString)
    {
        if (char.IsUpper(ch) && result.Length > 0)
        {
            result.Append(' ');
        }
        result.Append(ch);
    }
    return result.ToString();
}

The 3rd alternative would be only LINQ:

public static string SplitOnCapitalLetters3(this string inputString)
{
    return inputString.Aggregate(string.Empty, (result, next) =>
    {
        if (char.IsUpper(next) && result.Length > 0)
        {
            result += ' ';
        }
        return result + next;
    });
}
added 627 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
Loading
added 336 characters in body
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
Loading
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
Loading