Share remove last character from string c#

dovette

New member
#C ##String #Remove #Character

## Cách xóa ký tự cuối cùng khỏi chuỗi trong C#

Trong C#, bạn có thể xóa ký tự cuối cùng khỏi một chuỗi bằng phương thức `String.Remove ()`.Phương thức này có hai tham số: chuỗi được sửa đổi và chỉ mục của ký tự sẽ được xóa.Chỉ số của ký tự cuối cùng trong một chuỗi là độ dài của nó trừ một.

Ví dụ: mã sau sẽ loại bỏ ký tự cuối cùng khỏi chuỗi `" Hello World "`:

`` `C#
Chuỗi str = "Hello World";
str = str.remove (str.length - 1);

// str bây giờ chứa chuỗi "Xin chào worl"
`` `

Bạn cũng có thể sử dụng phương thức `String.SubString ()` để xóa ký tự cuối cùng khỏi chuỗi.Phương pháp này lấy hai tham số: Chỉ số bắt đầu của chuỗi con sẽ được trả về và độ dài của chuỗi con.Để xóa ký tự cuối cùng khỏi chuỗi, bạn sẽ chuyển độ dài của chuỗi dưới dạng tham số thứ hai.

Ví dụ: mã sau sẽ loại bỏ ký tự cuối cùng khỏi chuỗi `" Hello World "`:

`` `C#
Chuỗi str = "Hello World";
str = str.subString (0, str.length - 1);

// str bây giờ chứa chuỗi "Xin chào worl"
`` `

## ví dụ

Dưới đây là một số ví dụ về cách xóa ký tự cuối cùng khỏi chuỗi trong C#:

* Để xóa ký tự cuối cùng khỏi chuỗi bằng phương thức `String.Remove ()`, bạn sẽ sử dụng mã sau:

`` `C#
Chuỗi str = "Hello World";
str = str.remove (str.length - 1);
`` `

* Để xóa ký tự cuối cùng khỏi chuỗi bằng phương thức `String.subString ()`, bạn sẽ sử dụng mã sau:

`` `C#
Chuỗi str = "Hello World";
str = str.subString (0, str.length - 1);
`` `

* Để xóa ký tự cuối cùng khỏi chuỗi bằng mảng `char`, bạn sẽ sử dụng mã sau:

`` `C#
char [] chars = "hello world" .toararray ();
chars [chars.length - 1] = '\ 0';
`` `

## Phần kết luận

Xóa ký tự cuối cùng khỏi chuỗi trong C# là một tác vụ đơn giản có thể được thực hiện bằng cách sử dụng các phương thức `String.Remove ()` hoặc `String.SubString ()`.Cả hai phương pháp đều đơn giản để sử dụng và tạo ra cùng một kết quả.
=======================================
#C# #String #Remove #Character

## How to Remove the Last Character from a String in C#

In C#, you can remove the last character from a string using the `String.Remove()` method. This method takes two parameters: the string to be modified and the index of the character to be removed. The index of the last character in a string is its length minus one.

For example, the following code removes the last character from the string `"Hello World"`:

```c#
string str = "Hello World";
str = str.Remove(str.Length - 1);

// str now contains the string "Hello Worl"
```

You can also use the `String.Substring()` method to remove the last character from a string. This method takes two parameters: the start index of the substring to be returned and the length of the substring. To remove the last character from a string, you would pass the string's length as the second parameter.

For example, the following code removes the last character from the string `"Hello World"`:

```c#
string str = "Hello World";
str = str.Substring(0, str.Length - 1);

// str now contains the string "Hello Worl"
```

## Examples

Here are some examples of how to remove the last character from a string in C#:

* To remove the last character from a string using the `String.Remove()` method, you would use the following code:

```c#
string str = "Hello World";
str = str.Remove(str.Length - 1);
```

* To remove the last character from a string using the `String.Substring()` method, you would use the following code:

```c#
string str = "Hello World";
str = str.Substring(0, str.Length - 1);
```

* To remove the last character from a string using the `char` array, you would use the following code:

```c#
char[] chars = "Hello World".ToCharArray();
chars[chars.Length - 1] = '\0';
```

## Conclusion

Removing the last character from a string in C# is a simple task that can be accomplished using the `String.Remove()` or `String.Substring()` methods. Both methods are straightforward to use and produce the same result.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top