Java Substring Example: Extracting Part of a String
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 namedstrwith a hexadecimal string.String subStr = str.substring(5, 7);: This line uses thesubstring()method to extract a portion of thestrstring. The arguments5and7specify 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.
原文地址: https://www.cveoy.top/t/topic/qxUv 著作权归作者所有。请勿转载和采集!