Làm cách nào để viết thường một chuỗi ngoại trừ chữ cái đầu tiên JavaScript?

Vấn đề viết hoa một chuỗi khá phổ biến và đã được thảo luận nhiều lần. Nhưng đôi khi, chúng ta có thể gặp một vấn đề như thế này, trong đó chúng ta cần chuyển đổi ký tự đầu tiên của chuỗi thành chữ thường. Hãy để chúng tôi thảo luận về những cách nhất định trong đó điều này có thể được thực hiện.  

Phương pháp số 1. Sử dụng cắt chuỗi + Lower() Tác vụ này có thể dễ dàng được thực hiện bằng cách sử dụng phương thức Lower để viết thường các ký tự được cung cấp cho nó và cắt có thể được sử dụng để thêm chuỗi còn lại sau ký tự đầu tiên viết thường.  

Python3




# Python3 code to demonstrate working of

# Lowercase first character of String

# Using lower() + string slicing

 

# initializing string

test_str

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
1

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
2

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4_______0_______5
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
7
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8

 

# Using lower() + string slicing

# Lowercase first character of String

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
1
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3_______0_______4
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
5
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
9

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4_______0_______3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
7
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6

đầu ra.

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks

Thời gian phức tạp. Trên)
Không gian phụ trợ. Trên)

Phương pháp #2. Sử dụng lambda + cắt chuỗi + Lower() Công thức của hàm lambda phải được thêm vào nếu chúng ta cần thực hiện tác vụ xử lý cả giá trị Không có giá trị hoặc chuỗi rỗng và điều này trở nên cần thiết để xử lý các trường hợp cạnh.  

Python3




# Python3 code to demonstrate working of

# Lowercase first character of String

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
9

 

# initializing string

test_str

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
1

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
2

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4_______0_______5
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
7
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
9

# Lowercase first character of String

 

 

# Python3 code to demonstrate working of3 # Python3 code to demonstrate working of4# Python3 code to demonstrate working of5 # Python3 code to demonstrate working of6

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
5_______0_______6

 

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
9# Lowercase first character of String3 test_str# Lowercase first character of String5 # Lowercase first character of String6

 

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4_______0_______3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
7# Using lower() + string slicing3

đầu ra.

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks

Thời gian phức tạp. Trên)
Không gian phụ trợ. Trên)

Phương pháp #3. Sử dụng phương thức thay thế () mà không cần cắt

Python3




# Python3 code to demonstrate working of

# Lowercase first character of String

 

# initializing string

test_str

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
1

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
2

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4_______0_______5
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
7
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8

 

test_str

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0_______139_______9
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4test_str1
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4test_str3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8_______140_______5

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4_______0_______3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
7
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8

Đầu ra

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks

Thời gian phức tạp. Trên)
Không gian phụ trợ. Trên)

Phương pháp #4. Sử dụng phương thức cắt lát và chỉ mục ()

Python3




# Python3 code to demonstrate working of

# Lowercase first character of String

 

# initializing string

test_str

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
1

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
2

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4_______0_______5
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
7
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
16_______0_______0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
18

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
19_______0_______0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
21

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
1
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
24_______0_______4
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
26
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8_______0_______9

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4_______0_______3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
7
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6

Đầu ra

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks

Thời gian phức tạp. Trên)
Không gian phụ trợ. Trên)

Phương pháp #5. Sử dụng các hàm ord(),chr()

Python3




# Python3 code to demonstrate working of

# Lowercase first character of String

 

# initializing string

test_str

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
1

 

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
2

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
3
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4_______0_______5
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
7
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
8

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
51
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
53

# Lowercase first character of String3

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
56
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
57
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
59
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
56
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
63
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
64

The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
65
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
51
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
0
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
68
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
56
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
57
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
4
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
73
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
6
The original string is : GeeksforGeeks
The string after lowercasing initial character : geeksforGeeks
75test_str5

Làm cách nào để thay đổi tất cả chữ hoa thành chữ thường trừ chữ cái đầu tiên trong JavaScript?

Phương thức toLowerCase chuyển đổi một chuỗi thành chữ thường . Phương thức toLowerCase() không nhận bất kỳ tham số nào. Các chuỗi trong JavaScript là bất biến. Phương thức toLowerCase() chuyển đổi chuỗi được chỉ định thành một chuỗi mới chỉ bao gồm các chữ cái viết thường và trả về giá trị đó.

Làm cách nào để tạo một chuỗi chữ thường trong JavaScript?

Phương thức toLowerCase() trả về giá trị của chuỗi được chuyển thành chữ thường. toLowerCase() không ảnh hưởng đến giá trị của chính chuỗi str.

Làm cách nào để viết hoa chữ cái đầu tiên và viết hoa phần còn lại nhỏ trong JavaScript?

Để viết hoa ký tự đầu tiên của chuỗi, chúng ta có thể dùng hàm charAt() để tách ký tự đầu tiên rồi dùng hàm toUpperCase() để viết hoa ký tự đầu tiên. Now, we would get the remaining characters of the string using the slice() function.

Làm cách nào để kiểm tra xem chữ cái đầu tiên có phải là chữ hoa JavaScript không?

toUpperCase() . Ghi chú. chuỗi. charAt(index) được ưu tiên hơn string[index] (ký hiệu dấu ngoặc).