473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Remove specific char in string

maylortaylor
72 64KB
Expand|Select|Wrap|Line Numbers
  1. int index = -1;
  2.                 string NewStr = null;
  3.                 char[] lower = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  4.                 char[] upper = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  5.  
  6.                 foreach (char c in str)
  7.                 {
  8.  
  9.                     index = str.IndexOfAny(lower);
  10.                     if (index == -1)
  11.                     {
  12.                         index = str.IndexOfAny(upper);
  13.                     }                    
  14.                         NewStr = str.Remove(index, 1);                   
  15.                 }         
The code above loops through a string and for each character checks to see if it is a lower case character, then checks to see if it an upper case. It then removes it if it is. Leaving only the numbers.

However, it reuses the same string the entire time, never updating the string so it always finds the same (first) character.

Any help on figuring this out?
Jul 5 '13 #1

✓ answered by maylortaylor

Find this online and it did exactly what I wanted

Expand|Select|Wrap|Line Numbers
  1. using System.Text.RegularExpressions;
  2. ...
  3. string newString = Regex.Replace(oldString, "[^.0-9]", "");

6 2187
maylortaylor
72 64KB
ANSWERED MYSELF:

Expand|Select|Wrap|Line Numbers
  1. int index = -1;
  2.                 Boolean flag = false;
  3.                 string NewStr = null;
  4.                 char[] lower = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  5.                 char[] upper = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  6.  
  7.                 foreach (char c in str)
  8.                 {
  9.                     if (flag == true)
  10.                     {
  11.                         str = NewStr;
  12.                     }
  13.                     index = str.IndexOfAny(lower);
  14.                     if (index == -1)
  15.                     {
  16.                         index = str.IndexOfAny(upper);
  17.  
  18.                     }
  19.                     if (index != -1)
  20.                     {
  21.                         NewStr = str.Remove(index, 1);
  22.                         flag = true;
  23.                     }
  24.                 }  
Jul 5 '13 #2
Rabbit
12,516 Expert Mod 8TB
Leaving only the numbers.
That's not what the code does by the way. It leaves everything but letters. If you truly want to leave only numbers, then you need to whitelist the numbers, not blacklist the letters. With the current code, if there is incorrect input with letters, numbers, AND symbols, you'll be left with numbers and symbols.
Jul 5 '13 #3
maylortaylor
72 64KB
stuffing!!!..you are right. Welp, back to the drawing board. Any pseudo code help?
Jul 5 '13 #4
maylortaylor
72 64KB
Expand|Select|Wrap|Line Numbers
  1. char[] whitelist = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
  2.  
  3.  
  4.  
  5.                     for (int i = 0; i < whitelist.Count(); i++)
  6.                     {
  7.                         foreach (char c in str)
  8.                         {
  9.                             if (whitelist.Contains(c))
  10.                             {
  11.                                 NewStr += str.Substring(i, 1);
  12.                             }
  13.                         }
  14.                     }
Well I have this code now, that uses a whitelist of numbers. However, i'm having trouble figuring out the for loops and how to properly configuring them.
Jul 5 '13 #5
maylortaylor
72 64KB
Find this online and it did exactly what I wanted

Expand|Select|Wrap|Line Numbers
  1. using System.Text.RegularExpressions;
  2. ...
  3. string newString = Regex.Replace(oldString, "[^.0-9]", "");
Jul 5 '13 #6
Rabbit
12,516 Expert Mod 8TB
Glad you found a solution, good luck on the rest of your project.
Jul 5 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Andreas Müller | last post by:
hi @all, I have a char* string array and i want to fill it up in a while loop: while(i<3){ length = read(STDIN_FILENO, inputBuffer, sizeof(inputBuffer); ... } after this the array should...
2
by: JIM.H. | last post by:
Hello, I have a string and I want to remove only certain string, if exits. Here is an example: “ABC,FCG,XXX,FFAâ€, I need to remove XXX and keep the rest in the string, how can I do that?...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
14
by: gustavo | last post by:
I was looking at the Sendmail's source code, and i've got confused about this kind of initialization: ------------------------ struct prival PrivacyValues = { { "public", PRIV_PUBLIC }, {...
6
by: Tashfeen Bhimdi | last post by:
I'm trying to remove punctuation from a string with the following code: ---------------------------- #include <string> #include <algorithm> #include <cctype> .. using namespace std ..
5
by: Spoon | last post by:
Hello, I'm writing code to remove .whatever from the end of a string. e.g. hello.world.foo.bar -hello.world.foo The following code seems to work #include <string> #include <iostream>
3
by: anudu | last post by:
I have a string value in a javascript function , as ' var a=abcd?' . I want to remove the '?' char which is at the end of the string. I tried with a.replace(/?/g,''); but it gives a error when...
3
by: CF FAN | last post by:
Hi I have to remove # from a string in coldfusion ..but getting error .can u help me? plzzzzz <cfset narrative="nrr#tive"> <cfoutput>#Replace(narrative, '##',' ', 'ALL')#</cfoutput>
3
by: Okonita via DBMonster.com | last post by:
Hi all, Working on a little shell script to remove specific records from a flat file. I have scanned a one or two script books and googled for answers but have no success. Can someone show me how...
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.