Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to increase string character value

How to increase string character value

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    T Willey
    wrote on last edited by
    #1

    Say you have a variable named "Str" and it's value is "A", how would you change it to "B" not knowing which letter to start at? Could you use the same method for strings that represent numbers also? I have searched the net and haven't found an answer. Thanks in advance.

    Tim

    L M M 3 Replies Last reply
    0
    • T T Willey

      Say you have a variable named "Str" and it's value is "A", how would you change it to "B" not knowing which letter to start at? Could you use the same method for strings that represent numbers also? I have searched the net and haven't found an answer. Thanks in advance.

      Tim

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      string test = "A";

      test = string.Format("{0}", (char)(test[0] + 1));

      => test = "B" This will work, if your value is an ASCII char. It's converted to an int, increased by 1 and converted back to a char regards

      T 1 Reply Last reply
      0
      • T T Willey

        Say you have a variable named "Str" and it's value is "A", how would you change it to "B" not knowing which letter to start at? Could you use the same method for strings that represent numbers also? I have searched the net and haven't found an answer. Thanks in advance.

        Tim

        M Offline
        M Offline
        Michael Potter
        wrote on last edited by
        #3

        This will convert a full string. You will end up with some punctuation at the end of the alphabit.

        string oldStr = "Hello";
        string newStr = "";
        char[] hold = oldStr.ToCharArray();
        for (int i = 0; i < hold.Length; i++)
        {
            newStr += (char)(hold[i] + 1);
        }
        MessageBox.Show(oldStr + Environment.NewLine + newStr);
        
        T 1 Reply Last reply
        0
        • L Lost User

          string test = "A";

          test = string.Format("{0}", (char)(test[0] + 1));

          => test = "B" This will work, if your value is an ASCII char. It's converted to an int, increased by 1 and converted back to a char regards

          T Offline
          T Offline
          T Willey
          wrote on last edited by
          #4

          I think all my values will be ASCII char's right now. Thank you.

          Tim

          G 1 Reply Last reply
          0
          • M Michael Potter

            This will convert a full string. You will end up with some punctuation at the end of the alphabit.

            string oldStr = "Hello";
            string newStr = "";
            char[] hold = oldStr.ToCharArray();
            for (int i = 0; i < hold.Length; i++)
            {
                newStr += (char)(hold[i] + 1);
            }
            MessageBox.Show(oldStr + Environment.NewLine + newStr);
            
            T Offline
            T Offline
            T Willey
            wrote on last edited by
            #5

            Thanks for the code Michael.

            Tim

            1 Reply Last reply
            0
            • T T Willey

              I think all my values will be ASCII char's right now. Thank you.

              Tim

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Actually it works for any character, but it's not very meaningful for all characters.

              --- b { font-weight: normal; }

              1 Reply Last reply
              0
              • T T Willey

                Say you have a variable named "Str" and it's value is "A", how would you change it to "B" not knowing which letter to start at? Could you use the same method for strings that represent numbers also? I have searched the net and haven't found an answer. Thanks in advance.

                Tim

                M Offline
                M Offline
                mihoc
                wrote on last edited by
                #7

                Here's a small recursive code for increasing a string. It's for increasing through Excel columns, so it also increases strings like "AD" or "ZZ". Use the method IncString:

                private string IncString(string s)
                {
                if (String.IsNullOrEmpty(s)) return "A";
                if (s[s.Length - 1] == 'Z')
                {
                if (s.Length == 1) return "AA";
                else return IncString(s.Substring(0, s.Length - 1)) + "A";
                }
                else
                {
                if (s.Length == 1) return IncChar(s[0]).ToString();
                else return s.Substring(0, s.Length - 1) + IncChar(s[s.Length - 1]).ToString();
                }
                }

                    private char IncChar(char c)
                    {
                        return (char)((int)c + 1);
                    }
                
                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups