无法在Java中找到字符串的结束字符

huangapple go评论81阅读模式
英文:

Unable to find the end character of a string in Java

问题

尝试在不使用length方法的情况下找到字符串的长度...使用“charAt()”函数...请告知字符串的最后一个字符是什么..尝试使用""和null和'\0'..但仍然无法找到结束字符..

str = "test this string";
int i = 0;
char p = str.charAt(i); //'\u00A00';
while (p != '
str = "test this string";
int i = 0;
char p = str.charAt(i); //'\u00A00';
while (p != '\0') {
    p = str.charAt(i);
    ++i;
}
System.out.println("字符串 " + str + " 的长度是 " + i);
'
) {
p = str.charAt(i); ++i; } System.out.println("字符串 " + str + " 的长度是 " + i);
英文:

Trying to find the length of the string without using the length method…using "charAt()" function ..Kindly let know what is the last character of the string ..tried using ” and null and ‘\0′ ..but still not able to find the end character..

str=”test this string”;
int i=0;
char p=str.charAt(i);//’\u00A00′;
while(p!=’
str=”test this string”;
int i=0;
char p=str.charAt(i);//’\u00A00′;
while(p!=’\0’){
p=str.charAt(i);++i;
}
System.out.println("Length of string : "+str+" is "+i);
’){ p=str.charAt(i);++i; } System.out.println("Length of string : "+str+" is "+i);

答案1

得分: 2

在某些(通常是在 C 中)字符串实现中,字符串的“长度”是通过哨兵字符来跟踪的,通常是:'\0'。约定如下:

  • 实际字符 '\0' 不能在字符串中。
  • '\0' 字符是终止符;它本身不是字符串的一部分,表示字符串的结束。

然而,Java 的工作方式完全不同。

还有其他编码字符串长度的方法。例如,首先存储字符串的长度,然后存储相应数量的字节。以字节表示,想象一下在内存中的情况:

许多字节... 0x00 0x00 0x00 0x05 0x48 0x65 0x6C 0x6C 0x6F ... 还有许多字节

然后,给定指向第一个 0x00 的指针,代码可以确定它是“Hello”。您有一种假设的方式来存储字符串,它运行得非常好。然而,寻找“结尾的 0x00”是行不通的,因为这种特定的字符串存储方式没有这个。

上述情况接近 Java 的工作方式。然而,作为进一步的复杂性,Java 就是 Java。Java 不使用指针。不可能获得指向那个长度字段的内存指针。

总结:你想要做什么?不可能。没有\0 可供查找,也不能读取包含长度的内存。这已经抽象化了;如果你想要长度,就调用 stringInstance.length()。故事结束。

当然,还有创造性的、愚蠢的、完全学术的方法来实现。例如:

public int getStringLengthInAStupidWay(String in) {
  int i = -1;
  try {
    while (true) input.charAt(++i);
  } catch (IndexOutOfBoundsException inevitable) {}
  return i;
}

甚至可以通过一些花哨的数学方法将其变为 O(logn)。但是我真的不知道这样做可能有什么意义。

英文:

In some (usually in C) implementations of strings, the 'length' of a string is tracked by way of a sentinel character, generally: '\0'. The contract is:

  • The actual character '\0' cannot be in the string.
  • The '\0' char is the terminator; it is not itself part of the string and signals the end.

This is not how java works, at all, though.

There are other ways to encode string length. For example, first, you store the length of the string, then you store that many bytes. In bytes, imagine this is in memory:

many bytes ... 0x00 0x00 0x00 0x05 0x48 0x65 0x6C 0x6C 0x6F ... many more bytes

Then, given a pointer to that first 0x00, code can figure out that is "Hello". You have a hypothetical way of storing strings that works just great. However, scanning for 'the ending 0x00' is not going to work because this particular way of storing strings doesn't have that.

The above is close to how java works. However, as further complication, java is java. java does not do pointers. It is not possible to get a memory pointer pointing to that length field.

Conclusion: What you want to do? Not possible. There is no \0 to find, and you can't read the memory containing the length. It's abstracted away; if you want length, you invoke stringInstance.length(). End of story.

There are of course, creative, silly, and entirely academic ways to do it. For example:

public int getStringLengthInAStupidWay(String in) {
  int i = -1;
  try {
    while (true) input.charAt(++i);
  } catch (IndexOutOfBoundsException inevitable) {}
  return i;
}

You can even make that O(logn) with some fancy maths. I have absolutely no idea what possible point there'd be to this, though.

huangapple
  • 本文由 发表于 2020年9月22日 20:10:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64009454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定