0

0

在Java中,将一个单词的最后一个字母大写,并将第一个字母小写

PHPz

PHPz

发布时间:2023-08-20 10:05:07

|

923人浏览过

|

来源于tutorialspoint

转载

在java中,将一个单词的最后一个字母大写,并将第一个字母小写

String是一系列字符值的序列。在Java中,String被视为对象。我们有一个由Java提供的String类,用于创建和操作字符串。

We have to convert the first letter of the word to lowercase and last letter of the word to uppercase.

In this article we will see how the first and last letter can be converted into lower and upper case respectively. Let’s explore.

展示一些实例给你看

Instance-1

的中文翻译为:

实例-1

假设输入字符串是“Hello”

立即学习Java免费学习笔记(深入)”;

After converting the first letter to lower and last letter to capital, the new string will be “hellO”

Instance-2

的翻译为:

Instance-2

Suppose the input string is “Java”

After converting the first letter to lower and last letter to capital, the new string will be “javA”

Instance-3

Suppose the input string is “Programming”

将第一个字母转为小写,最后一个字母转为大写后,新字符串将为“programminG”

Syntax

要获取字符串的长度,Java的String类提供了一个length()方法。

Below is the syntax for that −

str.length();

其中,str是字符串变量。

要从原始字符串中获取子字符串,Java String类提供了substring()方法。

Syntax

Below is the syntax for that −

str.substring(int startIndex);
str.substring(int startIndex, int endIndex);

其中,startIndex是包含的,endIndex是不包含的。

To get a character at a specified index Java String class provides the charAt() method.

Syntax

Below is the syntax for that −

str.charAt(int index)

Where, index refers to the index of the character that you need.

Supercreator
Supercreator

AI视频创作编辑器,几分钟内从构思到创作。

下载

要将不同类型的值转换为字符串值,Java的String类提供了valueOf()方法。

Syntax

Below is the syntax for that −

String.valueOf(Object obj)

Algorithm

注意 - 这个问题也可以用数组的概念来解决。但是在这里,我们尝试了不使用数组概念来解决问题。同时,我们只使用了String类的内置方法。

Algorithm-1

  • 步骤 1 - 通过初始化或用户输入获取字符串/单词。

  • Step 2 − Get the first and last letter by using the substring() method. Then convert it to lower and upper case by using toLowerCase() method and toUpperCase() method respectively.

  • 第三步 - 使用substring()方法获取中间字符(除第一个和最后一个字符)。

  • 第四步 - 将第一个、中间和最后一个值组合起来,得到最终的字符串/单词。

Algorithm-2

  • 步骤 1 - 通过初始化或用户输入获取字符串/单词。

  • 第二步 - 通过使用charAt()方法获取第一个和最后一个字母。然后分别使用toLowerCase()方法和toUpperCase()方法将其转换为小写和大写。

  • 步骤 3 - 使用charAt()方法和for循环获取中间字符(除了第一个和最后一个字符)。

  • 第四步 - 然后将第一个、中间和最后一个值组合在一起,得到最终的字符串/单词。

Multiple Approaches

We have provided the solution in different approaches.

  • 通过使用内置的substring()方法

  • By Using inbuilt charAt() Method

Let’s see the program along with its output one by one.

Approach-1: By Using Inbuilt substring Method

Example

In this approach, we will make use of Algorithm-1

import java.util.Scanner;
public class Main{
   public static void main(String[] args) {
   
      //input string
      String str = "Java";
      System.out.println("Original string is: "+str);
      
      //get size of the string 
      int size = str.length();
      
      //get last character and convert it to upper case
      String last = str.substring(size-1,size);
      String lastinUpper = last.toUpperCase();
      
      //get first character and convert it to lower case
      String first = str.substring(0,1);
      String firstinLower = first.toLowerCase();
      
      //get middle parts of the word, except first and last character
      String middle = str.substring(1,size-1);
      
      //combine everything and get the final string
      String result = firstinLower+middle+lastinUpper;
      
      //print result
      System.out.println("Updated string is: "+result);
   }
}

Output

Original string is: Java
Updated string is: javA

Approach-2: By Using Inbuilt charAt() Method

Example

在这种方法中,我们将利用Algorithm-2

public class Main{
   public static void main(String[] args) { 
   
      //input String
      String str = "Python";
      System.out.println("Original string: "+str);
      
      //get length of string
      int size = str.length();
      
      //find last character and convert it to upper case
      char last = str.charAt(size-1);
      String finalLast = (String.valueOf(last)).toUpperCase();
      
      //find first character and convert it to lowercase
      char first = str.charAt(0);
      String finalFirst = (String.valueOf(first)).toLowerCase();
      
      //find middle characters
      String middle="";
      for(int i=1; i

Output

Original string: Python
Updated string: pythoN

在本文中,我们探讨了如何使用不同的方法将单词的首字母转换为小写,并将单词的最后一个字母转换为大写字母。

相关文章

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

14

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

7

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

5

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

19

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

105

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 2.8万人学习

C# 教程
C# 教程

共94课时 | 7.3万人学习

Java 教程
Java 教程

共578课时 | 49.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号