Reputation: 259
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
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
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
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
Reputation: 1160
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]")
Upvotes: -2
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
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
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
Reputation: 12534
string newString = str.Substring(0, 10);
will give you the first 10 characters (from position 0 to position 9).
Upvotes: 23
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
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