Microsoft word didn't provide a spelling tool for my language and I want to do it with my self, every this is going fine and there is a small problem I need to solve.
In order to spell check for each words I need to get each words first. obviously if I used
var doc = Globals.ThisAddIn.Application.ActiveDocument;
int counts = doc.Words.Count;
List<string> words=new List<string>;
for (int i=0;i<counts;i++)
{
words.Add(doc.Words[i].Text.Trim());
}
to get each words, it will run very slowly.
after that I write my own method to get each words, it runs good. after that I need to mark the wrong spelled words. I used
bool check = diccheck(word);
if (check == false)
{
doc.Words[i].Font.Underline = Word.WdUnderline.wdUnderlineWavy;
doc.Words[i].Font.UnderlineColor = Word.WdColor.wdColorRed;
}
and it is run very slow again. can any one help me show the way how to mark the wrong spelled words quickly?
What do you think about a thread runs while you writes?
Are you trying to correct wrong words?
If i get it right, may be more usefull if you only checks the text that changed.
Related
I am facing following problem: I have to change a certain part of URL if it contains a specific match ("siteassets") and transform it into different word ("syssiteassets"). This particular word which needs to be replaced can occur at various order in the URL, so for example once it can be "example.com/siteassets/title/index" and different time it can be: "example.com/de/items/siteassets/title/index".
I have tried my luck with pretty simple approach:
if (e.UrlBuilder.Path.Contains("siteassets") && (e.UrlBuilder.Path.Contains(".pdf") || e.UrlBuilder.Path.Contains(".dwg")))
{
e.UrlBuilder.Path = e.UrlBuilder.Path.Replace("siteassets", "syssiteassets");
}
...but since this if statement is in the middleware method through which requests run multiple times, the once changed string goes from "syssiteassets" to "syssyssiteassets".
What is the best way to deal with this? I cannot use REGEX (not my decision).
is "siteassets" always surrounded by "/" ? I hope else you might have weird bugs.
So if yes, why not just :
e.UrlBuilder.Path = e.UrlBuilder.Path.Replace("/siteassets/", "/syssiteassets/");
With the following code, I delete line by line in a TextBox (0, 39). Now there is on the last place a money amount (1 any Articel 10.00) which I want to deduct from the total amount. For that, I use the Substring. But there I get errors, as probably the empty spaces are not interpreted. Is there a simple solution to this? Thanks
private void btnDelete_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Length > 0)
{
txtTotal.Text = (Double.Parse(txtTotal.Text) - Double.Parse(TextBox1.Text.Substring(8, 2))).ToString("0.00");
TextBox1.Text = TextBox1.Text.Remove(0, 39);
}
if (TextBox1.Text.Length == 0)
{
MessageBox.Show("The cart is empty");
Few things you can do to make your life easier (assuming you have to keep a TextBox as you have stated to others.)
Before I get into the details however, the issue seems to be you're having trouble parsing text that represents lines of data, data which contains amounts which you want to act on. If this is an incorrect assumption, disregard this answer.
Ok, back to it...
Rather than trying to work with the text directly in the TextBox, start by reading in your entire string as a list of lines (i.e. List<String>). You can do that with the Split function or with RegEx expressions. See here
Use RegEx expressions for each line to identify not just the type of line it is (an 'item' line or the 'All' line at the bottom) as well as the various parts of those lines. For instance, you can use a RegEx that starts at the end of the line and goes backwards looking for a number (in the form of a string.) Use the result of that for your Parse method to get the actual numeric value.
Finally, if you still need to remove the lines of text (I'm not sure if you're removing the text just for your logic or if you need to display it) simply remove them from your list of strings for the lines. If it needs to be displayed back in the UI (doubtful as it seems it should be blank at the end of processing) just use Join to convert the lines back to a string, then set that back to the TextBox.Text property.
Hope this helps!
Mark
P.S. To (try and) avoid comments such as the ones you got about your design, it may help to start your question by saying something like 'Unfortunately I'm restricted to using a TextBox due to issues outside of this question, hence I'm looking for an answer here.' At least that should cut back on those responses telling you to 'Do it differently!' instead of answering your question.
I seem to be having a very weird problem and I really have no idea what's going on. This is the source code I'm trying to debug
StorageFile file = await roamingFolder.GetFileAsync(filename);
string text = await FileIO.ReadTextAsync(file);
string[] shows = text.Split(new[] { ":?;" }, StringSplitOptions.None);
List<show> showslist = new List<show>();
foreach (string splitshow in shows)
{
string[] show1 = splitshow.Split(new[] { ",?;" }, StringSplitOptions.None);
episode = show1[0];
name = show1[1];
showslist.Add(new show { name = name, episode = episode });
}
Output.ItemsSource = showslist;
The weird thing is that the list is shown only if I put Output.ItemsSource = showslist; inside of the foreach loop but not when it's outside and I really don't understand why it's not. I mean elements of the list have already been added to it haven't they??
Have tried many different methods and most of them even if they did show the list data had many different problems that are too messy to fix.
Anyway appreciate any hint or help, thank you.
I'll bet that your data isn't exactly correct. I think one of the later/last entries is throwing an exception and it's being swallowed higher up in your code (or not being reported to your logger/UI). It never completes the foreach loop and exits the method before you can assign your data source.
I would guess that your second split, one of the entries does not actually contain your delimiter ,?; so the show1 array is only of length 1 and does not contain a "name" entry. When you try to access show1[1], it throws an IndexOutOfRangeException.
As an aside, might I suggest that you investigate using simpler delimiters, or better yet, utilize some form of XML (or JSON, or other) serialization for reading your data.
EDIT: I see from your posted code sample in your comments, that the issue is the last entry. Given hlj,?;lljhjh:?;hhmm,?;drr:?;oo,?;hello:?;ff,?;ff:?;, your first String.Split operation on :?; will yield an empty string as the last entry. Thus when you try to perform your second split against ,?; it splits against empty and returns an array with a single entry of String.Empty. When you hit show1[1] the exception is thrown.
If you change your first split to use StringSplitOptions.RemoveEmptyEntries it should eliminate the empty entry:
string[] shows = text.Split(new[] { ":?;" }, StringSplitOptions.RemoveEmptyEntries);
If you like, you can add a check like if (show1.Length == 2) then you can avoid bad data (but perhaps you would prefer to report that so you can fix it). If your program is writing this bad data itself, perhaps you should make a couple quick unit tests to ensure that you're always writing/reading valid data.
I need to compare a value in a string to what user typed in a richtextbox.
For example: if a richtextbox holds string rtbText = "aaaka" and I compare this to another variable string comparable = "ka"(I want it to compare backwards). I want the last 2 letters from rtbText (comparable has only 2 letters) to be replaced with something that was predetermined(doesn't really matter what).
So rtbText should look like this:
rtbText = "aaa(something)"
This doesn't really have to be compared it can just count letters in comparable and based on that it can remove 2 letters from rtbText and replace them with something else.
UPDATE:
Here is what I have:
int coLen = comparable.Length;
comparable = null;
TextPointer caretBack = rtb.CaretPosition.GetPositionAtOffset(coLen, LogicalDirection.Backward);
TextRange rtbText = new TextRange(rtb.CaretPosition, caretBack);
string text = rtbText.Text;
rtbText returns an empty string or I get an error for everything longer than 3 characters. What am I doing wrong?
Let me elaborate it a little bit further. I have a listbox that holds replacements for values that user types in rtb. The values(replacements) are coming from there, meaning that I don't really need to go through the whole text to check values. I just need to check the values right before caret. I am comparing these values to what I have stored in another variable (comparable).
Please let me know if you don't understand something.
I did my best to explain what needs to be done.
Thank you
You could use Regex.Replace.
// this replaces all occurances of "ka" with "Replacement"
Regex replace = new Regex("ka");
string result = replace.Replace("aaaka","Replacemenet");
gumenimeda, I had similar problems few weeks ago. I found my self doing the following (I asume you will have more than one occurance in the RichTextBox that you will need to change), note that I did it for Windows Forms where I have access directly to the Rtf text of the control, not quite sure if it will work well in your scenario:
I find all the occurancies of the string (using IndexOf for example) and store them in a List for example.
Sort the list in descending order (max index goes first, the one before him second, etc)
Start replacing the occurancies directly in the RichTextBox, by removing the characters I don't need and appending the characters I need.
The sorting in step 2 is necessary as we always want to start from the last occurance going up to the first. Starting from the first occurance or any other and going down will have an unpleasant surprise - if the length of the chunk you want to remove and the length of the chunk you want to append are different in length, the string will be modified and all other occurancies will be invalid (for example if the second occurance was in at position 12 and your new string is 2 characters longer than the original, it will become 14th). This is not an issue if we go from the last to the first occurance as the change in string will not affect the next occurance in the list).
Ofcourse I can not be sure that this is the fastest way that can be used to achieve the desired result. It's just what I came up with and what worked for me.
Good luck!
First of all, I did a search on this and was able to find how to use something like String.Split() to extract the string based on a condition. I wasn't able to find however, how to extract it based on an ending condition as well. For example, I have a file with links to images: http://i594.photobucket.com/albums/tt27/34/444.jpghttp://i594.photobucket.com/albums/as/asfd/ghjk6.jpg
You will notice that all the images start with http:// and end with .jpg. However, .jpg is succeeded by http:// without a space, making this a little more difficult.
So basically I'm trying to find a way (Regex?) to extract a string from a string that starts with http:// and ends with .jpg
Regex is the easiest way to do this. If you're not familiar with regular expressions, you might check out Regex Buddy. It's a relatively cheap little tool that I found extremely useful when I was learning. For your particular case, a possible expression is:
(http://.+?\.jpg)
It probably requires some more refinement, as there are boundary cases that could trip this up, but it would work if the file is a simple list.
You can also do free quick testing of expressions here.
Per your latest comment, if you have links to other non-images as well, then you need to make sure it doesn't start at the http:// for one link and read all the way to the .jpg for the next image. Since URLs are not allowed to have whitespace, you can do it like this:
(http://[^\s]+\.jpg)
This basically says, "match a string starting with http:// and ending with .jpg where there is at least one character between the two and none of those characters are whitespace".
Regex RegexObj = new Regex("http://.+?\\.jpg");
Match MatchResults = RegexObj.Match(subject);
while (MatchResults.Success) {
//Do something with it
MatchResults = MatchResults.NextMatch();
}
In your specific case, you could always split if by ".jpg". You will probably end up with one empty element at the end of the array, and have to append the .jpg at the end of each file if you need that. Apart from that I think it would work.
Tested the following code and it worked fine:
public void SplitTest()
{
string test = "http://i594.photobucket.com/albums/tt27/34/444.jpghttp://i594.photobucket.com/albums/as/asfd/ghjk6.jpg";
string[] items = test.Split(new string[] { ".jpg" }, StringSplitOptions.RemoveEmptyEntries);
}
It even get rid of the empty entry...
The following LINQ will separate by http: and make sure to only get values that end with jpg.
var images = from i in imageList.Split(new[] {"http:"},
StringSplitOptions.RemoveEmptyEntries)
where i.EndsWith(".jpg")
select "http:" + i;