How Can Get a Substring from a String in C#?

How Can Get a Substring from a String in C#?

I have a large string and it’s stored in a string variable, str. And I want to get a substring from that in C#.

Suppose the string is: " Retrieves a substring from this instance. The substring starts at a specified character position, "

The substring result what I want to display is: The substring starts at a specified character position.

1

9 Answers

You could do this manually or using the IndexOf method.

Manually:

int index = 43;
string piece = myString.Substring(index);

Using IndexOf, you can see where the full stop is:

int index = myString.IndexOf(".") + 1;
string piece = myString.Substring(index);
2
string newString = str.Substring(0, 10);

will give you the first 10 characters (from position 0 to position 9).

See String.Substring Method.

0

Here is an example of getting a substring from the 14th character to the end of the string. You can modify it to fit your needs.

string text = "Retrieves a substring from this instance. The substring starts at a specified character position.";

// Get substring where 14 is the start index
string substring = text.Substring(14);
4

Making the assumption that you want to split on the full stop (.), then here's an approach that would capture all occurrences:

// Add @ to the string to allow split over multiple lines
// (for display purposes to save the scroll bar from 
// appearing on a Stack Overflow question :))
string strBig = @"Retrieves a substring from this instance.
            The substring starts at a specified character position. great";

// Split the string on the full stop, if it has a length>0
// then, trim that string to remove any undesired spaces
IEnumerable<string> subwords = strBig.Split('.')
    .Where(x => x.Length > 0).Select(x => x.Trim());

// Iterate around the new 'collection' to sanity check it
foreach (var subword in subwords)
{
    Console.WriteLine(subword);
}
string text = "Retrieves a substring from this instance. The substring starts at a specified character position. Some other text";

string result = text.Substring(text.IndexOf('.') + 1,text.LastIndexOf('.')-text.IndexOf('.'))

This will cut the part of string which lays between the special characters.

A better solution using index in C# 8:

string s = " Retrieves a substring from this instance. The substring starts at a specified character position, ";

string subString = s[43..^2]; // The substring starts at a specified character position
var data =" Retrieves a substring from this instance. The substring starts at a specified character position.";

var result = data.Split(new[] {'.'}, 1)[0];

Output:

Retrieves a substring from this instance. The substring starts at a specified character position.

All answers used the main string that decrease performance. You should use Span to have better performance:

var yourStringSpan = yourString.AsSpan();
int index = yourString.IndexOf(".") + 1;
string piece = yourStringSpan.slice(index);

It's easy to rewrite this code in C#...:

This method works if your value is between two substrings!

For example:

stringContent = "[myName]Alex[myName][color]red[color][etc]etc[etc]"

The calls should be:

myNameValue = SplitStringByASubstring(stringContent, "[myName]")

colorValue = SplitStringByASubstring(stringContent, "[color]")

etcValue = SplitStringByASubstring(stringContent, "[etc]")
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.