Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

spliting a string on length

Options
  • 28-04-2007 1:52pm
    #1
    Closed Accounts Posts: 381 ✭✭


    hey again! :P

    i am trying to split a string in asp on its length using ASP.

    Because access can only take fields upto 255 characters i need to make sure in the unlikely event that its longer that the string will be split in two and stored another field.

    "Code -

    Dim TheString
    Dim SplitArray

    TheString="0123456789"

    if len(Thestring) > 5 then
    SplitArray=Split(TheString,"4") 'Splits on character 4

    Response.write(SplitArray(0)

    Response.write(SplitArray(1)
    else
    Response.write(TheString)

    End if

    i use this to split it on the number 4 but seen as i wont know what the value of the 250th Character when someone is putting it in. how do i make it split on the 250th character? any idea?


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    I don't know VB but a C/C++ programmer.

    In c++ there is a substring function called substr that takes the number of charactors to splt instead of the actual value. I'm sure there is a similer thing in VB.

    Just found this, may help:
    MyString = "1234567890"
    
    startPos = 3
    charCount = 4
    
    Print Mid$(MyString,startPos,charCount)      ' Prints "3456"
    

    You would have start position as 0 or 1 (God vb sucks if first element is at position 1 - It usually element 0 that is the first in programming languages)


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Is there a likelihood that users will input more than 250 characters? Or perhaps put another way, is there a need to capture every single bit of user data without constraining what they can input?

    If not, why not limit the character input on the asp.net textbox control? The user will take the hint when they can't put any more data into the control and adjust accordingly.

    If you want to store data overrun, you can use the .Net String class method 'SubString()' to store data in two seperate strings.

    So what you could do is something like:
    // C# code, but should be easy to transpose to any other .Net language
    
    int maxSize = 250;
    string userInput = TextBoxControl.Text;
    string overflow = String.Empty;
    
    if (userInput.Length > maxSize)
    {
    	overflow = userInput.Substring(maxSize);	// copy from position 250 onwards
    	userInput = userInput.Remove(maxSize);			// Remove additional characters from original string
    }
    

    Granted that code is off the top of my head so might need some correction (and assuming I've picked up what you're asking for correctly too ... )

    But there is one issue with the code. And that is this. What if the overflow text is longer than 250 characters in turn?


  • Closed Accounts Posts: 381 ✭✭El_mariachi


    Lemming wrote:
    Is there a likelihood that users will input more than 250 characters? Or perhaps put another way, is there a need to capture every single bit of user data without constraining what they can input?

    Yea unfortunately i have to capture ever bit of data even if its over 250 Characters in length which is the problem. It mine never happen but the system needs to be able to handle the event.


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Yea unfortunately i have to capture ever bit of data even if its over 250 Characters in length which is the problem. It mine never happen but the system needs to be able to handle the event.

    In that case you then need to address the second question ..... what if the overflow is more than 250 characters in length too? Are you permitted indefinite (to use the term) numbers of overflow fields? Only one? etc.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Use the Left and Right functions to take a string from the X number of Characters from the start or end

    So You can Take the First 250 Chars and then get the get some more etc.. will you be splitting into multiple strings or whats the deal?

    You can also use Mid


  • Advertisement
  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Yea unfortunately i have to capture ever bit of data even if its over 250 Characters in length which is the problem.
    Change the field type from Text to Memo and it'll hold more than 255 Characters.


  • Closed Accounts Posts: 381 ✭✭El_mariachi


    Change the field type from Text to Memo and it'll hold more than 255 Characters.

    Your a Life saver man! thanks!!!!


  • Registered Users Posts: 14,148 ✭✭✭✭Lemming


    Hang on a sec ... I thought you mentioned the reason for wanting to split the string was to do with Access (as in the crappy wannabe-database manager) restrictions and not the ASP.net controls you were using?

    I'm confused now :confused:

    Oh wait ... TC mentioned field type. I totally misread that.


Advertisement