Riya
Riya

Reputation: 259

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.

Upvotes: 25

Views: 175429

Answers (10)

Fahad Mughal
Fahad Mughal

Reputation: 151

Just wondering if someone looking for this after all those years.. You can get your desired substring from a string by this line of code.

string text = "Retrieves a substring from this instance. The substring starts at a specified character position, ";
text = text.Substring(text.IndexOf('.') + 1);

Upvotes: 0

akbar
akbar

Reputation: 773

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);

Upvotes: 0

Ali Bayat
Ali Bayat

Reputation: 4096

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

Upvotes: 6

Alex
Alex

Reputation: 1160

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

Enter image description here

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]")

Upvotes: -2

jim tollan
jim tollan

Reputation: 22485

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);
}

Upvotes: 2

as-cii
as-cii

Reputation: 13039

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);

Upvotes: 30

Adrian Serafin
Adrian Serafin

Reputation: 7725

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);

Upvotes: 5

richard
richard

Reputation: 12534

string newString = str.Substring(0, 10);

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

See String.Substring Method.

Upvotes: 23

Vlad Bezden
Vlad Bezden

Reputation: 89745

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.

Upvotes: 0

LifeGuard
LifeGuard

Reputation: 15

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.

Upvotes: 1

Related Questions