String Split at index ....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tea Maker
    New Member
    • Jul 2007
    • 45

    String Split at index ....

    Hi,
    does anybody know how can I split a string at a specific index in vb.net or c#?

    Let's say I have a long text, and I want the text to display only the first 30 character, is there a way to do that?

    I know other split functions such as:

    Code:
    dim myInitialString, myStrings() as string
    Code:
    myInitialString = "Whatever....."
    Code:
    myStrings = myInitialString.split("anyCharacter")
    That's working very well, but I need to split my text at a specific index. Anyway help please?

    Regards,
    Wassim
  • khalid Ahmed
    New Member
    • Aug 2007
    • 11

    #2
    string myIntialString;
    string mystring=myInti alString.Substr ing(0,30);

    I hope this help you....

    Comment

    • Tea Maker
      New Member
      • Jul 2007
      • 45

      #3
      Originally posted by khalid Ahmed
      string myIntialString;
      string mystring=myInti alString.Substr ing(0,30);

      I hope this help you....

      Wow! thanks alot khaled. I tested it and it's working :D

      Just one question, what happens if the initial text is less than 30 characters? Doesn't it return an error on my page? or it just display its length normally?

      Regards,
      Wassim

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        It will error. Try something like this if you must:

        Code:
        string mystring="Less then 30 characters"
        int n=Math.Min(30,mystring.Length);
        string firstpart=mystring.SubString(0,n);

        Comment

        • Tea Maker
          New Member
          • Jul 2007
          • 45

          #5
          Originally posted by Plater
          It will error. Try something like this if you must:

          Code:
          string mystring="Less then 30 characters"
          int n=Math.Min(30,mystring.Length);
          string firstpart=mystring.SubString(0,n);
          Wow!!! fantastic. It's working amazingly now. The error disappeared, and I like the algorithm. Thanks Plater.
          Thanks alot guys, I appreciate your help :)

          Regards,
          Wassim

          Comment

          Working...