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 to get the last X Characters of a Golang String?
You can use a slice expression on a string to get the last three bytes.
s := "12121211122"
first3 := s[0:3]
last3 := s[len(s)-3:]
Or if you're using unicode you can do something like:
s := []rune("世界世界世界")
first3 := string(s[0:3])
last3 := string(s[len(s)-3:])
Check Strings, bytes, runes and characters in Go and Slice Tricks.
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).
Finding last character of a string without a function in C
Since ++
is a post-increment operator, this is what happens in the while
loop:
*s
is compared to0
.s
is incremented.- If the comparison succeeds, the loop exits.
When s
reaches the end of the string it's pointing to the null character. It's incremented to point to the character after the null, and then the loop exits. At that point:
*s
= the character after the null, which happens to be the letter O
in your case.
*(s-1)
= the null character.
*(s-2)
= the last character in the string.
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 to get last character of string in c++?
You can use string.back() to get a reference to the last character in the string. The last character of the string is the first character in the reversed string, so string.rbegin() will give you an iterator to the last character.
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];
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'
How to extract the last character of a string of unknown length?
You can get the last character position of a string using string-length
(or rather one less than):
(string-ref str (sub1 (string-length str)))
Note that a character is different from a string of length 1. Thus the correct way to extract a character is with string-ref
or the like, rather than substring
.
Related Topics
Socket.Io 1.X: Use Websockets Only
How to Use Redis Publish/Subscribe with Nodejs to Notify Clients When Data Values Change
Password-Check Directive in Angularjs
Typescript Compile to Single File
When to Use the Double Not (!!) Operator in JavaScript
Why Are Certain Function Calls Termed "Illegal Invocations" in JavaScript
How to Convert from Hex to Ascii in JavaScript
Where Would I Use a Bitwise Operator in JavaScript
Differences Between Contenttype and Datatype in Jquery Ajax Function
The Preferred Way of Creating a New Element with Jquery
How to Make an Angularjs Directive to Stoppropagation
What Is the Meaning of an Underscore in JavaScript Function Parameter
JavaScript || or Operator with an Undefined Variable
How to Use Code from Script with Type=Module