Arijeet Acharyya
Arijeet Acharyya

Reputation: 1

Facing problems performing string operations in C#

I would like to let everyone know that I am totally new to C# and Object oriented programming.

Also, can anyone please show me another way to solve the question?

The question which I tried to execute is this :

Class Formatter

Formatter()

Formatting of strings is done in the constructor.

CapitalizeLetter(this string)

This method capitalizes the letters entered by the user according to the conditions given. The first letter of the passed string should be capitalized. If the string contains any space or full stop, the next letter should also be capitalized. All the other letters should be in lower cases.

For example: ajaY malik. k

Output : Ajay Malik. K The code I tried doing in Formatter class as specified:

public static class Formatter
{
    static Formatter()
    {

    }

    public static string CapitalizeLetter(this string value)
    {
        string output = null;
        string[] splittedProduct = value.Split(' ','.');
        foreach (String temp in splittedProduct)
        {
            output = output + " " + temp[0].ToString().ToUpper() + temp.Substring(1).ToLower();
        }
        output = output.Trim();
        return output;
    }

    public static string UrlEncode(this string input)
    {
        return input.Replace(" ", "%20");
    }
}

My output is just changing the first letter in the string only.

My string is this : Interviewer is very nice.technically strong also.

Upvotes: 0

Views: 98

Answers (3)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

If you are studing strings and thus out of the box ToTitleCase is not an option, be careful with string + string.

Concatenating strings is time consuming (especially in loop): each + creates a new string. Usually we use StrignBuilder for string building. So the Title Case implementation can be

public static string CapitalizeLetter(this string value) {
  // Do not forget to validate public methods' input
  if (string.IsNullOrEmpty(value))
    return value;

  // We know the size of string we want to build - value.Length 
  StringBuilder sb = new StringBuilder(value.Length);

  bool toUpper = true;

  foreach (var c in value) {
    char add = c;

    if (char.IsWhiteSpace(c) || c == '.')
      toUpper = true;
    else {
      if (char.IsLetter(c))
        add = toUpper ? char.ToUpper(c) : char.ToLower(c);

      toUpper = false;
    }

    sb.Append(add);
  }

  return sb.ToString();
}

Upvotes: -1

BugFinder
BugFinder

Reputation: 17858

You are reinventing the wheel.

MS have already explained how

The link above shows how to use TextInfo and get the culture appropriate capitalized string on every word.. this is known as title case by this document.

In short it uses CultureInfo, TextInfo, and then uses the textinfo class to produce

"this is a test" to "This Is A Test"

Upvotes: 2

Gianmarco Varriale
Gianmarco Varriale

Reputation: 163

Capitalizes the first letter of each word in the string.

using System.Globalization;
string capitalized = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(“capitalizing the first letter of  some text”);

Here The CultureInfo class is from the System.Globalization namespace. From this class, you can get information about pretty much every possible culture out there, including a wide range of culture specific settings.

After the execution, capitalized string would have this value : “Capitalizing The First Letter Of Some Text” . which is exactly what we needed, right?

Suppose we want to set the current culture as of United States then,

TextInfo UsaTextInfo = new CultureInfo(“en-US”, false).TextInfo;
string capitalized = UsaTextInfo.ToTitleCase(“capitalizing the first letter of  some text”);

Upvotes: 1

Related Questions