Ave
Ave

Reputation: 4430

How to add char[item] to string?

I tried with List and array Char[] but can't add this. In this case, I tried appends to result variable.

I want to return string result:

char[] array = valuesNew.ToCharArray();
string result = "";
for (int i = 1; i < array.Length; i++)
{
    if (array[i - 1] == ' ')
    {
            array[i] = char.ToUpper(array[i]);
            result += array[i];
    }
}
return new string(result);

But error like:

Cannot convert a string to char.

at line:

return new string(result);

Upvotes: 1

Views: 1183

Answers (5)

Andrew
Andrew

Reputation: 7880

If you just need the first letter of each word (using space as separator), do something like this:

private static string FirstLetters(string text)
{
    string[] words = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    return new string(words.Select(x => Char.ToUpper(x[0])).ToArray());
}

static void Main(string[] args)
{
    Console.WriteLine(FirstLetters("This is a sentence.")); // Prints "TIAS"
    Console.WriteLine(FirstLetters("To dO someThing in HomEWork")); // Prints "TDSIH"
    Console.ReadKey();
}

If for some reason you prefer not to use Linq, you can do this (doesn't work fine if there are multiple spaces next to each other):

private static string FirstLetters(string text)
{
    string result = string.Empty;
    for (int i = 0; i < text.Length - 1; i++)
    {
        if (text[i] == ' ')
            result += Char.ToUpper(text[i + 1]);
    }

    return result;
}

Upvotes: 4

atlaste
atlaste

Reputation: 31106

If I look at the question, the array is always smaller than the string that you need to return. A string is basically a series of characters with some functionality... so why not simply modify everything in-place and create one from the char[]? Tools like StringBuilder are also an option, but they need to allocate, reallocate, etc - which is unnecessary in this case.

Here's what I came up with... not sure if it's what you mean, but it does resemble your question.

static void Main(string[] args)
{
    char[] array = " This is  a sentence.".ToCharArray();
    int dst = 0;

    if (array[0] != ' ' && char.IsLetter(array[0]))
    {
        array[dst++] = char.ToUpper(array[0]);
    }
    for (int i = 1; i < array.Length; i++)
    {
        if (array[i - 1] == ' ' && char.IsLetter(array[i]))
        {
            array[dst++] = char.ToUpper(array[i]);
        }
    }
    string result = new string(array, 0, dst);

    Console.WriteLine(result);
    Console.ReadLine();
}

So, why this approach?

  • The OP is interested in characters, not digits and other stuff. That's pretty easy to check in a loop, especially if you want to handle more complex stuff (f.ex. '#@foo123' -- if we wanted to add a 'F' here, this is easy to implement)
  • Adding characters to a string using += means we have to allocate a string, copy the old one, add a character, and throw away the old string. That's an easy way to break your performance any day of the week. StringBuilder's are better... but why use something like that if we already know what we want to have.

Upvotes: 1

LifeOfPi
LifeOfPi

Reputation: 625

Try below code. You can accept parameter as a input

still not clear about your requirement as you said you want first character with space.

public static void Main()
    {

        string test = GetString("FooBar");           

    }

    public static string GetString(string valuesNew)
    {
        //string valuesNew = "Foo";
        char[] array = valuesNew.ToCharArray();
        string result = "";

        array[0] = char.ToUpper(array[0]);
        result = array[0] + " ";

        return result;
    }

Upvotes: -1

Yogi
Yogi

Reputation: 9739

The error seems to be in the following line -

return new string(result);

Because the result is of type string, but the new string.. takes in char array.

But anyway as you are returning string, you can directly return result

EDIT: Following your comments, this should work for you -

        string valuesNew = "To dO someThing in HomEWork";
        char[] array = valuesNew.ToCharArray();
        string result = array[0].ToString();
        for (int i = 1; i < array.Length; i++)
        {
            if (array[i - 1] == ' ')
            {
                array[i] = char.ToUpper(array[i]);
                result += array[i];
            }
        }

        return result;

Output is TDSIH

Upvotes: 2

Chaos Legion
Chaos Legion

Reputation: 2970

If I understand your query correctly, you are looking for something like this:

using System.Globalization;
...
...
...
public string CapitalizeEveryWordInMyString(string input)
{
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input);
}

Upvotes: 0

Related Questions