Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we extract substring of the string by position and separator

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 ///.

like image 573
Harikrishna Avatar asked Apr 07 '10 09:04

Harikrishna


People also ask

How can you extract a substring from a given 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.

How do you separate parts of a string?

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.

Which function is used to extract a substring?

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.

How do you extract a substring from a string in Python regex?

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.


1 Answers

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);
like image 143
Guffa Avatar answered Oct 02 '22 11:10

Guffa