How do I get the last character of a string?
The code:
public class Test {
public static void main(String args[]) {
String string = args[0];
System.out.println("last character: " +
string.substring(string.length() - 1));
}
}
The output of java Test abcdef
:
last character: f
How can I get last characters of a string
EDIT: As others have pointed out, use slice(-5)
instead of substr
. However, see the .split().pop()
solution at the bottom of this answer for another approach.
Original answer:
You'll want to use the Javascript string method .substr()
combined with the .length
property.
var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"
This gets the characters starting at id.length - 5 and, since the second argument for .substr() is omitted, continues to the end of the string.
You can also use the .slice()
method as others have pointed out below.
If you're simply looking to find the characters after the underscore, you could use this:
var tabId = id.split("_").pop(); // => "Tabs1"
This splits the string into an array on the underscore and then "pops" the last element off the array (which is the string you want).
How to select last two characters of a string
You can pass a negative index to .slice()
. That will indicate an offset from the end of the set.
var member = "my name is Mate";
var last2 = member.slice(-2);
alert(last2); // "te"
How to get the last character of a &str?
That is how you get the last char
(which may not be what you think of as a "character"):
mystring.chars().last().unwrap();
Use unwrap
only if you are sure that there is at least one char in your string.
Warning: About the general case (do the same thing as mystring[-n]
in Python): UTF-8 strings are not to be used through indexing, because indexing is not a O(1) operation (a string in Rust is not an array). Please read this for more information.
However, if you want to index from the end like in Python, you must do this in Rust:
mystring.chars().rev().nth(n - 1) // Python: mystring[-n]
and check if there is such a character.
If you miss the simplicity of Python syntax, you can write your own extension:
trait StrExt {
fn from_end(&self, n: usize) -> char;
}
impl<'a> StrExt for &'a str {
fn from_end(&self, n: usize) -> char {
self.chars().rev().nth(n).expect("Index out of range in 'from_end'")
}
}
fn main() {
println!("{}", "foobar".from_end(2)) // prints 'b'
}
How can I get the last character in a string?
Since in Javascript a string is a char array, you can access the last character by the length of the string.
var lastChar = myString[myString.length -1];
Get last 3 characters of string
Many ways this can be achieved.
Simple approach should be taking Substring
of an input string.
var result = input.Substring(input.Length - 3);
Another approach using Regular Expression
to extract last 3 characters.
var result = Regex.Match(input,@"(.{3})\s*$");
Working Demo
Javascript: Getting last few characters of a string
How to get last 7 characters
Try
"amangupta".slice(-7)
How to get last 4 characters of a string?
Swift 2:
A solution is substringFromIndex
let a = "StackOverFlow"
let last4 = a.substringFromIndex(a.endIndex.advancedBy(-4))
or suffix
on characters
let last4 = String(a.characters.suffix(4))
Swift 3:
In Swift 3 the syntax for the first solution has been changed to
let last4 = a.substring(from:a.index(a.endIndex, offsetBy: -4))
Swift 4+:
In Swift 4 it becomes more convenient:
let last4 = a.suffix(4)
The type of the result is a new type Substring
which behaves as a String
in many cases. However if the substring is supposed to leave the scope where it's created in you have to create a new String
instance.
let last4 = String(a.suffix(4))
How to return the last character of a string in Python?
As shown in the official Python tutorial,
>>> word = 'Python'
[...]
Indices may also be negative numbers, to start counting from the right:
>>> word[-1] # last character
'n'
Related Topics
How to Sequentially Run Promises with Q in JavaScript
Jquery Document.Ready VS Self Calling Anonymous Function
Settimeout Ignores Timeout? (Fires Immediately)
HTML Alignment Issue in One MAChine Only (Both IE8)
HTML "Data-" Attribute as JavaScript Parameter
How to Change Only Text Node in Element
What Are Some Methods to Debug JavaScript Inside of a Uiwebview
Make Checkbox Behave Like Radio Buttons with JavaScript
What Does Three Dots Do in Reactjs
Format Date in a Specific Timezone
How to Stop Event Bubbling on Checkbox Click
How to Run HTML Files Directly from Github, Instead of Just Viewing Their Source