473,788 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find first non-repeated character in a string

How in C#?
Nov 15 '05 #1
3 3333
Please clarify what you mean by non-repeated with a couple of string
examples.

You should be able to use regular expressions along with some
look-ahead/look-behind assertions to find what you are looking for.
--
Justin Rogers
DigiTec Web Consultants, LLC.

"Alex" <a.*******@veri zon.net> wrote in message
news:uc******** ******@TK2MSFTN GP10.phx.gbl...
How in C#?

Nov 15 '05 #2

Hello,
I have done this using C#.NET and collection classes.

private void button1_Click(o bject sender, System.EventArg s e)
{
String str = textBox1.Text.T rim();
function(str);
}

private void function(String str)
{
int a=str.Length;
Hashtable h = new Hashtable();

for(int i=0;i<a;i++)
{
String c = str.Substring(i ,1);
if(h.Contains(c ))
{
int f=GetValue(ref h,c);
h.Remove(c);
h.Add(c,f + 1);
}
else
{
h.Add(c,1);
}
}

for(int i=0;i<a;i++)
{
String g = str.Substring(i ,1);
if(GetValue(ref h,g)==1)
{
textBox2.Text=g ;
break;
}
}
}

private int GetValue(ref Hashtable h, String s)
{
IDictionaryEnum erator de = h.GetEnumerator ();
int d = 0;
while(de.MoveNe xt())
{
if(de.Key.ToStr ing() ==s)
{
d=Convert.ToInt 32(de.Value.ToS tring());
break;
}
}
return d;
--
smart_yupp
-----------------------------------------------------------------------
smart_yuppy's Profile: http://www.msusenet.com/member.php?userid=171
View this thread: http://www.msusenet.com/t-18363

Nov 17 '05 #3
smart_yuppy <sm************ ****@no-mx.msusenet.com > wrote:
I have done this using C#.NET and collection classes.


Note that there is no reason to pass your hashtable reference *by*
reference - and that a somewhat easier to understand version of
str.Substring(i , 1) is str[i] (as a char, rather than a string -
there's no need to create new strings all over the place). Simpler
still though, you can just use a foreach over the string to get each
character in turn.

You're also completely ignoring the whole point of a hashtable - that
you don't have to step through each element of it. You also don't need
to convert the value in the hashtable to a string and back, and you
don't need to remove the previous value if you use the indexer rather
than the Add method. At that point it's not really worth having a
separate method in the first place.

Here's a briefer version of your code:

private void function(String str)
{
Hashtable h = new Hashtable();

foreach (char c in str)
{
if (h.ContainsKey( c))
{
h[c] = ((int)h[c])+1;
}
else
{
h[c] = 1;
}
}

for (int i=0; i < str.Length; i++)
{
char c = str[i];
if (h[c].Equals(1))
{
textBox2.Text = c.ToString();
break;
}
}
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8185
by: Nir.Hazan | last post by:
When I try to update my GridView I get the error: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'Update' that has parameters: CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry, original_OrderID. Details: VS2005 C# website. GridView is bound to a ObjectDataSource1.
26
30937
by: Martin R | last post by:
Hi, How to find first not null value in column whitout chacking whole table (if there is a not null value then show me it and stop searching, the table is quite big)? thx, Martin *** Sent via Developersdex http://www.developersdex.com ***
1
4250
by: tparks69 | last post by:
I get the error: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'Insert' that has parameters: last_name, first_name, email, role_id, user_id, modified_by, modified_date when my application is trying to insert a record from a formview. It works fine for delete, update, but insert just keeps failing. I have removed the OldValuesParameterFormatString="original_{0}" from the
0
4840
by: Dennis | last post by:
I created a table adapter on an xsd that uses 4 basic stored procedures for select, update, insert, delete. Then I created an aspx page with an object data source that uses the table adapter and a formview that is bound to the datasource. Whenever I try to delete a record I get the error: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'Delete' that has parameters
2
4208
by: dave | last post by:
Hi, I have searched for the answer for this error message without success. I have seen the question many times though:) I create an ASP.NET project (VS 2005, C#), and use a very simple .mdf file (which I can provide if necessary). I use 'Add new Item' and pick 'DataSet'. I believe this creates a TypedDataSet, CORRECT? I take all the defaults as far as Insert, and I pick the advanced tab and ask for Update Statements and Delete...
0
1515
by: Bryce | last post by:
I have an ObjectDataSource binding a GridView, with Delete enabled. The following is the declaration (with non-delete stuff left out to save space): <asp:ObjectDataSource ID="CatalogItemsListDataSource" runat="server" DeleteMethod="DeleteCatalogItem" InsertMethod="InsertCatalogItem" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCatalogItemsList" TypeName="CatalogItemManager" UpdateMethod="UpdateCatalogItem">...
9
8848
by: Kernel Bling | last post by:
Hi Everyone, This Saturday the stage was set. The problem simply could not go on existing -- it had to be solved. Many hours, articles, compilations and frustrations later I still did not find an answer. Even pacing around my flat until I nearly went into an altered state of reality proved futile. So here is the problem... When I link up the ObjectDataSource UpdateMethod to a method in the
0
1525
by: rote | last post by:
I have a project i created using TableAdapters(Datasets with .xsd) and everything works well on my PC. But when i create a websetup project and deploy it to another server. I keep getting Error ObjectDatasource cannot find a non generic update method Whats the best way to deploy projects when using DataSets like TableAdapters..
6
1937
by: tshad | last post by:
I have a filename that I want to extract the 1st set of numbers up to either a ".", "-", "_" or "~" and make that an int. Or I guess easier just take all the values that are 0-9 up to the 1st non number value. So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I want to get the 449231 only and make that an integer. Can that be done in one statement?
3
2354
by: pchaitanya | last post by:
I have selected some list of valid dates to a label. now i need to find first day among the given dates from label contrl i got dates from calender control by clicking for entire week.. that is i have dates from sunday to saturday and I have to find which date is sunday......
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.