PDA

Click to See Complete Forum and Search --> : looping thru a string to remove letters


KingSatan
Apr 17th, 2007, 05:05 PM
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.

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);
}
}
}
}

}

jmcilhinney
Apr 17th, 2007, 05:58 PM
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.

KingSatan
Apr 17th, 2007, 06:15 PM
yes
the only thing i saw out of place was currentchar which would stay on the first letter and
String.Remove(x,1);
wont remove it

jmcilhinney
Apr 17th, 2007, 07:20 PM
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.

mendhak
Apr 18th, 2007, 06:10 AM
You look like you're trying to create a validator. Have you considered using a Regular Expression for this?

wossname
Apr 18th, 2007, 06:33 AM
currentchar = currentchar.Remove(i,1);

KingSatan
Apr 18th, 2007, 12:30 PM
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.

KingSatan
Apr 18th, 2007, 12:48 PM
i got it to work



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....
:D

KingSatan
Apr 18th, 2007, 04:09 PM
i was wondering... is there any wildcard in c#???

like if i were to say
if(deletechar==".."+wildcard){do this code;}

or something like that....

jmcilhinney
Apr 18th, 2007, 08:52 PM
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.

nemaroller
Apr 19th, 2007, 04:03 PM
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.

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);
}

KingSatan
Apr 19th, 2007, 04:50 PM
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?

nemaroller
Apr 19th, 2007, 04:54 PM
double money = 1231123.12;
Console.WriteLine(money.ToString("C"));

KingSatan
Apr 19th, 2007, 04:58 PM
its a c#.net 2003 web app

nemaroller
Apr 19th, 2007, 05:00 PM
double money = 1231123.12;
someWebTextBox.Text = money.ToString("C");

KingSatan
Apr 19th, 2007, 05:16 PM
how about showing it in the datagrid without having to store the $'s and ,'s in the DB?

nemaroller
Apr 20th, 2007, 07:20 AM
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:
<asp:boundcolumn DataFormatString="{0:C}" DataField="amount"></asp:boundcolumn>

If you are using a template column:

<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?p=1996296&postcount=4

KingSatan
Apr 20th, 2007, 05:26 PM
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


</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>


:ehh:

jmcilhinney
Apr 20th, 2007, 08:27 PM
This DataGrid currency stuff has nothing to do with the original question and should have been asked in a different thread.