Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Manipulation.Find string between 2 indexes

Tags:

c#

.net

Given a string eg 500 chars and I want to pick a string between index 400 and index 430. How do you write such a function?

Thanks

like image 503
user9969 Avatar asked Oct 13 '11 15:10

user9969


People also ask

How do I get the string between two indexes?

Use the string. Substring() method. Like var truncString = longString. Substring(400, 30); .

How do I extract a string between two delimiters in Python?

The Python standard library comes with a function for splitting strings: the split() function. This function can be used to split strings between characters. The split() function takes two parameters.

How do you find the index of a specific string?

Java String indexOf() Method The indexOf() method returns the position of the first occurrence of specified character(s) in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character(s) in a string.


2 Answers

Use the string.Substring() method.

Like var truncString = longString.Substring(400, 30);.

Note, that you should check the length of longString that it is at least 430 chars. If it is not, Substring will throw an exception.

like image 187
Albin Sunnanbo Avatar answered Oct 17 '22 03:10

Albin Sunnanbo


string x = "aaaa";
string part = x.Substring(400,Math.Min(x.Length,430)-400);
like image 32
Adilson de Almeida Jr Avatar answered Oct 17 '22 05:10

Adilson de Almeida Jr