Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Rey
    Rey is offline
    New Coder
    Join Date
    Sep 2006
    Location
    Peoria, AZ
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Appreciate help in debugging code

    Howdy.

    Posted to the ap.net forum (Default page does not display) when a simple page would not display. This page had 2 external javascript files.

    When I commented the links out, page displayed. Combined the files into one (utilities.js) and page again does not display.

    Appreciate some troubleshooting help as the page displays but there is no err and also no objects (textbox nor button) displayed. Am using Werb Developer Express 2005.

    Code below contains the word wrap code converted from vb.net to c# (both work) and now to javascript. Also included is the vb to javascript code obtained from 4guys from rolla site.

    Thank you,

    Rey




    Code:
    
     
        // contains wordwrap code and vb type functions (left, right, mid) to javascript 
    
        //function WordWrapText(userNotes)
        function WordWrapText()
        {
                    // purpose: read in line and wrap it at 50 chars if space 
                    // or read backwards til space is found 
    
    				var userNotes = "";
                    var intTextLength = 0;
                    var aLine = "";
                    var strInitialRemainder = "";        // text remaining after initial 50 chars taken       
                    var x = 0;                                        // counter 
                    var s;                                           // test each char to see if a space chr(32) 
                    var intMaxLetters = 50;                           // max chars/letters in a line 
                    var aLineRemainder = "";             // chars remaining in line after space found       
                    var strRemainder = "";               // remainder from 1st line 
                    var strTheLines = "";                // display of lines from array 
                    var aSpace = " ";                 // a space
                    var aCharacter;                                  // holds a character;
    
                    // using arraylist to dynamically add elements; no Redim in c#
                    ArrayList arlLines = new ArrayList();
                    
                    userNotes = "this is a test of the american broadcast system. this is only a test.";
    
                    intTextLength = userNotes.Length;
    
                    // if len is >= 50 take the first 50 
                    if (intTextLength >= 51)
                    {
                            aLine = userNotes.Substring(0, intMaxLetters);
    
                            // place remainder of txt 
                            strInitialRemainder = userNotes.Substring(aLine.Length, (userNotes.Length - intMaxLetters));
                    }
                    else
                    {
                            aLine = userNotes;
    
                            arlLines.Add(aLine);
                    }
    
                    // see if last char is space 
                    // if not walk back... 
                    // 1st line in note is task verbiage? 
                    if (intTextLength >= 51)
                    {                               
                            aCharacter = Right(aLine, 1);
                            
                            //if (clsLeftRightMid.Right(aLine, 1) == aSpace)
                            if (aCharacter == aSpace)
                            {
    
                                    arlLines.Add(aLine.Trim());
                            }
                            else
                            {
                                    // need to get to 1st space to make up a line 
                                    for (x = (aLine.Length - 1); x >= 0; x += -1)
                                    {
                                            aCharacter = aLine[x];
    
                                            s = aLine[x];
    
                                            if (s == Convert.ToChar(32))
                                            {
                                                    // take remainder of 1st line to prepend to initial remainder 
                                                    //aLineRemainder = clsLeftRightMid.Right(aLine, aLine.Length - x);
                                                    aLineRemainder = Right(aLine, aLine.Length - x);
    
                                                    arlLines.Add(aLine.Substring(0, x));
                                                    break;
                                            }
                                    }
                            }
    
                            // now for rest 
                            // add text remainder from 1st line to initial text remainder 
                            strRemainder = aLineRemainder + strInitialRemainder;
                            //L += 1;
    
                            // get other lines 
                            while (strRemainder.Length != 0)
                            {
                                    // if remainder less than 50, set next array element and exit 
                                    if (strRemainder.Length < intMaxLetters)
                                    {
                                            aLine = strRemainder.Substring(0);
                                            arlLines.Add(aLine);
                                            break;
                                    }
                                    else
                                    {
                                            aLine = strRemainder.Substring(0, intMaxLetters);
                                    }
    
                                    // remove letters 
                                    strRemainder = strRemainder.Replace(aLine, "");
    
    
                                    // do it again                                   
                                    aCharacter = Right(aLine, 1);
    
    
                                    if (aCharacter == aSpace)
                                    {
                                            arlLines.Add(aLine.Trim());
                                    }
                                    else
                                    {
                                            // need to get to 1st space to make up a line 
                                            for (x = (aLine.Length - 1); x >= 0; x += -1)
                                            {
                                                    s = aLine[x];
    
                                                    // looking for space 
                                                    //if (s == Convert.ToChar(32))
                                                    if (s == aSpace)
                                                    {
                                                            // take remainder of 1st line to add to initial remainder 
                                                            aLineRemainder = Right(aLine, aLine.Length - x);
    
                                                            // place txt remainder in front of rest 
                                                            strRemainder = aLineRemainder + strRemainder;
    
                                                            arlLines.Add(aLine.Substring(0, x));
                                                            break;
                                                    }
                                            }
                                    }
    
                            }//while (strRemainder.Length != 0)
                    }//if (intTextLength >= 51)
    
                    // to loop thru arraylist
                    //IEnumerator theEnumerator;
    
                    //theEnumerator = arlLines.GetEnumerator();
                    //x = 0;
                    //theEnumerator.MoveNext();
    
                    //// move to first element in arraylist arlLines       
                    //foreach (string thestring in arlLines)
                    //{
                    //    strTheLines += "Line " + x.ToString() + ": " + thestring + "\r\n";
                    //    x += 1;
                    //    theEnumerator.MoveNext();
                    //}
    
    
                    //MessageBox.Show(strTheLines, "Done - Lines to input", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
                    // return an array
                    Array arrTest = arlLines.ToArray();
                    return arrTest;
    
    
        }  
    
    
    
    // vb to javascript functions
        // retrieved 8/25/08 from http://www.4guysfromrolla.com/webtech/vb2java.shtml
    
    
        //Left(string, length): Returns a specified number of characters from the
        //                      left side of a string
    
        function Left(str, n)
        /***
        IN: str - the string we are LEFTing
        n - the number of characters we want to return
         
        RETVAL: n characters from the left side of the string
        ***/
        {
            if (n <= 0)     // Invalid bound, return blank string
                return "";
            else if (n > String(str).length)   // Invalid bound, return
                return str;                // entire string
            else // Valid bound, return appropriate substring
                return String(str).substring(0, n);
        }
    
        //Right(string, length): Returns a specified number of characters from the
        //                       right side of a string
    
        function Right(str, n)
        /***
        IN: str - the string we are RIGHTing
        n - the number of characters we want to return
         
        RETVAL: n characters from the right side of the string
        ***/
        {
            if (n <= 0)     // Invalid bound, return blank string
                return "";
            else if (n > String(str).length)   // Invalid bound, return
                return str;                     // entire string
            else { // Valid bound, return appropriate substring
                var iLen = String(str).length;
                return String(str).substring(iLen, iLen - n);
            }
        }
    
        //Trim(string) : Returns a copy of a string without leading or
        //              trailing spaces
        function Trim(str)
        /***
        PURPOSE: Remove trailing and leading blanks from our string.
        IN: str - the string we want to Trim
         
        RETVAL: A Trimmed string!
        ***/
        {
            return RTrim(LTrim(str));
        }
    
        //Mid(string, start, length): Returns a specified number of characters from a
        //                            string
    
        function Mid(str, start, len)
        /***
        IN: str - the string we are LEFTing
        start - our string's starting position (0 based!!)
        len - how many characters from start we want to get
         
        RETVAL: The substring from start to start+len
        ***/
        {
            // Make sure start and len are within proper bounds
            if (start < 0 || len < 0) return "";
    
            var iEnd, iLen = String(str).length;
            if (start + len > iLen)
                iEnd = iLen;
            else
                iEnd = start + len;
    
            return String(str).substring(start, iEnd);
        }
    
        //LTrim(string) : Returns a copy of a string without leading spaces.
        function LTrim(str)
        /***
        PURPOSE: Remove leading blanks from our string.
        IN: str - the string we want to LTrim
         
        RETVAL: An LTrimmed string!
        ***/
        {
            var whitespace = new String(" \t\n\r");
    
            var s = new String(str);
    
            if (whitespace.indexOf(s.charAt(0)) != -1) {
                // We have a string with leading blank(s)...
    
                var j = 0, i = s.length;
    
                // Iterate from the far left of string until we
                // don't have any more whitespace...
                while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                    j++;
    
    
                // Get the substring from the first non-whitespace
                // character to the end of the string...
                s = s.substring(j, i);
            }
    
            return s;
        }
    
        //RTrim(string) : Returns a copy of a string without trailing spaces.
        function RTrim(str)
        /***
        PURPOSE: Remove trailing blanks from our string.
        IN: str - the string we want to RTrim
         
        RETVAL: An RTrimmed string!
        ***/
        {
            // We don't want to trip JUST spaces, but also tabs,
            // line feeds, etc.  Add anything else you want to
            // "trim" here in Whitespace
            var whitespace = new String(" \t\n\r");
    
            var s = new String(str);
    
            if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
                // We have a string with trailing blank(s)...
    
                var i = s.length - 1;       // Get length of string
    
                // Iterate from the far right of string until we
                // don't have any more whitespace...
                while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                    i--;
    
    
                // Get the substring from the front of the string to
                // where the last non-whitespace character is...
                s = s.substring(0, i + 1);
            }
    
            return s;
        }
    
    
        //Len(String) : Returns the number of characters in a string
        function Len(str)
        /***
        IN: str - the string whose length we are interested in
         
        RETVAL: The number of characters in the string
        ***/
        { return String(str).length; }
    
        //InStr(str, SearchForStr) : Returns the location a character (charSearchFor)
        //                           was found in the string str
    
        // InStr function written by: Steve Bamelis - steve.bamelis@pandora.be
    
        function InStr(strSearch, charSearchFor)
        /*
        InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
        was found in the string str.  (If the character is not
        found, -1 is returned.)
         
        Requires use of:
        Mid function
        Len function
        */
        {
            for (i = 0; i < Len(strSearch); i++) {
                if (charSearchFor == Mid(strSearch, i, 1)) {
                    return i;
                }
            }
            return -1;
        }
    
    
    
    
        // Keep in mind that strings in JavaScript are zero-based, so if you ask
        // for Mid("Hello",1,1), you will get "e", not "H".  To get "H", you would
        // simply type in Mid("Hello",0,1)
    
        // You can alter the above function so that the string is one-based.  Just
        // check to make sure start is not <= 0, alter the iEnd = start + len to
        // iEnd = (start - 1) + len, and in your final return statement, just
        // return ...substring(start-1,iEnd)

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    This approach can use a more sophisticated system of hyphenation,
    and provision against orphans ending up on their own line,
    but it is a lot simpler than your method.

    Code:
    String.prototype.wrap=function(n,hyphen){
    	var str1, tem, ax, diff, S=[], A=this.split(/\s*\n/);
    	n= n || 50;
    	hyphen= hyphen || Math.min(12,n);
    	while(A.length){
    		str1= A.shift();
    		while(str1 && str1.length >n){
    			tem= str1.substring(0,n);
    			ax= tem.lastIndexOf(' ')+ 1;
    			tem= str1.substring(0,ax);
    			diff= n-ax;			
    			if(hyphen && diff > hyphen){
    				ax += hyphen;
    				tem= str1.substring(0,ax)+ '-';
    			}
    			str1= str1.substring(ax);			
    			S.push(tem);
    		}
    		if(str1) S.push(str1);
    	}
    	return S.join('\n');
    }
    //test:
    var s="this is a test of the american broadcast system. this is only a test.";
    //alert(s.wrap(line-length(Integer),hyphenate line ending words longer than(Integer)))



    s.wrap(50) // returns>>
    this is a test of the american broadcast system.
    this is only a test.

    s.wrap(20,5) // returns>>
    this is a test of
    the american broad-
    cast system. this
    is only a test.
    Last edited by mrhoo; 08-31-2008 at 07:01 AM.

  • #3
    Rey
    Rey is offline
    New Coder
    Join Date
    Sep 2006
    Location
    Peoria, AZ
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, mrhoo, for the reply and code.
    Nifty.

    Rey


  •  

    Posting Permissions

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