CSharp problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hkhellhkhell@hotmail.com

    CSharp problem

    i have an application that will take user input from a text box and
    write it
    to an access database. i need to make sure that if they ever enter a
    single
    line of text that it has at least 1 space for every 40 characters.

    so before i write the info to the database i have to make sure there is
    no
    lines of text that are longer than 40 characters without a space, and
    if
    there are insert a space at the 41st character. is that as hard as it
    sounds?


    hkhellhkhell@ho tmail.com

  • Michael Nemtsev

    #2
    Re: CSharp problem

    Hello hkhellhkhell@ho tmail.com,

    use Regexp class or manually check the TextBox.TextLen gth property
    longer than 40 characters without a space, and
    if
    there are insert a space at the 41st character. is that as hard as it
    sounds?
    ---
    WBR,
    Michael Nemtsev :: blog: http://spaces.live.com/laflour

    "At times one remains faithful to a cause only because its opponents do not
    cease to be insipid." (c) Friedrich Nietzsche


    Comment

    • Bruce Wood

      #3
      Re: CSharp problem


      hkhellhkhell@ho tmail.com wrote:
      i have an application that will take user input from a text box and
      write it
      to an access database. i need to make sure that if they ever enter a
      single
      line of text that it has at least 1 space for every 40 characters.
      >
      so before i write the info to the database i have to make sure there is
      no
      lines of text that are longer than 40 characters without a space, and
      if
      there are insert a space at the 41st character. is that as hard as it
      sounds?
      I'm assuming that you want a space every 40 characters on a line. That
      is, that you want a line of 90 non-blank characters to have spaces
      inserted at 40 and at 80, not just at 40.

      string rawText;
      StringBuilder sb = new StringBuilder() ;
      int i = 0;
      int charsWithNoSpac e = 0;
      while (i < rawText.Length)
      {
      if (charsWithNoSpa ce == 41)
      {
      sb.Append(' ');
      charsWithNoSpac e = 0;
      }
      if (rawText[i] == ' ' || rawText[i] == '\n')
      {
      charsWithNoSpac e = 0;
      }
      sb.Append(rawTe xt[i]);
      charsWithNoSpac e = 0;
      i += 1;
      }

      Possible refinements: if you want _any_ whitespace character to count,
      not just space and newline, you could use Char.IsWhitespa ce(rawText[i])
      instead of the explicit test for blank and newline.

      Comment

      • Kevin Spencer

        #4
        Re: CSharp problem

        The following function will do what I believe you want. Note that I used a
        parameter to specify the maximum number of characters before a space, just
        in case you decide to change it from 40.

        public string EnsureSpaces(st ring target, int maxChars)
        {
        StringBuilder result = new StringBuilder() ;
        int charCount = 0;
        for (int i = 0; i < target.Length; i++)
        {
        if (target[i] != ' ') charCount++;
        else charCount = 0;
        if (charCount >= maxChars) result.Append(' ');
        result.Append(t arget[i]);
        }
        return result.ToString ();
        }

        --
        HTH,

        Kevin Spencer
        Microsoft MVP
        Software Composer
        Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.


        A watched clock never boils.

        <hkhellhkhell@h otmail.comwrote in message
        news:1159624797 .857754.37860@e 3g2000cwe.googl egroups.com...
        >i have an application that will take user input from a text box and
        write it
        to an access database. i need to make sure that if they ever enter a
        single
        line of text that it has at least 1 space for every 40 characters.
        >
        so before i write the info to the database i have to make sure there is
        no
        lines of text that are longer than 40 characters without a space, and
        if
        there are insert a space at the 41st character. is that as hard as it
        sounds?
        >
        >
        hkhellhkhell@ho tmail.com
        >

        Comment

        Working...