?
Solved

Removing all characters from a string

Posted on 2005-03-19
7
Medium Priority
?
228 Views
Last Modified: 2010-04-16
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
Comment
Question by:ba272
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:sgvill
ID: 13583566
while ( s.IndexOf( '+' )  >= 0)
    s = s.Remove(s.IndexOf( '+' ),1);      

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

Steve
0
 
LVL 4

Accepted Solution

by:
sgvill earned 2000 total points
ID: 13583573
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
 

Author Comment

by:ba272
ID: 13583608
Does it return -1 when there none found?

Thanks.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Barthax
ID: 13583686
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
 

Author Comment

by:ba272
ID: 13583805
Thanks.
0
 

Author Comment

by:ba272
ID: 13583815
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
 
LVL 4

Expert Comment

by:sgvill
ID: 13583848
yes, IndexOf  returns -1 when no matches are found.  -- Steve
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question