|
Hi,
How can I find all the positions of the word hello in string s1?
Note that I do not want to use the built in pattern matching functionality such as:
String::split(), String::indexOf(), String::lastIndexOf(), String::substring(), Regex.Match(),
string s1 = "Hello how are you, hello how are you, hello how are you this morning"
string s2 = "Hello"
The above should return:
1, 20, 39
|
|
|
|
|
|
so write your own little method by walking the string and comparing individual characters.
Extra challenge: make sure you get it right when one or both strings stutter, as in: locating "cucumber" in "I don't like cucucumbers at all".
|
|
|
|
|
This is the C# forum, not C++. You should be asking this in the Managed C++/CLI forum instead.
|
|
|
|
|
Why sir, it is deserted; there is more people on Mount Everest than there is in the C++/CLI forum. And the problem can be solved in any language.
|
|
|
|
|
This is a c# questioin, and apologies I should have placed string.Indexof, etc.
Thanks
|
|
|
|
|
Why do you refuse to use the right tools for the job? If you want to drive a nail, use a hammer. If you want to hit a baseball, use a bat.
Arbitrary restrictions on allowed methods or libraries without explanation smells like "do my homework for me".
But in case it isn't your homework, convert both strings to char arrays. Loop through array 1 until the letter matches the letter in array 2. Increment both indexes and compare, if they match continue incrementing, else reset index on array 2.
|
|
|
|
|
Hello,
string s1 = "Hello how are you, hello how are you, hello how are you this morning"
string s2 = "Hello"
This is what I have now but do not know how to compare one string to the other to see if they match. Can you help please?
Thanks
char[] chars1 = strs1.ToCharArray();
char[] chars2 = strs2.ToCharArray();
for (int i = 0; i < chars1.Length; i++)
{
char letter = chars1[i];
}
|
|
|
|
|
Assume chars2 holds N letters, and chars1 holds at least N.
Now try some index i and check that for all values j in [0,N-1] chars1[i+j] equals chars2[j]; if so, you have a match at position i.
Hence, two nested for loops would be sufficient; it may not be the most efficient way, it is an effective one.
|
|
|
|
|
|
Right. Of course.
But actually, there is no need to explicitly create the char arrays, as a string is perfectly indexable as is: string[i] returns the i-th character all by itself (as it did in C).
|
|
|
|
|
Hi All,
I'm recieving a System.IO.Exception after creating a file and then trying to append text to that file. The message I recieve is:
The process cannot access the file 'File path goes here' because it is being used by another process.
The code to create the file is:
if (!File.Exists(filePath))
File.Create(filePath);
The code I'm using to try and append text to the file is:
StreamWriter sw = File.AppendText(readIDFilePath);
Does any one have any clue to why I am recieving this exception? Does File.Create open the file it's creating?
Is the file already exists then no System.IO.Exception is recieved.
Any feedback would be greatly appreciated.
Thanks,
Mel
|
|
|
|
|
According to the documentation[^], File.Create returns a FileStream that provides read/write access to the file specified in path.
So in answer to your question, yes File.Create does open the file.
Use the returned FileStream to write your data, or call dispose on the filestream before using AppendText.
|
|
|
|
|
Thanks for your feedback, I captured the FileStream that File.Create returns and close/disposed it as you suggested and it works great.
I guess I was just looking at it for too long and over looked the obvious.
Thanks again.
Mel
|
|
|
|
|
You should be able to figure this one out yourself, mainly by reading the documentation on the classes and methods you use.
I would agree with you if you were to say the error message you are getting isn't perfect; when it says "... by another process" that may include your very own process too, so "another process" should really be "some (maybe this) process".
|
|
|
|
|
this file may be opened last time and not be closed.
|
|
|
|
|
right now I have one column header that does not alphabetically sort its self when column header is clicked.
so I figured I can make an event if that column header is clicked to a column header that sorts the it how I need to
Column header Im targeting is gridView.columnFlags
so far I have
private void OnColumnHeaderClick(object sender, Divelements.SandGrid.GridColumnEventArgs e)
{
if (e.GridColumn.Equals(columnTest))
{
}
gms.HandleSort(e.GridColumn);
|
|
|
|
|
As this is a 3rd party control, you are more likely to get help if you ask on their web site. Here[^].
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
Why do programmers often confuse Halloween and Christmas? - Because 31 Oct = 25 Dec.
Business Myths of the Geek #4 'What you think matters.'
|
|
|
|
|
Hello,
I'm using C# in Visual Studio 2008. Is it possible to trap the F1 key in a file dialog (Open or 'Save As')? I've been searching on the internet for the answer to this but I've found none. Any help is appreciated.
Regards,
Mike
|
|
|
|
|
Although I have never done this, my first thought is GetActiveWindow + Global keyboard hook.
|
|
|
|
|
d@nish,
I'm not sure about the GetActiveWindow, but I found a global keyboard hook program. Everything appears to be working now. Thank you for your help.
Regards,
Mike
|
|
|
|
|
Really? You might want to see what happens in your app while its running and the user switches to another app and hits F1.
|
|
|
|
|
Dave,
What happened when you did this?
Regards,
Mike
-- Modified Tuesday, June 15, 2010 11:45 AM
|
|
|
|
|
Remember, that's a GLOBAL keyboard hook, that means SYSTEM GLOBAL, not application. If the users hits that key, while your app is running, in ANY application or Explorer window, it'll fire in your application. Is that what you want it to do??
|
|
|
|
|
It is "global keyboard hook". That will work regardless of the active window. So, if you have a notepad and your application running, and user presses F1 in the notepad, still the code in your application will fire. That is why I said GetActiveWindow + global keyboard hook.
You should check in which window is that F1 key pressed and then process it.
|
|
|
|