How can we divide the substring from the string
Like I have string
String mainString="///Trade Time///Trade Number///Amount Rs.///";
Now I have other string
String subString="Amount"
Then I want to extract the substring Amount Rs.
with the help of second string named subString not by any other method But it should be extracted through two parameters like first is I have index no of Amount string and second is until the next string ///.
You can extract a substring from a string before a specific character using the rpartition() method. rpartition() method partitions the given string based on the last occurrence of the delimiter and it generates tuples that contain three elements where.
Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.
Other than SUBSTRING() function, MID() and SUBSTR() functions are also used to extract a substring from a string. They both are synonyms of SUBSTRING() function.
Use re.search() to extract a substring matching a regular expression pattern. Specify the regular expression pattern as the first parameter and the target string as the second parameter. \d matches a digit character, and + matches one or more repetitions of the preceding pattern.
First get the index of the substring:
int startIndex = mainString.IndexOf(subString);
Then get the index of the following slashes:
int endIndex = mainString.IndexOf("///", startIndex);
Then you can get the substring that you want:
string amountString = mainString.Substring(startIndex, endIndex - startIndex);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With