Solved

How can i remove some part of the string

Posted on 2004-08-15
5
227 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 50
Initial get page response times and IIS8 15 55
What can cause the styling on a .NET site to not be found? 17 29
Testing of the web services 1 35
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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