C# - Counting Occurrences Of A String In An Array And Then Removing Duplicates?

Jan 2, 2010

I am fairly new to C# programming and I am stuck on my little ASP.NET project.

My website currently examines Twitter statuses for URLs and then adds those URLs to an array, all via a regular expression pattern matching procedure. Clearly more than one person will update a with a specific URL so I do not want to list duplicates, and I want to count the number of times a particular URL is mentioned in, say, 100 tweets.

Now I have a List<String> which I can sort so that all duplicate URLs are next to each other. I was under the impression that I could compare list[i] with list[i+1] and if they match, for a counter to be added to (count++), and if they don't match, then for the URL and the count value to be added to a new array, assuming that this is the end of the duplicates.

This would remove duplicates and give me a count of the number of occurrences for each URL. At the moment, what I have is not working, and I do not know why (like I say, I am not very experienced with it all).

With the code below, assume that a JSON feed has been searched for using a keyword into srchResponse.results. The results with URLs in them get added to sList, a string List type, which contains only the URLs, not the message as a whole.

I want to put one of each URL (no duplicates), a count integer (to string) for the number of occurrences of a URL, and the username, message, and user image URL all into my jagged array called 'urls[100][]'. I have made the array 100 rows long to make sure everything can fit but generally, this is too big. Each 'row' will have 5 elements in them.

The debugger gets stuck on the line: if (sList[i] == sList[i + 1]) which is the crux of my idea, so clearly the logic is not working.

Here is sample code:

var sList = new ArrayList();
string[][] urls = new string[100][];
int ctr = 0;
int j = 1;
foreach (Result res in srchResponse.results)
{
string content = res.text;
string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\))+[wd:#@%/;$()~_?+-=\.&]*)";
MatchCollection matches = Regex.Matches(content, pattern);
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
sList.Add(groups[0].Value.ToString());
}
}
sList.Sort();
foreach (Result res in srchResponse.results)
{
for (int i = 0; i < 100; i++)
{
if (sList[i] == sList[i + 1])
{
j++;
}
else
{
urls[ctr][0] = sList[i].ToString();
urls[ctr][1] = j.ToString();
urls[ctr][2] = res.text;
urls[ctr][3] = res.from_user;
urls[ctr][4] = res.profile_image_url;
ctr++;
j = 1;
}
}
}

The code then goes on to add each result into a StringBuilder method with the HTML.Is now edite

View 4 Replies


Similar Messages:

Removing Duplicates In List?

Dec 16, 2010

i have list<player> with duplicates.

i need to create another list<player> from the first list <player> with out any duplicates.

View 3 Replies

Forms Data Controls :: Replacing All Occurrences Of A String With Another String?

Dec 15, 2010

i have a gridview and i want all occurences of a particular string inside the gridview to be replaced with another string.

View 5 Replies

Can Avoid Duplicates From Array (in C#)

Feb 15, 2010

i want to avoid duplicate entries from array.....(in c#)

View 2 Replies

Find # Of Occurrences & Count In String Using Collections / Generics?

Mar 4, 2010

I tried so much working with this to get distinct Words and their count. I'm using ASP.NET 2.0. And i also googled so much.

1)Ex(String): The DOTNET is Very Cool! The Best.

2) Capture all the sequences like Space, FullStop, Question Mark, Exclamation, Apostrophe, New Line....(May be with RegEx?)

3)Split Words According to the above sequences

4)Get Distinct Words(Occurences) and their count based on the input string in 1st statement.

Desired O/P:
The - 2
DOTNET - 1
Is - 1
Very - 1
Cool - 1
Best - 1

This is my Sample Code for your Ref.

[Code]....

View 3 Replies

DataSource Controls :: Removing Duplicates From Two Similar Columns Or Any Other Columns In A Database Schema

Apr 21, 2010

I have two columns first name and last name , that have duplicates, how can i remove the duplicates and only leave the distinct members intact?

View 6 Replies

How To Add <br> In String After Counting Particular Characters

Jan 25, 2011

I want to display description of product in gridview, but i want to display only 15 characters on one line, I want to break it after 15 characters, I have written countchar function as follows:

public int CountChars(string value)
{
bool lastWasSpace = false;
foreach (char c in value)
{
result++;
lastWasSpace = false;
}
return result;
}

and called function as:

string description="sdfsdfsd sdfsdf sdfsdf asdfsa dfsda safsaf sdfdf sdfs sdfsdf sdff sdf ";

CountChars(description);

And i want to check:

if(result>15)
{
after every 15 characters i want to break the line.
}

View 3 Replies

WCF / ASMX :: Removing A Number From Array List With Web Service?

Mar 25, 2010

I need to pass a random number from 1 to 100 from the client to a webservice, and then the webservice will subtract this number from the list of 100 numbers (say 1-100). Then, in the next call, the next random number will be subtracted from the rest 99 numbers and so on.

I need this to be done with AJAX. I don't know where the Array list should be held in the first place (would it be in the web service or in the code behind? ), and how this can be done?

View 2 Replies

Finding Duplicates In String And Display?

Aug 4, 2010

Let say I have a string like this

Dim Mystring as String = "Dogs;cats;Dogs;apple;cars;dogs;cats"

I want to only find the values that is in the string more then once....and then create a new string

like Result Dim NEWstring as String = "Dogs;cats"

View 9 Replies

String Manipulation - Removing Last Few Characters

Feb 11, 2011

When I try to remove the last few characters of a string, I get an index out of range error. I am using the following to remove the characters from the end of the string: objJSONStringBuilder.Remove(objJSONStringBuilder.Length - 1, 6) The string has <hr /> at the end which I want to remove.

View 3 Replies

Web Forms :: How To Split The String To String Array

Jan 21, 2010

I would like to split the string to the string array.

but when the string hasn't has the splite sperator, i can't put the string into string array directly.

how to do?

if (Str.Contains(','))
{
str_array=Str.Split(',');
}
else
{
str_array=Str; <-- error occurs at this line.
}

View 3 Replies

DataSource Controls :: Removing Whitespace In String Not Working?

Jan 20, 2010

I am accumulating a string for a SQL IN clause, so the format needs to be WHERE var IN ('string1','string2','string3',etc)

The strings are coming from a multiline textbox where they are pasted in from the user (one per line).

For some reason, I always get a space right after the tick and before the value in every value after the first. For instance my DataReader is only returning one record because the input string turns out like this:

SELECT * FROM Table
WHERE ID IN ('12345',' 23456',' 34567')

Here is how I am processing the string, and I can't see why it is not taking that space out.

[Code]....

Even if I move the replace space (line 2) to the end of the code block, it still wont trim it out...

View 3 Replies

SQL Server :: Removing Matching String In Multiple Rows?

Dec 8, 2010

I am looking for a better way to update a list of records. I have a string column in my table that contains an array of strings like so:"11112,AAAA1,AAAA2,99998" etc. It is possible that the column could contain one of those values at any position, for instance, these are ALL valid values for the column:

"11112"
"AAAA1,11112"
"11112,AAAA1"
"AAAA1,11112,AAAA2"

I want to write a SQL statement that will remove "11112" from all of the columns when needed. This MUST also remove any leading, trailing, or double comma values that may be left from simply replacing the "11112" with an empty string.As of now, I simply load a SqlDataReader with any records that contain "11112", I replace it with nothing, "", then I fix the string if need be and update it.

View 3 Replies

Configuration :: Removing Login Account Without Removing User?

May 31, 2010

Long ago, I created an ASPNET user for development use.However, every time I boot up my dev system, I'm presented with a user login for ASPNET, among others.I don't want to remove ASPNET; I need it for dev work.But how do I keep it from appearing among the list of User Logins available at boot-up?

View 4 Replies

SQL Server :: Count Occurrences In Bridge Table?

Nov 22, 2010

Im looking for a sql solution for counting records in a bridge table. I want to select all in table1 and add a column with count of each author's titles. Ive tried

SELCET Table1(*), COUNT(DISTINCT table.au_id) AS Titles

but keep getting an error.

The tables I have are as follows:

Table1(
au_id
au_fname
au_lname
ect..)
Table2(
au_id
title_id)
Table3(
title_id
title_name)

View 4 Replies

Convert From String To Int From Array In C#

Dec 23, 2010

strring[] arritem={"1"};
int i=convert.Toint32(arritem[0]);

now error will throwed.how to slove this problem?

View 4 Replies

How To Get The Last Chars In A String Array

Sep 22, 2010

I am having trouble with parsing a srtring, let me figure out :

This is my string (being gathered from DB) : students/notes/8293/location/michael.jpg

and i want to parse "michael.jpg" from this string to be able to process it.

View 3 Replies

Convert Byte Array Into String?

Feb 17, 2010

i need the to convert the byte array in to string. The byte array is a two dimentional array. Is it possible to convert?

View 3 Replies

How To Add A String To Array With Multiple Item

Apr 7, 2010

I got an array:

[Code]....

In arrHeader, I wanted to add a string value from arrRemark(1), with seperator ",". How can I do that?

View 6 Replies

MVC :: Validation For String Array In Model?

Mar 25, 2011

I am using an array to keep track what checkboxes a user has clicked.

[Code]....

However, validation doesn't seem to work for this field. This is how I am rendering it in the view:

[Code]....

[Code]....

[Code]....

View 3 Replies

Converting Byte Array To String?

Apr 16, 2010

I am trying to upload a file to the database using the File Upload control. The database column is of type "text"

In the Insert command, I tried to retrieve the file and pass, but as it is a byte array, I am not able to save the data.

[Code]....

How to convert the byte array to string?

View 9 Replies

C# - Moving From String Array Into Datarow

Dec 22, 2010

I have a problem with assigning string array into Datarow. Firstly, i have created the object for string array and put 2 values in the array out of 100(whole size). How many values should be placed in the array dependds on a different, which i am not showing here, though.

Then i tried converting into DataRow. But it says. "object reference not set to an instance of an object"

DataRow dr = null;
string[] strconcat = new string[100];
dr["concat"] = strconcat[i];

Edit-- Actually i was trying put these string array values into dropdown (ddchooseadeal).

[Code]....

View 5 Replies

C# - Add Contents Of Arraylist To String Array?

Jan 11, 2011

In for each loop i am adding the contents into ArrayList. Now i need to add (or copy/move) the contents of arraylist into string array.

By string array i mean string[].

View 4 Replies

Group List And Get A Count Of The Number Of Occurrences Sorted Descending

Feb 8, 2011

I have a list of ids properly stored in a List<>. I now need to Group list and get a count of the number of occurrences sorted descending.

Example:

List<string> aryIDs = new List<string>;
aryIDs.Add("1234");
aryIDs.Add("4321");
aryIDs.Add("3214");
aryIDs.Add("1234");
aryIDs.Add("4321");
aryIDs.Add("1234");

Would Produce:

"1234", 3
"4321", 2
"3214", 1

This would be easy in TSQL, but I would like to avoid the server roundtrip, unnecessary tables, etc. if possible.

Update: The VB.NET conversion for Ralph Shillington's answer below:

[code]....

View 2 Replies

Return Array From Comma Separated String?

Mar 8, 2011

Suppose I have the string like String sample="{K,S},{T}". Then splitting with "," I need the output of the string array. The string array is having items {k,S}, {T}. how to split the above string with "," separated.

View 7 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved