• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

String Copy with parameters

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
nstefanov
Asked:
nstefanov
  • 3
1 Solution
 
ennixoCommented:
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
 
AlexFMCommented:
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
 
bruintjeCommented:
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
 
bruintjeCommented:
shouldn't type that much :)
0
 
bruintjeCommented:
string one = test.substring(x, y - x + 1); // results in "wha"
string two = test.substring(x, y - x + 1); // results in "tever"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now