[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String Copy with parameters

Posted on 2006-04-22
5
Medium Priority
?
220 Views
Last Modified: 2010-08-05
Is there a function in c# which copies a string like so:

two integers

int x = 0;
int y = 2;

string test = "whatever";

i want to copy like so:

string one = test(x, y) where x is the starting character and y is the ending character so in this example one would be "wha".

then I want to change x and y

x = 3;
y = 7;

string two = test(x, y) would result in "tever"

The buit in substring function either takes two indexes and copies from first index trough the second index, or one index and copies from begining to the end of the string.  I want to be able to specify the beginning and ending indexes.  Thank you very much.
0
Comment
Question by:nstefanov
  • 3
5 Comments
 
LVL 5

Expert Comment

by:ennixo
ID: 16515144
no but you can make your method :

public string SubString2(string originalString, int startIndex, int endIndex)
{
    return originalString.SubString(startIndex, endIndex - startIndex);
}

very easy.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 1000 total points
ID: 16515158
Existing Substring function has (startIndex, length) parameters. You can write your own function:

string MySubstring(string s, int startIndex, int endIndex)
{
    return s.Substring(startIndex, endIntex - startIndex + 1);
}
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16515170
Hello nstefanov,

the substring is indeed only using starting index and length, but from your description you might do something like

int x = 0;
int y = 2;

string test = "whatever";
string one = test(x, y - x + 1); // results in "wha"

x = 3;
y = 7;

string two = test(x, y - x + 1); // results in "tever"

hope this helps a bit
bruintje
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16515173
shouldn't type that much :)
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16515177
string one = test.substring(x, y - x + 1); // results in "wha"
string two = test.substring(x, y - x + 1); // results in "tever"
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month18 days, 7 hours left to enroll

830 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