I need to know how to get the last chars of some Strings
get last character of string python
get last character of string c#
get last character of string php
get last character of string c++
substring - last character sql
get last 4 characters of string java
I got this chars DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT
in the case of the first 3 i want the last 4 chars e the case of the last i want the last 3 chars, how can i get them using substr and get them in the correct order eg: RJ11.
You can do this with regular expression matching using
data have; infile datalines; input mystr $ @@; datalines; DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT ; run; data want; set have; suffix = prxchange('s/(DDSP|DDR)(.*)/$2/', 1, mystr); run;
@user667489 is perfect answer if it you have can read all of values separately. if it is in same variable as shown below you can use the same code given by @user667489. and add can add can function. prxnext, can also be used to achieve the same. both examples are shown below
data have; val= "DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT"; run; /* using prxchange with scan*/ data want; set have; suffix = prxchange('s/(DDSP|DDR)//', -1, val); do i = 1 to countw(suffix,' '); newstr= scan(suffix, i); output; end; drop suffix val; run; /* using prxposn*/ data want; length val1 re $200.; set have; start = 1; stop = length(val); re = prxparse('/(DDSP|DDR)/'); set have; call prxnext(re, start, stop, trim(val), position, length); do while (position > 0); val1 = substr(val, position+length, length); call prxnext(re, start, stop, trim(val), position, length); output; end; drop re start stop position length val; run;
Here is how you can do it in a simple python. I assumed that, you want last 4 char of every word except last.
string_1 = 'DDSPRJ11 DDSPRJ12 DDSPRJ12 DDRJCT'
list_string = string_1.split()
new_list = 
for i in range(len(list_string)):
if i == len(list_string) - 1:
output: ['RJ11', 'RJ12', 'RJ12', 'JCT']
Manipulating Characters in a String (The Java™ Tutorials , The len function returns the number of characters in a string: >>> fruit = "banana" To get the last character, we have to subtract 1 from length: length = len(fruit) last To see how this works, let's start with a few examples: >>> "His name is %s. <cfset newstring=Right("Receipt", 2)> This should do it. Check out the string functions section on livedocs to get an idea of what string manipulation functions are available.
- Is the condition of how many suffix characters you want dependent on the prefix characters (
DDR) ? What should be done for rogue values such as
DDREXTREME? Is the focus more concerned with removing the prefix or detecting and extracting a suffix ?