String Functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RazSam
    New Member
    • Aug 2007
    • 16

    String Functions

    I am trying to format a .dat file which has "" characters in it, example hello, i would like to return "Hello" only.

    I am trying to use the replace string function but no joy, i have a for loop to read through the text and when it encounters a "" i am trying to delete it as:

    am i doing this the right way?

    for (i =0; i<text.Length;i ++)
    {
    string = text.substring( i,1);
    if (string == "")
    string.replace( "", "");
    }

    Thanks

    Raz
  • DKn
    New Member
    • Aug 2007
    • 53

    #2
    Hai,,

    check this code..
    It may help you.

    string ex="hello ";
    string ex1 = string.Empty;
    for (int i =0; i<ex.Length;i++ )
    {
    if ( ! (ex[i].ToString() == ""))
    {
    ex1 += ex[i];
    }

    }
    MessageBox.Show (ex1.ToString() );


    Originally posted by RazSam
    I am trying to format a .dat file which has "" characters in it, example hello, i would like to return "Hello" only.

    I am trying to use the replace string function but no joy, i have a for loop to read through the text and when it encounters a "" i am trying to delete it as:

    am i doing this the right way?

    for (i =0; i<text.Length;i ++)
    {
    string = text.substring( i,1);
    if (string == "")
    string.replace( "", "");
    }

    Thanks

    Raz

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I would recomend that you read the file in as Byte[] (Since it's a binaryfile?)
      And then use System.Text.Enc oding.ASCII.Get String() on the byte array.
      It should successfully remove all non-standard ascii characters out and return a string with only the text.

      Another soultion would be to take that Byte[] and itterate through each index. If the value is withen allowable ASCII character ranges (Check an ASCII chart) you can cast it as a CHAR and add it to a string.

      Comment

      • RazSam
        New Member
        • Aug 2007
        • 16

        #4
        Originally posted by DKn
        Hai,,

        check this code..
        It may help you.

        string ex="hello ";
        string ex1 = string.Empty;
        for (int i =0; i<ex.Length;i++ )
        {
        if ( ! (ex[i].ToString() == ""))
        {
        ex1 += ex[i];
        }

        }
        MessageBox.Show (ex1.ToString() );

        Thanks DKn, i cant understand these characters are from a Unix .Dat file and i cant ger rid of them

        Comment

        • RazSam
          New Member
          • Aug 2007
          • 16

          #5
          Hi Plater

          Thnaks for that, do you have an example i can look at, i am still a bit confussed.

          Thanks

          Raz





          Originally posted by Plater
          I would recomend that you read the file in as Byte[] (Since it's a binaryfile?)
          And then use System.Text.Enc oding.ASCII.Get String() on the byte array.
          It should successfully remove all non-standard ascii characters out and return a string with only the text.

          Another soultion would be to take that Byte[] and itterate through each index. If the value is withen allowable ASCII character ranges (Check an ASCII chart) you can cast it as a CHAR and add it to a string.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Well, try this:
            Code:
            Byte[] mybuf=new Byte[0];
            
            //insert the code here to read in the .DAT file into a Byte[] 
            //and store it in the mybuf varriable
            
            string mystring=System.Text.Encoding.ASCII.GetString(mybuf,0,mybuf.Length);

            Comment

            • RazSam
              New Member
              • Aug 2007
              • 16

              #7
              That was great thanks sorry about the late reply


              Originally posted by RazSam
              Thanks DKn, i cant understand these characters are from a Unix .Dat file and i cant ger rid of them

              Comment

              Working...