Solved

How can i remove some part of the string

Posted on 2004-08-15
5
230 Views
Last Modified: 2010-04-07
Hi,
I want to take some part of my string apart. Say mystr="how are you today sir"
how can i take "you today" out and keep the rest and set mystr to "how are sir"
Is there a built-in function for that? If not how can i do it?
thanks in advance,
-shane

0
Comment
Question by:e106199
[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
5 Comments
 
LVL 8

Expert Comment

by:shovavnik
ID: 11803463
(If you're using VB.NET, just remove the semicolons at the end of each statement.)

From memory, try the following:

String newstr = mystr.Remove( 8, 10 );

This should remove the "you today" substring.  You may need to check on msdn for the exact parameters for the String.Remove() method.
IIRC, the first is the character index and the second is the number of characters to remove.

Remember that the String methods do not modify the string variable itself, but return the results instead, so you need to assign the value.

If you want to automate this, and you want to remove a substring that the user requests, you can try this:

String removestr = "you today ";
String mystr = "how are you today sir";
String newstr = mystr.Remove( mystr.IndexOf( removestr ), removestr.Length );

HTH
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11803467

How about this,
mystr = mystr.Replace( "you today ", "" )
0
 
LVL 8

Accepted Solution

by:
shovavnik earned 125 total points
ID: 11803561
That works too.

If you want to base it on user input, you can try also:

String removestr = "you today "; // You can get this from the user
String mystr = "how are you today sir";
String newstr = mystr.Replace( removestr, "" );

In this case, you get the same result with Remove and with Replace.

I'm guessing though that the Remove() method is more efficient, because it uses indexes and most likely does fewer textual comparisons.

It's really a question of which is more suitable to your needs.  Will your automation need to replace the text with something other than an empty string in the future?  If so, use Replace().  If not, use Remove().
0
 

Author Comment

by:e106199
ID: 11804289
thank you,
both worked for me
-shane
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 11804300
You're welcome.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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