This code snippet demonstrates how to extract a portion of a string in Java using the substring() method. Here's the breakdown:

String str = '78780B23C0012204000100084ABA0D0A';
String subStr = str.substring(5, 7);
System.out.println(subStr);

Explanation:

  • String str = '78780B23C0012204000100084ABA0D0A';: This line initializes a string variable named str with a hexadecimal string.
  • String subStr = str.substring(5, 7);: This line uses the substring() method to extract a portion of the str string. The arguments 5 and 7 specify the starting and ending indices (exclusive) of the substring to be extracted.
  • System.out.println(subStr);: This line prints the extracted substring to the console.

Output:

B2

This example showcases the basic functionality of the substring() method in Java, demonstrating how to extract a specific part of a string based on starting and ending indices.

Java Substring Example: Extracting Part of a String

原文地址: https://www.cveoy.top/t/topic/qxUv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录