Solved

How can i remove some part of the string

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

710 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