Results 1 to 13 of 13

Thread: [RESOLVED] StringManipulation

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    43

    Resolved [RESOLVED] StringManipulation

    Hi guys,

    I just want to make sure that what I am doing here is correct. The following class splitting a string into two strings when it finds ",". It works fine but I am a bit worried of my code.
    Can you guys please let me know is it correct way splitting the string or is there any easier way I can write. I appreciate your comments.

    public class StringManipulation
    {

    //constructor
    StringManipulation(string textValues)
    {

    this.textOne = this.GetTextOne(textValues);
    this.textTwo = this.GetTextTwo(textValues);
    }
    //Methods
    public string GetTextOne(string textValues)
    {
    string[] arInfo = new string[1000];
    string info = textValues;
    char[] splitter = {','};
    arInfo = info.Split(splitter);
    if(arInfo.Length > 0)
    {
    return arInfo[0].Trim();
    }
    else
    {
    return string.Empty;

    }


    }
    public string GetTextTwo(string textValues)
    {
    string[] arInfo = new string[1000];
    string info = textValues;
    char[] splitter = {','};
    arInfo = info.Split(splitter);
    if(arInfo.Length > 1)
    {
    return arInfo[1].Trim();
    }
    else
    {
    return arInfo[0].Trim();

    }

    }
    //propterties
    public string TextOne
    {

    return this.textOne;
    }
    public string TextTwo
    {
    return this.textTwo

    }
    //Member variables
    private string textOne;
    private string textTwo;

    }
    ***learning everyday ***

  2. #2
    ex-Administrator brad jones's Avatar
    Join Date
    Nov 2002
    Location
    Indianapolis
    Posts
    6,614

    Re: StringManipulation

    Creating the arrays of strings and such would be a lot of processing for no real return. If all you are doing is splitting a string into two, then the following code is much more concise. (but still isn't necessarily the best)

    Code:
    using System;
    
    public class StringManipulation
    {
      //constructor
      StringManipulation(string textValues)
      {
        this.textOne = this.GetTextOne(textValues);
        this.textTwo = this.GetTextTwo(textValues);
      }
      //Methods
      public string GetTextOne(string textValues)
      {
        int index = textValues.IndexOf(",");
        return textValues.Substring(0, index);
      }
      public string GetTextTwo(string textValues)
      {
        int index = textValues.IndexOf(",");
        return textValues.Substring(index+1, (textValues.Length - index));
      }
      //propterties
      public string TextOne
      {
        get
        {
          return this.textOne;
        }
      }
      public string TextTwo
      {
        get
        {
          return this.textTwo;
        }
      }
      //Member variables
      private string textOne;
      private string textTwo;
    
      // added just to test it out... 
      public static void Main()
      {
        string MyString = "1234567890,abcdefghijklmnopqrstuvwxyz";
    
        StringManipulation NewString = new StringManipulation(MyString);
    
        Console.WriteLine("Part 1: >>{0}<<", NewString.TextOne );
        Console.WriteLine("Part 2: >>{0}<<", NewString.TextTwo );
        Console.Read();
      }
    }
    I based this code off of what you had already done.

    Brad!
    Have you given out your reputation points today? Select the Rate This Post link to give points for good posts!
    -------------------------------------------------------------
    Brad! Jones
    Lots of Software, LLC
    (I wrote: C Programming in One Hour a Day) (Dad Jokes Book) (Follow me on Twitter)

    --------------------------------------------------------------

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: StringManipulation

    Code:
    StringManipulation(string textValues)
      {
        int index = textValues.IndexOf(",");
    
        this.textOne = textValues.Substring(0, index);
        this.textTwo = textValues.Substring(index + 1);
      }
    Then you don't need GetTextOne() or GetTextTwo().
    I don't live here any more.

  4. #4
    ex-Administrator brad jones's Avatar
    Join Date
    Nov 2002
    Location
    Indianapolis
    Posts
    6,614

    Re: StringManipulation

    Very good. I was focused on just getting his class to work and should have looked closer....

    Brad!
    Have you given out your reputation points today? Select the Rate This Post link to give points for good posts!
    -------------------------------------------------------------
    Brad! Jones
    Lots of Software, LLC
    (I wrote: C Programming in One Hour a Day) (Dad Jokes Book) (Follow me on Twitter)

    --------------------------------------------------------------

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    43

    Re: StringManipulation

    Bravo! In fact I did not know that I can use as simple that,

    Thanks very much guys. By the way Brad_jones it is not his. it is her

    Again how do I say that this question is [RESOLVED]. Do I need to edit my post and modify the subject as [RESOLVED].

    bye for now.
    ***learning everyday ***

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    43

    Re: StringManipulation

    OOPS! sorry it did not actually work wossname.

    It throws an errrrrrrrrrror

    Length cannot be less than zero. Parameter name: length

    it always expect sometext after "," but my class functions handle that.


    ***learning everyday ***

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: StringManipulation

    Quote Originally Posted by dummy_pmgr
    Again how do I say that this question is [RESOLVED]. Do I need to edit my post and modify the subject as [RESOLVED].

    Nearly on the top of this page, there is some links. One of them says "thread tools", if you click it, then you will get a pop up menu. Press the resolved link, and it will be done for you..



    PS: I am a bit fussed by your last post. Does it work now, or do you still have the error?


    - ØØ -

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    43

    Re: StringManipulation

    Hi NoteMe,

    Thanks for Thread tools.

    Yea! it works fine according to my post. I wanted to simplyfy my code. If you go through this whole thread you will understand what I mean.

    thanks
    Last edited by dummy_pmgr; Aug 5th, 2005 at 11:53 AM.
    ***learning everyday ***

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: StringManipulation

    Quote Originally Posted by dummy_pmgr
    OOPS! sorry it did not actually work wossname.

    It throws an errrrrrrrrrror

    Length cannot be less than zero. Parameter name: length

    it always expect sometext after "," but my class functions handle that.


    It works perfectly, your data must be bad. You get an index of -1 when IndexOf() did not find a comma in your string
    I don't live here any more.

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    43

    Re: StringManipulation

    Thanks wossname, I will try with some more data and let you know.

    cheers,
    ***learning everyday ***

  11. #11

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    43

    Re: StringManipulation

    Hello again Wossname,

    It is all my dodgy data. It works great and perfectly now.

    I changed a bit to handle the empty data.

    StringManipulation(string textValues)
    {
    int index = textValues.IndexOf(",");
    this.textOne = textValues.Substring(0, index);

    if(index != -1)
    {
    this.textTwo = textValues.Substring(index + 1);
    }
    else
    {
    this.textTwo = "There is no string after comma";
    }
    }

    One final question in this thread which I really dont know. Is it the best practice to write a lots of code in the constructor?

    Thanks for your help.

    dummy
    ***learning everyday ***

  12. #12
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: StringManipulation

    "Lots of code" is misleading. It is best to do as much of work as possible in the constructor, because it will save having to do it later on when speed might be critical.

    There are hundreds of ways to maximise performance and this is just one of them, but don't go overboard. You should be able to justify every line of code you write.
    I don't live here any more.

  13. #13

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    43

    Re: StringManipulation

    Thanks wossname
    ***learning everyday ***

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width