Get the Last Character of a String Without Using Array

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:

  1. *s is compared to 0.
  2. s is incremented.
  3. 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.

Get the last character of a string without using array?

Using last to get the last Character

For Swift 4:

let str = "hello worldbr>let lastChar = str.last!   // lastChar is "br>

For Swift 2 and Swift 3:

let str = "hello worldbr>let lastChar = str.characters.last!   // lastChar is "br>

For Swift 1.2:

let str = "hello worldbr>let lastChar = last(str)!   // lastChar is "br>

Subscripting the String to get the last Character

This works for Swift 3 and Swift 4:

let str = "hello worldbr>let lastChar = str[str.index(before: str.endIndex)]   // lastChar is "br>

And for Swift 1.2 and Swift 2:

let str = "hello worldbr>let lastChar = str[str.endIndex.predecessor()]   // lastChar is "br>

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 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];

Why is the last character of a string not captured?

For an array of characters to be treated as a propper string, its last character must be a null terminator (or null byte) '\0'.

The fgets function, in particular always makes sure that this character is added to the char array, so for a size argument of 10 it stores the first 9 caracters in the array and a null byte in the last available space, if the input is larger than or equal to 9.

Be aware that the unread characters, like b in your sample case, will remain in the input buffer stdin, and can disrupt future input reads.

This null byte acts as a sentinel, and is used by functions like printf to know where the string ends, needless to say that this character is not printable.

If you pass a non null terminated array of characters to printf this will amount to undefined behavior.

Many other functions in the standard library (and others) rely on this to work properly so it's imperative that you make sure that all your strings are properly null terminated.

how do u get the last character of the last string in java?

The last string will be at args.length - 1 in the args array, since arrays are 0-based in Java. This is also true in general for any array; the last element is always at array.length - 1.

So you can do:

String lastString = args[args.length - 1];
Character lastCharacter = lastString.charAt(lastString.length() - 1);

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.

last character of character array is getting excluded

You allocated n+1 characters for your array, but then you told getline that there were only n characters available. It should be like this:

int n;
cin>>n;
cin.ignore();

char arr[n+1];
cin.getline(arr,n+1); // change here
cin.ignore();
cout<<arr;


Related Topics



Leave a reply



Submit