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:
Please ignore any syntax errors besides the line I'm having trouble with (I converted it from VB)
RichTextBox rtbox1 = new RichTextBox;
OpenFileDialog ofd = new OpenFileDialog;
.FileName = "";
.Filter = "Rich Text Format|*.rtf";
if (.ShowDialog == DialogResult.OK)
System.IO.StreamWriter out = System.IO.File.CreateText("Temp.rtf");
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.
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:
Hope this helps.:)
static void Main()
String str1 = "this} is} a} test}";
ReplaceLastInstanceOf(ref str1, "}", ""); // replace the final } with nothing
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);
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.
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.
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.
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...
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
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:
That temporarily converts variable "i" from long to int. Not sure if it's the same in vb.net.
Array.IndexOf(A.Lines, "MarkerText", (int)i)
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.
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.
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.
Originally Posted by kairozamorro
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.
Maximum int size is 4,294,967,295. (0xFFFFFFFF)
That's maximum unsigned int, surely?
Originally Posted by theoobe
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...