Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Removing all characters from a string

Hi,

I know how to remove a character: s = s.Remove(s.IndexOf("+"),1);      

++Test++

But what other string function do I use if I want to delete all the '+' signs?

I'd like to say:

while ( s.IndexOf( '+' )  ?? )
    s = s.Remove(s.IndexOf( '+' ),1);      

But I don't know the syntax of IndexOf

Please reply if you know.


Thanks,
Bob
0
ba272
Asked:
ba272
  • 3
  • 3
1 Solution
 
sgvillCommented:
while ( s.IndexOf( '+' )  >= 0)
    s = s.Remove(s.IndexOf( '+' ),1);      

should do it.  IndexOf returns -1 if there are no matches.

Steve
0
 
sgvillCommented:
oops. i cut and paste from yours.  it needs to be a string, so...

while ( s.IndexOf( "+" )  >= 0 )
    s = s.Remove(s.IndexOf( "+"),1);      
0
 
ba272Author Commented:
Does it return -1 when there none found?

Thanks.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
BarthaxCommented:
In your example, it would be quicker to use the Trim('+') method instead... but this would only work continuously if you can guarantee that the string you want to remove is at the start, end or both of the string.

Trim: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemStringClassTrimTopic2.asp?frame=true
Remove: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemStringClassRemoveTopic.asp?frame=true

Personally, I would use the following (untested) code:

i = 0;
while (i < s.Length())
{
    if (s.Chars(i) == '+')
    {
        s.Remove(i);
    } else
    {
        i++;
    }
}
0
 
ba272Author Commented:
Thanks.
0
 
ba272Author Commented:
As far as trim, you're right.  But I made up a phony string ++Test++.  In actuality the single quote symbol I was trying to remove is embedded in an exception mesage.

But thanks.
0
 
sgvillCommented:
yes, IndexOf  returns -1 when no matches are found.  -- Steve
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now