Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用substr函数获取32K长度字符串的最后100个字节?_Java_String_Substring - Fatal编程技术网

Java 如何使用substr函数获取32K长度字符串的最后100个字节?

Java 如何使用substr函数获取32K长度字符串的最后100个字节?,java,string,substring,Java,String,Substring,摘要问题:在Java中,如何正确使用substr函数获取32K长度字符串的最后100个字节 我必须承认,我的C/C++很瘦,很少使用,我的Java经验也很好,这两个程序是我目前正在使用的 只是想得到一个测试,将一个字符串来回传递到主机,主机是我的舒适区(请不要发表评论…;-)…至少在接下来的30年里都不会坐在轮椅上…真的!) 字符串刚好低于32K,回到PC Eclipse IDE端,我将其定义为: String Hold_Container_Data = cont.getCHARData();

摘要问题:在Java中,如何正确使用substr函数获取32K长度字符串的最后100个字节

我必须承认,我的C/C++很瘦,很少使用,我的Java经验也很好,这两个程序是我目前正在使用的

只是想得到一个测试,将一个字符串来回传递到主机,主机是我的舒适区(请不要发表评论…;-)…至少在接下来的30年里都不会坐在轮椅上…真的!)

字符串刚好低于32K,回到PC Eclipse IDE端,我将其定义为:

String Hold_Container_Data = cont.getCHARData();
我复制的示例默认使用System.out.println打印所有内容。IDE控制台中有10个32K字符串。所以我想解决这个问题

所以我只想显示前100个字节和最后100个字节,因为诊断显示非常方便

使用以下工具轻松获取前100个字节:

Hold_Container_Data.substring( 0, 100 );
首先是:

Hold_Container_Data.substring( length-100, 100 );
我认为这可能是语法问题,即使它编译得很好

对于露齿而笑,我尝试了两种方法:

Hold_Container_Data.substring( 31000, 100 );
Hold_Container_Data.substring( 500, 100 );
后者收到:

java.lang.StringIndexOutOfBoundsException: String index out of range: -400 at java.lang.String.substring(Unknown Source) 哪种方法有效

以及:

哪一个看起来是空白的??当这些位置不应为空白时

当我使用以下命令时:

int mylength = Hold_Container_Data.length();
然后打印:

System.out.println
该值与实际字符串长度正确吗

我使用调试器,查看arg0和arg1的子字符串函数以及字符串数据区本身,它们在我有限的Java使用中看起来都很好

所以这里一定有我遗漏的非常简单的东西

有人能告诉我一个可以看的地方吗

提前感谢您在新开发平台上对我的耐心

阅读有助于:

公共字符串子字符串(int beginIndex, 内部索引)

返回作为此字符串的子字符串的新字符串。子字符串从指定的beginIndex开始,并延伸到索引endIndex-1处的字符。因此,子字符串的长度是endIndex beginIndex

根据描述,呼叫

Hold_Container_Data.substring( length-100, 100 );
毫无意义

您还应该阅读命名约定,并在使用Java编码时尊重Java约定。还要注意,字符串不包含字节,而是包含字符。

摘要答案:

int length = Hold_Container_Data.length();
String str = Hold_Container_Data.substring(length-100,length);

阅读精美手册?从Javadoc:

public String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. 
The substring begins at the specified beginIndex and 
extends to the character at index endIndex - 1. Thus the 
length of the substring is endIndex-beginIndex.

Examples:

     "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"


Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive. 
Returns:
    the specified substring. 
Throws:
    IndexOutOfBoundsException - if the beginIndex is negative, 
    or endIndex is larger than the length of this String object,
    or beginIndex is larger than endIndex.

看看JavaDocs总是一件好事,这里回答了很多问题。例如,子字符串具有从x开始复制最后一个字符的方法

Hold_Container_Data.substring(Hold_Container_Data.length() - 100);

我会成功的

您不需要在标题中包含Java。这就是标记的用途。作为旁注,字符串的100个字符实际上是200个字节。这实际上是我第一次做的,我忘记了包含int语句,但它就在那里。然而,它看起来是对的,但它给了我出界的机会。谢谢
public String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string. 
The substring begins at the specified beginIndex and 
extends to the character at index endIndex - 1. Thus the 
length of the substring is endIndex-beginIndex.

Examples:

     "hamburger".substring(4, 8) returns "urge"
     "smiles".substring(1, 5) returns "mile"


Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive. 
Returns:
    the specified substring. 
Throws:
    IndexOutOfBoundsException - if the beginIndex is negative, 
    or endIndex is larger than the length of this String object,
    or beginIndex is larger than endIndex.
Hold_Container_Data.substring(Hold_Container_Data.length() - 100);