Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can i remove some part of the string

Posted on 2004-08-15
5
Medium Priority
?
231 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 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