|
-
Apr 17th, 2007, 05:05 PM
#1
Thread Starter
Addicted Member
looping thru a string to remove letters
im trying to loop thru a string one letter at a time and get rid of all letters aqnd symbols except "."
and for some reason the string.remove(x,x) doesnt work....
it just stays on the same letter and doesnt rip it out...
like say i put abc123def into my pricerange textbox it would lopp over and over removing "a" but not really removing it...
heres my code
I'm always using
Visual Studio 2003 c#
I'm always working on
web applications.
c# Code:
bool ifd;
string currentchar;
string deletechar;//set inside of another method as the value which goes into the database...(but this isnt the prob ill get to this later)
public void DelChar()
{
for(int i=0; i<txtPriceRangeSpecialAddNew.Text.ToString().Length;)
{
if (ifd == false)
{
currentchar=deletechar.Substring(i,1);
ifd=true;
}
else
{
currentchar=currentchar;
}
int x=0;
try
{
Convert.ToInt32(currentchar);
deletechar ="$"+deletechar+currentchar;
x++;
}
catch
{
if (currentchar==".")
{
x++;
}
else
{
try
{
Convert.ToInt32(currentchar);
x++;
}
catch
{
currentchar.Remove(i,1);
}
}
}
}
}
-
Apr 17th, 2007, 05:58 PM
#2
Re: looping thru a string to remove letters
Have you stepped through your code and watched the variables as you go? That's the way to understand how your code actually works.
Having said that, all you need to do is loop through the current string and if you don't want to remove the current character then add it to a new string. Once you're done the new string will contain all the characters you want to keep.
-
Apr 17th, 2007, 06:15 PM
#3
Thread Starter
Addicted Member
Re: looping thru a string to remove letters
yes
the only thing i saw out of place was currentchar which would stay on the first letter and
wont remove it
-
Apr 17th, 2007, 07:20 PM
#4
Re: looping thru a string to remove letters
Have you read the documentation for String.Remove to see whether you're using it correctly? You always need to keep in mind that String objects are immutable, i.e. once you create one you cannot change it. If you need to make changes you need to create a whole new String object. That's why it would be much more efficient to simply build a new string one character at a time using a StringBuilder, rather than removing one character at a time from an existing string.
-
Apr 18th, 2007, 06:10 AM
#5
Re: looping thru a string to remove letters
You look like you're trying to create a validator. Have you considered using a Regular Expression for this?
-
Apr 18th, 2007, 06:33 AM
#6
Re: looping thru a string to remove letters
currentchar = currentchar.Remove(i,1);
I don't live here any more.
-
Apr 18th, 2007, 12:30 PM
#7
Thread Starter
Addicted Member
Re: looping thru a string to remove letters
i use Sting.Remove(i,1) and i got it to remove a out of "abc123def" in a string value, but it wont move on from there.
-
Apr 18th, 2007, 12:48 PM
#8
Thread Starter
Addicted Member
Re: looping thru a string to remove letters
i got it to work
c# Code:
string deletechar;// this is the string which you assing the variable or param which is used by the textbox in the Update_button.click event
string currentchar;
public void DelChar()
{
for(int i=0; i<deletechar.Length;)
{
currentchar=deletechar.Substring(i,1);
try
{
Convert.ToInt32(currentchar);
i++;
}
catch
{
if (currentchar==".")
{
i++;
}
else
{
try
{
Convert.ToInt32(currentchar);
i++;
}
catch
{
currentchar = deletechar.Remove(i,1);
deletechar=currentchar;
}
}
}
}
}
the value gets tossed around like a hot potato
currentchar = deletechar.Remove(i,1);
deletechar=currentchar;
the highlighted is the value of the string after the letter is removed
and it just keeps on removing
but i also ran into another prob on my way...
i was using x as an int for some reason intead of i in my loop so when it reached the number it didnt remove the letters on the right side....
Last edited by KingSatan; Apr 18th, 2007 at 12:57 PM.
Reason: messed up
-
Apr 18th, 2007, 04:09 PM
#9
Thread Starter
Addicted Member
Re: looping thru a string to remove letters
i was wondering... is there any wildcard in c#???
like if i were to say
c# Code:
if(deletechar==".."+wildcard){do this code;}
or something like that....
-
Apr 18th, 2007, 08:52 PM
#10
Re: looping thru a string to remove letters
Code:
string originalString = "abc12a3defa";
char[] charsToRemove = {'a'};
StringBuilder finalString = new StringBuilder();
foreach (char ch in originalString)
{
if (Array.IndexOf(charsToRemove, ch) == -1)
{
// This is a valid character so add it to the output.
finalString.Append(ch);
}
}
MessageBox.Show(finalString.ToString());
If you want to use something like a wild card then you should take penagate's advice and use a Regex.
-
Apr 19th, 2007, 04:03 PM
#11
I wonder how many charact
Re: looping thru a string to remove letters
I thought he wanted to remove all characters except the one specified (by the way he worded his post).
Regardless, Array.IndexOf involves boxing... which is costly. Best to just use a byte array, you will achieve 60% more efficiency.
Code:
public static string RemoveAny(string source, char[] charsToRemove)
{
byte[] sourceBytes = System.Text.Encoding.Default.GetBytes(source);
byte[] charBytesToRemove = System.Text.Encoding.Default.GetBytes(charsToRemove);
byte[] tempBuffer = new byte[sourceBytes.Length];
int resultLength = 0;
bool matchFound = false;
for (int i = 0; i < sourceBytes.Length; i++)
{
matchFound = false;
for (int f = 0; f < charBytesToRemove.Length; f++)
{
if (sourceBytes[i] == charBytesToRemove[f])
{
matchFound = true;
break;
}
}
if (! matchFound)
{
tempBuffer[resultLength] = sourceBytes[i];
resultLength++;
}
}
return System.Text.Encoding.Default.GetString(tempBuffer,0, resultLength);
}
Last edited by nemaroller; Apr 19th, 2007 at 04:15 PM.
Reason: forgot the ! operator
-
Apr 19th, 2007, 04:50 PM
#12
Thread Starter
Addicted Member
Re: looping thru a string to remove letters
do any of you know how t oshow a dollar sign and commas (if the number is over 1000.00) without storing them in the database?
-
Apr 19th, 2007, 04:54 PM
#13
I wonder how many charact
Re: looping thru a string to remove letters
Code:
double money = 1231123.12;
Console.WriteLine(money.ToString("C"));
-
Apr 19th, 2007, 04:58 PM
#14
Thread Starter
Addicted Member
Re: looping thru a string to remove letters
its a c#.net 2003 web app
-
Apr 19th, 2007, 05:00 PM
#15
I wonder how many charact
Re: looping thru a string to remove letters
Code:
double money = 1231123.12;
someWebTextBox.Text = money.ToString("C");
-
Apr 19th, 2007, 05:16 PM
#16
Thread Starter
Addicted Member
Re: looping thru a string to remove letters
how about showing it in the datagrid without having to store the $'s and ,'s in the DB?
-
Apr 20th, 2007, 07:20 AM
#17
I wonder how many charact
Re: looping thru a string to remove letters
Well, depends on how your binding the data of course:
For a bound column, you must make sure your results are returning as a double:
Code:
<asp:boundcolumn DataFormatString="{0:C}" DataField="amount"></asp:boundcolumn>
If you are using a template column:
Code:
<asp:templatecolumn>
<itemtemplate>
<%# ((double)((System.Data.DataRowView)Container.DataItem)["fieldNameofAmount"]).ToString("C") %>
</itemtemplate>
</asp:templatecolumn>
If you are handling the Datagrid.ItemDataBound event,
then you can simply use the code in my previous post and assign it to whatever control is necessary.
See this thread for a little more info:
http://www.vbforums.com/showpost.php...96&postcount=4
Last edited by nemaroller; Apr 20th, 2007 at 07:54 AM.
-
Apr 20th, 2007, 05:26 PM
#18
Thread Starter
Addicted Member
Re: looping thru a string to remove letters
well i have this and it works fine but doesnt add the dollars or commas....
which is all im really trying to do....
but i dont want them sotered into the DB
HTML Code:
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Price Range">
<ItemTemplate>
<asp:Label runat="server" ID="PriceRangeS" Text='<%# ((double)((System.Data.DataRowView)Container.DataItem)["chrPriceRange"]).ToString("{0:c}") %> '>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtPriceRangeS" Width="104px" Text='<%# DataBinder.Eval(Container, "DataItem.chrPriceRange") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
-
Apr 20th, 2007, 08:27 PM
#19
Re: looping thru a string to remove letters
This DataGrid currency stuff has nothing to do with the original question and should have been asked in a different thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|