搜索
首页Javajava教程在Java中,将一个单词的最后一个字母大写,并将第一个字母小写

在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”

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.

要将不同类型的值转换为字符串值,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<size-1; i++){
         String s = String.valueOf(str.charAt(i));
         middle=middle+s;
      }
      
      //find the updated string 
      String result = finalFirst+middle+finalLast;
      System.out.println("Updated string: "+result);
   }
}

Output

Original string: Python
Updated string: pythoN

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

以上是在Java中,将一个单词的最后一个字母大写,并将第一个字母小写的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?Mar 17, 2025 pm 05:46 PM

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?Mar 17, 2025 pm 05:44 PM

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?Mar 17, 2025 pm 05:43 PM

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Mar 17, 2025 pm 05:35 PM

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)