Jump to content

C# ToUpper() function

MisterWhite

So i need to correct the capitalization of the string so that every first letter would be Upper, here's what i've done (not sure if i'm using the Substring correctly):

for (int i = 0; i < amount; i++) 
{
  cities [i].ToLower ();
  upper = cities [i];
  for (int j = 0; j < cities [i].Length; j++) 
  {
   upper.Substring (0).ToUpper ();
   if (upper.Substring (j) == " ")
   upper.Substring (j + 1).ToUpper ();
  }
  cities [i] = upper;
}

 

but it does nothing, were is my mistake?(maybe the usage of substring)

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Can you post the entire class / method?

All code?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

using System;

namespace CitiesDatabase
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string[] cities = new string[500];
            int[] peopleCount = new int[500]; 
            int option = -1;
            int amount = 0;
            string search;
            string modify;
            int remove;
            string upper;

            while (option != 0) 
            {
                Console.WriteLine("1.- Add a new city");
                Console.WriteLine("2.- View all cities");
                Console.WriteLine("3.- Modify a record");
                Console.WriteLine("4.- Insert a new record (CURRENTLY NOT WORKING!!)");
                Console.WriteLine("5.- Delete a record");
                Console.WriteLine("6.- Search in the records");
                Console.WriteLine("7.- Correct the capitalization of the names");
                Console.WriteLine("0.- Exit");
                Console.WriteLine();
                Console.Write("Select option: ");
                option = Convert.ToInt32 (Console.ReadLine ());

                switch (option) 
                {
                case 7:
                    for (int i = 0; i < amount; i++) 
                    {
                     EDIT:   cities [i]=cities.ToLower ();
                        upper = cities [i];
                        for (int j = 0; j < cities [i].Length; j++) 
                        {
                            upper.Substring (0).ToUpper ();
                            if (upper.Substring (j) == " ")
                                upper.Substring (j + 1).ToUpper ();
                        }
                        cities [i] = upper;
                    }
                    Console.WriteLine ("Corrected!");
                    break;
                default:
                    Console.WriteLine("Wrong option");
                    break;
                }
            }
        }
    }
}

 

I deleted other switch options because they are unrelated

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't used C#, but shouldn't this

cities [i].ToLower ();
be this?

cities[i] = cities [i].ToLower ();
Or does the function assign the new value already?

 

It did make everything tolower atleast, thanks for this

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Is the 0th element always the first char of a name? Or could there be spaces? 

 

How do you know what's a name? 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Is the 0th element always the first char of a name? Or could there be spaces? 

 

How do you know what's a name? 

Well no city name starts with a space :P

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't used C#, but shouldn't this

cities [i].ToLower ();
be this?

cities[i] = cities [i].ToLower ();
Or does the function assign the new value already?

 

 

 

Ok. I think you have the same problem with the upper, you're just calling the Upper function, but you're not actually making the upper case letter replace the one present in the string.

@MisterWhite

 

https://msdn.microsoft.com/en-us/library/system.string.tolower(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx

The ToUpper and ToLower methods do not modify the variable, they return a copy of the string. You need to store the new string somewhere if you want to use those to assist in modifying the original string

Have you tried this:

string title = "war and peace";TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;title = textInfo.ToTitleCase(title); //War And Peace
Source: http://stackoverflow.com/questions/1206019/converting-string-to-title-case-in-c-sharp

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

@MisterWhite

 

https://msdn.microsoft.com/en-us/library/system.string.tolower(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx

The ToUpper and ToLower methods do not modify the variable, they return a copy of the string. You need to store the new string somewhere if you want to use those to assist in modifying the original string

Have you tried this:

 

string title = "war and peace";TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;title = textInfo.ToTitleCase(title); //War And Peace
Source: http://stackoverflow.com/questions/1206019/converting-string-to-title-case-in-c-sharp

 

 

Dont realy know what it means

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

MAYBE i just use the Substring incorrectly??

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah. I'm sure I could figure this out without the entire class/method with time, but it would help if I had some background on the variables

 

 

 

@MisterWhite

 

https://msdn.microsoft.com/en-us/library/system.string.tolower(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx

The ToUpper and ToLower methods do not modify the variable, they return a copy of the string. You need to store the new string somewhere if you want to use those to assist in modifying the original string

Have you tried this:

 

string title = "war and peace";TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;title = textInfo.ToTitleCase(title); //War And Peace
Source: http://stackoverflow.com/questions/1206019/converting-string-to-title-case-in-c-sharp

 

 

Hey you 2, maybe the whole issue is in my usage of Substring? i used it for the first time

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

MAYBE i just use the Substring incorrectly??

no....

cities [i].ToLower ();  upper = cities [i];

you are using the ToUpper() and ToLower() methods incorrectly

 

they DO NOT MODIFY the string in question

 

cities.ToLower() returns the character at that index, converted to lowercase. It does not modify that character, meaning the string never changes.

 

so when you call upper = cities, you are still storing the original value of that index because it was never changed to begin with.

Link to comment
Share on other sites

Link to post
Share on other sites

no....

cities [i].ToLower ();  upper = cities [i];

you are using the ToUpper() and ToLower() methods incorrectly

 

they DO NOT MODIFY the string in question

 

cities.ToLower() returns the character at that index, converted to lowercase. It does not modify that character, meaning the string never changes.

 

so when you call upper = cities, you are still storing the original value of that index because it was never changed to begin with.

Well i corrected that mistake, still nothing

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well i corrected that mistake, still nothing

upper.Substring (0).ToUpper ();   if (upper.Substring (j) == " ")   upper.Substring (j + 1).ToUpper ();

https://msdn.microsoft.com/en-us/library/system.string.substring(v=vs.110).aspx

substring needs a starting and ending index, otherwise it just goes until the end of string.

try a foreach loop instead of the first for loop, makes it a bit easier logically.

 

foreach(string city in cities)

{

    ciy[0] = city[0].ToUpper();

    //other stuff you wanna do

}

just semantics though, won't make any difference really.

I already posted a better solution anyways. All you're doing is converting a string to CamelCase, or TitleCase as its referred to below

 

Have you tried this:

string title = "war and peace";TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;title = textInfo.ToTitleCase(title); //War And Peace
Source: http://stackoverflow.com/questions/1206019/converting-string-to-title-case-in-c-sharp

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

upper.Substring (0).ToUpper ();   if (upper.Substring (j) == " ")   upper.Substring (j + 1).ToUpper ();

when i correct this into this:

upper=upper.Substring (0).ToUpper ();if (upper.Substring (j) == " ")upper=upper.Substring (j + 1).ToUpper ();

It makes every letter Capital

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

upper.Substring (0).ToUpper ();   if (upper.Substring (j) == " ")   upper.Substring (j + 1).ToUpper ();

when i correct this into this:

upper=upper.Substring (0).ToUpper ();if (upper.Substring (j) == " ")upper=upper.Substring (j + 1).ToUpper ();

It makes every letter Capital

 

read the documentation for the substring method

 

not sure why you're using the substring and spaces, if the string contains a space, split the string on the space, and handle the pieces accordingly

Link to comment
Share on other sites

Link to post
Share on other sites

using System;

namespace CitiesDatabase

{

    class MainClass

    {

        public static void Main (string[] args)

        {

            string[] cities = new string[500];

            int[] peopleCount = new int[500]; 

            int option = -1;

            int amount = 0;

            string search;

            string modify;

            int remove;

            string upper;

            while (option != 0) 

            {

                Console.WriteLine("1.- Add a new city");

                Console.WriteLine("2.- View all cities");

                Console.WriteLine("3.- Modify a record");

                Console.WriteLine("4.- Insert a new record (CURRENTLY NOT WORKING!!)");

                Console.WriteLine("5.- Delete a record");

                Console.WriteLine("6.- Search in the records");

                Console.WriteLine("7.- Correct the capitalization of the names");

                Console.WriteLine("0.- Exit");

                Console.WriteLine();

                Console.Write("Select option: ");

                option = Convert.ToInt32 (Console.ReadLine ());

                switch (option) 

                {

                case 7:

                    for (int i = 0; i < amount; i++) 

                    {

                     EDIT:   cities [i]=cities.ToLower ();

                        upper = cities [i];

                        for (int j = 0; j < cities [i].Length; j++) 

                        {

                            upper.Substring (0).ToUpper ();

                            if (upper.Substring (j) == " ")

                                upper.Substring (j + 1).ToUpper ();

                        }

                        cities [i] = upper;

                    }

                    Console.WriteLine ("Corrected!");

                    break;

                default:

                    Console.WriteLine("Wrong option");

                    break;

                }

            }

        }

    }

}

 

I deleted other switch options because they are unrelated

before you go any further, make sure you add comments into your code. get into this habit now. you'll take me later when you return to a project after 2+ years and wonder wtf you where thinking, but it is all commented in code.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×