How to search for a string data in Cosmos!!?

May 21, 2012 at 7:21 PM

Hey,

I wanted to ask how to search for a string data in Cosmos, for example I have a text "cosmos on codeplex" so I want to check if the word "codeplex" exists.

I looked at a function in PearOS but it only enables to search for one char.

 

help!!!

May 21, 2012 at 7:33 PM
Edited May 21, 2012 at 7:36 PM
ahmed01000410657 wrote:

Hey,

I wanted to ask how to search for a string data in Cosmos, for example I have a text "cosmos on codeplex" so I want to check if the word "codeplex" exists.

I looked at a function in PearOS but it only enables to search for one char.

 

help!!!

That would be easy. Just do something like this. Sorry it wasn't in there. I only add string functions when I need them, and a word search was something I didn't need yet. Hope it helps - Matt. Sorry the code is not in C# format, like a code block. I wrote it off the top of my head. It should work, I bet I have a few syntax errors, so you might want to line by line add it in.

public bool Search(string word, bool iscase) {

string[] str = word.split(' ');

for (int i = 0; i < str.length; i++) {

   if (str[i].ToLower() == word.ToLower() && iscase == false) {

          return true;

 }

 else if (str[i] == word && iscase == true) {

return true;

}

}

return false;

}

 

*EDIT OOPS FOUND A BUG, FIXED NOW.

May 21, 2012 at 8:04 PM

I got these errors

 

Error    2    Argument 1: cannot convert from 'string' to 'char[]' 

The best overloaded method match for 'string.Split(params char[])' has some invalid arguments 

May 21, 2012 at 8:06 PM

Thanks I handled it.

 

But I have and error says "Not all code returns a value"??

May 21, 2012 at 8:18 PM

and I don't know how to use it.

I don't know where to add the text I want to search in and the text I want to search for.

Thanks man.

Coordinator
May 21, 2012 at 9:41 PM

This is going to exhaust your ram very quickly....

Op 21 mei 2012 21:33 schreef "civilwarrock" <notifications@codeplex.com> het volgende:

From: civilwarrock

ahmed01000410657 wrote:

Hey,

I wanted to ask how to search for a string data in Cosmos, for example I have a text "cosmos on codeplex" so I want to check if the word "codeplex" exists.

I looked at a function in PearOS but it only enables to search for one char.

help!!!

That would be easy. Just do something like this

public bool Search(string word, bool iscase) {

string[] str = word.split(' ');

for (int i = 0; i < str.length; i++) {

if (str[i].ToLower() == word.ToLower() && iscase == false) {

return true;

}

else if (str[i] == word && iscase == true) {

return false;

}

}

return false;

}

Read the full discussion online.

To add a post to this discussion, reply to this email (Cosmos@discussions.codeplex.com)

To start a new discussion for this project, email Cosmos@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

May 21, 2012 at 10:34 PM

You don't understand me, I have a sentence and it's "cosmos project on codeplex.com", So I want a function to tell me is the word "project" does exist in the sentence or no, That what I really want.

May 21, 2012 at 11:07 PM
ahmed01000410657 wrote:

You don't understand me, I have a sentence and it's "cosmos project on codeplex.com", So I want a function to tell me is the word "project" does exist in the sentence or no, That what I really want.

Try this. Hope it helps - Matt

public static bool Search(string str, string word, bool iscase) {
//If case is set to true, then caps matter. If not, then it doesn't //matter.
//str, is the sentence, and word is the word you are looking for.

string str2 = str.split(' ');
for (int i = 0; i < str2.Length; i++) {

if (iscase == true && str2[i] == word) {
return true;
}
else if (iscase == false && str2[i].ToLower() == word.ToLower()) {
return true;
}

}
return false;
}

That should work. If not let me know, I am kind of tired, I have a cold, and again this is off the top of my head. But it should work!

 

Developer
May 22, 2012 at 12:54 PM
Edited May 23, 2012 at 12:47 PM

You don't need to split the string.

Also, there are is another way to do this that is a bit more complicated but searches faster.
The difference should only really be noticed if the search string is long, and there are a lot
of almsot matches. As I just implemented this one from scratch in 5 or so minutes, (at most),
this is just a basic algorithm.

To do a case insensitive search, you'd have to modify theis code slightly. (mostly converting characters (not the strings, as that would eat away memory) to either lower or uppercase before comparing them)

Also, this code doesn't check for the bounds, so a partial match at the end of a string could cause issues.

This code should do what you need. (and not use anthing other than the stack to do it's search)

/// <summary>
/// Checks if the given string contains the
/// specified string.
/// </summary>
/// <param name="str">The string to search in.</param>
/// <param name="searchString">The string to search for.</param>
/// <returns>True if the string exists in the specified string.</returns>
public static bool String_Contains(string str, string searchString)
{
	for (int i = 0; i < str.Length; i++)
	{
		if (str[i] == searchString[i])
		{
			for (int i2 = 1; i2 < searchString.Length; i2++)
			{
				if (str[i + i2] != searchString[i2])
				{
					goto Continue;
				}
			}
			return true;
		}
	Continue:
		continue;
	}
	return false;
}