12 Replies - 1440 Views - Last Post: 04 August 2010 - 05:49 AM Rate Topic: -----

#1 t5Guy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-July 10

Replacing character in a string

Posted 02 August 2010 - 02:52 AM

Hi,
I wanrt to be able to replace a character in a string with upper case, thus: 'this is a string' becomes 'This Is A String.'
Just an exercise, I'm not a student or anything, just moving on from VB to C#.
So... I have set up an object called ToUpper that takes a string argument and in it there is a loop and within the loop an if statement thus:
for(i=1;i<string.Length;i++)
  If(substring(string,i-1, 1) = " "
{
substring(string, i, 1) = substring(string,i,1).ToUpper;
}

Now this works in VB and in several other languages bit in C# I get a message saying that the left hand side of an asssignment must be a variable,property or indexer.
What can I do?

This post has been edited by AdamSpeight2008: 02 August 2010 - 03:52 AM
Reason for edit:: Please use the code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Replacing character in a string

#2 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Replacing character in a string

Posted 02 August 2010 - 03:16 AM

Hey you had some syntax errors and i fixed them

    string a= "this is a string";

            for (int i = 1; i < a.Length; i++)
            {
                if (a.Substring(i - 1, 1) == " ")
                {

                a.Substring( i, 1) = a.Substring(i,1).ToUpper;  // This is not possible. Gives a error

                }

            }


This post has been edited by Crunch: 02 August 2010 - 03:17 AM

Was This Post Helpful? -1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,464
  • Joined: 29-May 08

Re: Replacing character in a string

Posted 02 August 2010 - 03:54 AM

Strings are an immutable data type. Which means you to create a new one if you want to alter or change it.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Replacing character in a string

Posted 02 August 2010 - 05:04 AM

I am not sure what you are expecting this line to do...

substring(string, i, 1) = substring(string,i,1).ToUpper;



maybe you should use the Replace method?
Was This Post Helpful? 0
  • +
  • -

#5 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Replacing character in a string

Posted 02 August 2010 - 06:06 AM

I think an easier way would be to use the Split() method. You can check it up for more information on it, but the Split() method "splits" up a string and puts it into an array. The default parameters of this method(no parameters) splits it up by spaces.

So if you have this string:
String test = "This will be split up";


And then do this:
String[] array = test.Split();


Then you would have an array that looks like this:
array = {this, will, be, split, up};



Then for the first letter of each element in your array you could use the char.ToUpper() method.

Should work great for you. If you get stuck on the implementation or need some more guidance post what you've done and I'll check it out.

Hope this helps!

This post has been edited by eZACKe: 02 August 2010 - 06:08 AM

Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Replacing character in a string

Posted 02 August 2010 - 06:21 AM

This worked for me..

class Program
{
    static void Main(string[] args)
    {
        string test = "this is a string";
        Console.WriteLine(test);

        string temp = string.Empty;

        for (int i = 0; i < test.Length; i++)
        {
            if (i == 0 || test.Substring(i - 1, 1) == " ")
            {
                temp = test.Substring(i, 1);
                test = test.Remove(i, 1);
                test = test.Insert(i, temp.ToUpper());
            }
        }

        Console.WriteLine(test);
        Console.Read();
    }
}


Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,464
  • Joined: 29-May 08

Re: Replacing character in a string

Posted 02 August 2010 - 07:04 AM

There a short way in vb.net see snippet.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Replacing character in a string

Posted 02 August 2010 - 07:19 AM

View PostAdamSpeight2008, on 02 August 2010 - 10:04 AM, said:

There a short way in vb.net see snippet.


but that's not available in C#, which he is moving to.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4484
  • View blog
  • Posts: 7,812
  • Joined: 08-June 10

Re: Replacing character in a string

Posted 02 August 2010 - 07:23 AM

Here's the corresponding C# version, Adam.
public static string ToTitleCase(this string sentence)
{
    return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(sentence);
}

Was This Post Helpful? 2
  • +
  • -

#10 t5Guy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-July 10

Re: Replacing character in a string

Posted 03 August 2010 - 03:33 PM

eZACKe - thanks, I did the array bit but the Char stuff I can't find how to do that. I can do all of what I want to do in 23 different languages but not the one I now need to learn! Lol
Was This Post Helpful? 0
  • +
  • -

#11 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Replacing character in a string

Posted 03 August 2010 - 09:33 PM

Alright t5Guy, then let's go back to where I left off last time:

You after the split you would have an array such as:
array = {this, will, be, split, up};



So each one of those strings are at a different index, 0 through 4 in the array.

This code can be a bit complicated, but I'm not going to just give it to you, I think you can figure it out with the help of an algorithm:

-Do all of this for every element in the array:
-Find the substring of the element from 0 to 1 (just the first character)
-Find the substring of everything but the first character
-Use the ToChar() method on the variable in which you have the first character:
begin = "" + Char.ToUpper(begin[0]);


-Concatenate the two strings together
-Put the new concatenated string back in the array where it goes


On a first read through it may seem a bit confusing, but it's really not too hard. Took about 5 lines.


Hope this helps! Need more help? Just ask!
Was This Post Helpful? 0
  • +
  • -

#12 t5Guy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-July 10

Re: Replacing character in a string

Posted 04 August 2010 - 03:22 AM

eZACKe:
Thanks, man - this worked fine:
private string properCase()
     {
         string [] char_Array = textToSet.Split();
         textToSet = "";
         for (int j = 0; j < char_Array.Length; j++)
          {
           string first_char = char_Array[j].Substring(0, 1);
           string rest_of_chars = char_Array[j].Substring(1, char_Array[j].Length - 1);
           first_char = first_char.ToUpper();
           char_Array[j] = first_char + rest_of_chars;
           textToSet = textToSet + char_Array[j] + " ";
          }
           return textToSet;
        }


I just call the method from the main routine when the radio button for Proper Case is selected.
Thanks again. I have over 20 years' programming experience and more languages than you could shake a stick at, but C# is testing my learning power a bit!

This post has been edited by AdamSpeight2008: 04 August 2010 - 05:13 AM
Reason for edit:: Please use the codetags for code

Was This Post Helpful? 0
  • +
  • -

#13 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Replacing character in a string

Posted 04 August 2010 - 05:49 AM

Glad I could be assistance!

Just keep practicing, I'm sure you'll catch on. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1