Get the Pre-Last Element of a String Split by Spaces

Get the pre-last element of a string split by spaces

You can use the overload for Split() and pass the RemoveEmptyEntires enum:

string input = "7 9 12 16 18 21 25 27 30 34 36 39 43 45 48 52 54 57 61 ";
var splitInput = input.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
var inputInt = Convert.ToInt32(splitInput[splitInput.Length - 2]);
// inputInt is 57

Doing it this way allows your last element to actually be what you want.

Fiddle here

Get characters before last space in string

You can do

var sentence = "Your message is too large";
var lastWord = sentence.split(' ').pop()

Result : "large"

Or with a regular expression :

var lastWord = sentence.match(/\S*$/)[0]

How to split a string in shell and get the last field

You can use string operators:

$ foo=1:2:3:4:5
$ echo ${foo##*:}
5

This trims everything from the front until a ':', greedily.

${foo  <-- from variable foo
## <-- greedy front trim
* <-- matches anything
: <-- until the last ':'
}

Split a string with powershell to get the first and last element

By using String.split() with the count parameter to manage dashes in the commitid:

$x = "0.3.1-15-g3b885c5"
$tag = $x.split("-",3)[0]
$commitid = $x.split("-",3)[-1]

Explode string on second last and last space to create exactly 3 elements

You can approach this using explode and str_replace

$string = "Testing (3WIR)";
$stringToArray = explode(":",str_replace("(",":(",$string));
echo '<pre>';
print_r($stringToArray);

Edited question answer:-

$subject = "Fluency in English Conversation Defklmno (1WIR)";
$toArray = explode(' ',$subject);
if(count($toArray) > 2){
$first = implode(" ",array_slice($toArray, 0,count($toArray)-2));
$second = $toArray[count($toArray)-2];
$third = $toArray[count($toArray)-1];
$result = array_values(array_filter([$first, $second, $third]));
}else{
$result = array_values(array_filter(explode(":",str_replace("(",":(",$subject))));
}

DEMO HERE

Split a string and get the second last word

What you need is String lastWord = bits[bits.length-2]; because bits[bits.length-1]; will return you the last word and not the second last.

This is because indexing of array starts with 0 and ends in length-1.

Here is the updated code snippet:

String string = "Hai,Hello,How,are,you";
String[] bits = string.split(",");
String lastWord = bits[bits.length - 2];
tvs.setText(lastWord);

How to get the second last string in C#

Step 1: You can Split the String using space delimeter to get all words from String.

Step 2: You can use the WordsLength-2 to get the 2'nd word from Last.

Try This:

string input = "Village Siaban  WDT no.39 91308 Semporna Sabah";

var words = input.Split(' ');
var reqWord = "";
if(words.Length > 1)
reqWord = words[words.Length-2];

Splitting strings in Oracle SQL by last white space before given index

You may need to split the words up into 3:

WITH bounds ( value, start_pos, end_pos ) AS (
SELECT value,
1,
INSTR( SUBSTR( value, 1, 33 ), ' ', -1 )
FROM table_name
UNION ALL
SELECT value,
end_pos + 1,
CASE
WHEN LENGTH( value ) - end_pos <= 32
THEN end_pos
ELSE end_pos + INSTR( SUBSTR( value, end_pos + 1, 33 ), ' ', -1 )
END
FROM bounds
WHERE end_pos > start_pos
)
SELECT value,
CASE
WHEN end_pos < start_pos
THEN SUBSTR( value, start_pos )
ELSE SUBSTR( value, start_pos, end_pos - start_pos )
END AS substring,
ROW_NUMBER() OVER ( PARTITION BY value ORDER BY start_pos ) AS idx
FROM bounds
ORDER BY value, idx;

Which for the sample data:

INSERT ALL
INTO table_name VALUES ( 'This string is 32 charact. long. This string is 31 charac. long.' )
INTO table_name VALUES ( 'This string is 31 charac. long. This string is 32 charact. long.' )
INTO table_name VALUES ( 'This string is 30 chara. long. This string is 30 chara. long. 2c' )
INTO table_name VALUES ( 'This string is 29 char. long. This string is 29 char. long. 4chr' )
INTO table_name VALUES ( 'Some Random Very Long Comapany Name Co. Ltd.' )
SELECT * FROM DUAL;

Outputs:


VALUE | SUBSTRING | IDX
:--------------------------------------------------------------- | :------------------------------- | --:
Some Random Very Long Comapany Name Co. Ltd. | Some Random Very Long Comapany | 1
Some Random Very Long Comapany Name Co. Ltd. | Name Co. Ltd. | 2
This string is 29 char. long. This string is 29 char. long. 4chr | This string is 29 char. long. | 1
This string is 29 char. long. This string is 29 char. long. 4chr | This string is 29 char. long. | 2
This string is 29 char. long. This string is 29 char. long. 4chr | 4chr | 3
This string is 30 chara. long. This string is 30 chara. long. 2c | This string is 30 chara. long. | 1
This string is 30 chara. long. This string is 30 chara. long. 2c | This string is 30 chara. long. | 2
This string is 30 chara. long. This string is 30 chara. long. 2c | 2c | 3
This string is 31 charac. long. This string is 32 charact. long. | This string is 31 charac. long. | 1
This string is 31 charac. long. This string is 32 charact. long. | This string is 32 charact. long. | 2
This string is 32 charact. long. This string is 31 charac. long. | This string is 32 charact. long. | 1
This string is 32 charact. long. This string is 31 charac. long. | This string is 31 charac. long. | 2


Update

I'm aware of that I will only split those that second part after splitting will be no longer than 32, which is 90% of cases. Otherwise I will split it exactly in a half. Splitting into 3 fields is not possible in that case.

In light of the clarified requirements:

You can use:

SELECT CASE
WHEN len - split_point <= 32
THEN SUBSTR( value, 1, split_point - 1 )
ELSE RTRIM( SUBSTR( value, 1, 32 ) )
END AS first_substring,
CASE
WHEN len - split_point <= 32
THEN SUBSTR( value, split_point + 1 )
ELSE LTRIM( SUBSTR( value, 33 ) )
END AS second_substring
FROM (
SELECT value,
INSTR( SUBSTR( value, 1, 33 ), ' ', -1 ) AS split_point,
LENGTH( value ) AS len
FROM table_name
)

Which, for the sample data above, outputs:


FIRST_SUBSTRING | SECOND_SUBSTRING
:------------------------------- | :-------------------------------
This string is 32 charact. long. | This string is 31 charac. long.
This string is 31 charac. long. | This string is 32 charact. long.
This string is 30 chara. long. T | his string is 30 chara. long. 2c
This string is 29 char. long. Th | is string is 29 char. long. 4chr
Some Random Very Long Comapany | Name Co. Ltd.

db<>fiddle here

jQuery - split string by symbol and get last part

There are a few means of doing this, the basics are to find the last part and then use that last part:

// iterate over each <span>:
$("span").each(function() {
// find the text of the current <span>, split
// on the '/' characters, and retrieve that last
// part:
var kodBanky = $(this).text().split("/").pop();

// append a newly created <p> element to the
// <body> element:
$('<p/>', {
'text' : kodBanky
}.appendTo('body');
});