Click here to Skip to main content
15,891,900 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
"The best time to start is now. Some time gen-\r\n
erally means no time. You can put away as much as I put away, if you try."\r\n
" All right, I’ll do it—next week."\r\n
" No, this week," and Dale smiled good-hu-\r\n
moredly.\r\n
"Gracious, Dale! are you becoming my guar-\r\n
dian.\r\n


In the string above I need to find the gen-\r\n, and move up the rest of the word erally to join them together to make gen-erally. Same with good-hu- and guar- I can use the substring of -\r\n to find them but I cannot find a way to go past the first substring.

What I have tried:

I need a find next or something. I have searched Google for a way to do it but have not found what I am looking for. I seem to remember seeing something before but cannot think of it or find it now. I have tried using
while (SaveWordIndex != -1);
but that will not work, it is never -1.

Sorry, here is my code if it helps.
foreach (KeyValuePair<string, string> record in MyOCR.ToList())
            {
                string v1 = record.Key;
                string v2 = record.Value;


                pageString = v2;
                SaveWordIndex = pageString.IndexOf("-" + "\r\n");
                if (SaveWordIndex != -1)
                {
                    SaveWords1 = pageString.Substring(0, SaveWordIndex);
                    pageString = pageString.Remove(0, SaveWords1.Length);
                    SaveWordIndex = pageString.IndexOf(" ");
                    if (SaveWordIndex != -1)
                    {
                        SaveWords2 = pageString.Substring(0, SaveWordIndex);
                        SaveWords2 = SaveWords2.Remove(0, 3);  // remove the -\r\n
                        SaveWords3 = SaveWords1 + SaveWords2 + CrLf;
                        pageString = pageString.Remove(0, SaveWordIndex + 1);

                        richTextBox1.Text = SaveWords3 + pageString;
                    }
                }
Posted
Updated 12-Apr-23 13:12pm
v2
Comments
PIEBALDconsult 12-Apr-23 11:48am    
Use a Regular Expression.
Member 14711713 12-Apr-23 19:05pm    
Wish I knew Regx, perhaps now is the time to start learning it.

Use the String.IndexOf Method (System) | Microsoft Learn[^], which allows you to specify the starting position to seach from. You could also use String.Split Method (System) | Microsoft Learn[^] to separate into an array of strings delineated by the "-\r\n". You then recombine the resultant strings to get the results you want.

[edit]
You could also use the String.Replace Method (System) | Microsoft Learn[^].
[/edit]
 
Share this answer
 
v2
Comments
Member 14711713 12-Apr-23 19:09pm    
Looks like I will have to use the split method to make this work. I was hoping to avoid that way of doing this, so messy.
You could just use string.Replace:
C#
string str = "\"The best time to start is now. Some time gen-\r\n"
            +"erally means no time. You can put away as much as I put away, if you try.\"\r\n"
            +"\" All right, I’ll do it—next week.\"\r\n"
            +"\" No, this week,\" and Dale smiled good-hu-\r\n"
            +"moredly.\r\n"
            +"\"Gracious, Dale! are you becoming my guar-\r\n"
            +"dian.\"\r\n";
str = str.Replace("-\r\n", "-");
Console.WriteLine(str);

"The best time to start is now. Some time gen-erally means no time. You can put away as much as I put away, if you try."
" All right, I’ll do it—next week."
" No, this week," and Dale smiled good-hu-moredly.
"Gracious, Dale! are you becoming my guar-dian."
 
Share this answer
 
v2
Comments
Member 14711713 12-Apr-23 19:06pm    
Didn't work for me. Still only changes the first instants of the problem.
Member 14711713 12-Apr-23 21:19pm    
oops, error. I got it to work
OriginalGriff 13-Apr-23 0:31am    
Glad to hear it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900