String Replace Function

This is a discussion on String Replace Function within the C# Programming forums, part of the General Programming Boards category; I have a need to merge two rtf files and have discovered its possible (a much simpler way than I've ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    49

    String Replace Function

    I have a need to merge two rtf files and have discovered its possible (a much simpler way than I've been seeing from others and codeproject) by appending the contents of file B to file A but before File A's closing }. I've done some testing to see if I could output File A to a test file minus the last } in the file, so I could than write in File B plus the closing } for file A, but the string replace function isnt working for me:

    Code:
                RichTextBox rtbox1 = new RichTextBox;
                OpenFileDialog ofd = new OpenFileDialog;
                with ofd
                {
                    .FileName = "";
                    .Filter = "Rich Text Format|*.rtf";
                    if (.ShowDialog == DialogResult.OK)
                    {
                        rtbox1.LoadFile(.FileName);
                        System.IO.StreamWriter out = System.IO.File.CreateText("Temp.rtf");
                        out.WriteLine(rtbox1.Rtf.ToString.Replace(rtbox1.Rtf.ToString.LastIndexOf("}"), "@"));
                        out.Close();
                    }
                }
    Please ignore any syntax errors besides the line I'm having trouble with (I converted it from VB)

    The problem to be specific is with this line: out.WriteLine(rtbox1.Rtf.ToString.Replace(rtbox1.R tf.ToString.LastIndexOf("}"), "@"))

    In the example I'm just testing it by replacing the last } with a @ but ideally I'd just want to remove it.
    Last edited by kairozamorro; 08-20-2009 at 02:32 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    LastIndexOf will return the index position of your query as an integer (or returns -1 if no match is found). The Replace function asks to replace a string with a string, but what you are doing in your code is replacing an integer with a string.

    I'll show you one way to do what you're looking for. It might not be the best way, and perhaps someone knows a better way, but I suggest you cut your string into 2 substrings and then join them together with your required alteration.

    Here's a demo to explain what I mean:

    Code:
    using System;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main()
            {
                String str1 = "this} is} a} test}";
                ReplaceLastInstanceOf(ref str1, "}", ""); // replace the final } with nothing
                Console.Write(str1);
                Console.Read();
            }
    
            static void ReplaceLastInstanceOf(ref String _str, String _old, String _new)
            {
                int i = _str.LastIndexOf(_old);
    
                if (i > -1) // if it's -1 then there was no instances found
                    _str = _str.Substring(0, i) + _new + _str.Substring(i + _old.Length);
            }
        }
    }
    Hope this helps.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    49
    You're right on with substring. I wasnt paying attention to lastindexof returning a int. To simplify things wouldn't this work?

    out.WriteLine(rtfbox1.Rtf.ToString.SubString(rtfbo x1.Rtf.ToString.Replace(rtfbox.Rtf.ToString.LastIn dexOf("}"), "@"), 1));

    I think i'll go ahead and try it. Still feel free to comment if you've got any other suggestions of course. Thanks for your help.
    Last edited by kairozamorro; 08-20-2009 at 10:08 PM. Reason: Had functions in wrong order....

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    49
    Ok, I think I got a better solution here. Instead of writing/reading to/from file I well just use the contents as is from a richtextbox. For loading (reading) from file I'm having to ReadToEnd() to get the file contents into the richbox. The program's input well contain more than just RTF formatted data though, so my next problem...

    I have two RichTextBoxes. One contains the contents of the file thats been read in, the other well contain only the RTF data like I want.

    To get everything to work, I need to be able to find the next instance of a string that signifies the end of the RTF data and have the function return not the index for where the next instance begins, but rather the line on which it was found in the RichTextBox control. If this is possible what function do I need to use? The return result well be used in a constrainedcopy to only get the RTF data for a paticular item from the one RichTextBox containing the contents of the file to the RichTextBox that'll be dedicated to RTF only data.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    49
    Did some google searching, think GetLineFromCharIndex() function of RichTextBox is going to help me get out of this one (I'll be trying it later), but anyone's got any other approaches to tackling the problem I'm all ears. Its complex I know but I need it this way so everything the program needs is in one file.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    49
    Ok... maybe not. Problem is I can't specify the index to start looking at. This is a problem cause the value being looked for may appear several times. I need the index to the next instance of found string starting at a specified index...

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    49
    Well was hoping Array.IndexOf would be my ticket, since you can tell it where to start from. To update everyone code wise this is what I'm trying to do:

    Array.ConstrainedCopy(A.Lines, i, B.Lines, 0, Array.IndexOf(A.Lines, "MarkerText", i) - i)

    The goals I'm trying to achieve here:
    • A and B are both RichTextBoxes. RTB A contains the contents of the file. I need to get the RTF contents from a range of RTB A's lines to RTB B (RTF data only). I start at the current index i which is the line after a beginning marker I use to identify that RTF data is coming its way, and want to go all the way up to the last line of the first occurance of "MarkerText" found after line i.

    Okay, surely someone knows at least if I'm using the functions the wrong way? I'm just typing in what intellisense is asking for but I'm still getting errors. The paticular error I'm getting is at the Array.IndexOf function (no errors elesewhere for the moment but we'll see):

    Error 1 Overload resolution failed because no accessible 'IndexOf' can be called without a narrowing conversion:
    'Public Shared Function IndexOf(Of String)(array() As String, value As String, startIndex As Integer) As Integer': Argument matching parameter 'startIndex' narrows from 'Long' to 'Integer'.
    'Public Shared Function IndexOf(array As System.Array, value As Object, startIndex As Integer) As Integer': Argument matching parameter 'startIndex' narrows from 'Long' to 'Integer'. M:\Projects\Visual Studio\Sample - Copy\Sample\Sample.vb 69 113 Sample

  8. #8
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Looking at your exception message, it seems that variable "i" is a long rather than an int, but Array.IndexOf is expecting an int.

    Is variable "i" a long? If so try something like:

    Code:
    Array.IndexOf(A.Lines, "MarkerText", (int)i)
    That temporarily converts variable "i" from long to int. Not sure if it's the same in vb.net.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    49
    The program handles long to hold more information, suppose I can trim it down to int if it'll fix this. I'll report back with update later, thanks again.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    49
    Ok, used CInt(i) and sure enough no errors. Don't know if it'll do what I want (well need to some testing) but right now with no more errors things are looking up. I'll be converting all instances of i to int now.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    49
    Anyone know of a builtin function that can take in a long though? The file being read in can contain just about anything in its rtf data, making it possible for the file to have thousands of lines so the need comes into play being able to access an index beyond the capabilities of an integer.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by kairozamorro View Post
    Anyone know of a builtin function that can take in a long though? The file being read in can contain just about anything in its rtf data, making it possible for the file to have thousands of lines so the need comes into play being able to access an index beyond the capabilities of an integer.
    I don't see any.

    Thousands of lines is not an issue -- int goes up to 2 billion, and at the "usual" 80 characters to a line that's a mere 25 million lines. I'm pretty sure your array can't have more than 2 billion characters in it either.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Maximum int size is 4,294,967,295. (0xFFFFFFFF)


  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by theoobe View Post
    Maximum int size is 4,294,967,295. (0xFFFFFFFF)

    That's maximum unsigned int, surely?

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    49
    4 trillions or 2 billions the max for a 32-bit int? I thought it was something like 32k. Ok than, it should be enough, pending some testing of course. The test file I had earlier was already up to 70k in lines though...
    Last edited by kairozamorro; 08-27-2009 at 12:22 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Using the string replace function
    By FoodDude in forum C++ Programming
    Replies: 7
    Last Post: 09-23-2005, 02:31 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21