Solved

How can i remove some part of the string

Posted on 2004-08-15
5
224 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
  • 3
5 Comments
 
LVL 8

Expert Comment

by:shovavnik
Comment Utility
(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
Comment Utility

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

Accepted Solution

by:
shovavnik earned 125 total points
Comment Utility
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
Comment Utility
thank you,
both worked for me
-shane
0
 
LVL 8

Expert Comment

by:shovavnik
Comment Utility
You're welcome.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now