'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.
Solution 1:[1]
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);
Solution 2:[2]
string newString = str.Substring(0, 10);
will give you the first 10 characters (from position 0 to position 9).
Solution 3:[3]
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);
Solution 4:[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);
}
Solution 5:[5]
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.
Solution 6:[6]
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
Solution 7:[7]
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.
Solution 8:[8]
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);
Solution 9:[9]
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]")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow