Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take substring of a given string until the first appearance of specified character?

The question is for perl.

For example if I have "hello.world" and the specified character is '.' then the result I want is "hello".

like image 694
Mihran Hovsepyan Avatar asked Nov 24 '11 11:11

Mihran Hovsepyan


People also ask

How do you get a substring that comes before a certain character?

Use the substring() method to get the substring before a specific character, e.g. const before = str. substring(0, str. indexOf('_')); . The substring method will return a new string containing the part of the string before the specified character.

How do you grab a substring after a specified character?

To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character.

How do you cut a string before a specific character in Python?

Use the split() method to cut string before the character in Python. The split() method splits a string into a list. After listing into the list take only 0 indexed values.


1 Answers

See perldoc -f index:

$x = "hello.world";
$y = substr($x, 0, index($x, '.'));
like image 56
Jim Davis Avatar answered Oct 05 '22 16:10

Jim Davis