English 中文(简体)
Java固定装置
原标题:String terminator in Java

我是一位经验丰富的C++方案管理员,但新到 Java。

我有某种操纵的算法,我想在中间结束扼杀。

我不想这样做。

String str("hello, world");
char[] str2 = str.toCharArray(); 
//pass str2 to some function 
myFunc(str2);
...
//inside myFunc, I decide to terminate str2 at position 3, so I did
//str2[3] = 0

//then it returns, how can I construct an String type variable from str2 that includes chars before the   ?
//String str3 = new String(str2), doesn t work

我可以写上我自己的C类:<代码>strlen。 然而,我要知道,在 Java这样做是多么可取的。

另外,很难找到关于“代码”类型的文件。 每当一谷歌,我只能找到关于“果园”而不是“果园”的文件。 我想有以下方法:substrstrlen/>。

感谢任何建议!

最佳回答

In Java, char[] and String are completely different. Forget the association from C, As far as you re concerned for now, they re not connected in any way. (For completeness, that String objects hold a final char[] representing the string (and it s not null-terminated), which you can obtain as you did in your example. Don t do that until you re clear on the difference.) You call all your methods on the string. But remember, in Java, Strings are immutable - if you have a String, it will always be the same. You can assign a new String object to a variable, but the old string won t change (until all references are gone and it gets GC d, of course). This is important to remember when you are trying to get a new string, like this. Once you ve figured out how long you want your string, use .substring().

因此,你的例子应当看一下:

// Remember in Java no object is statically allocated.
String str1 = "Hello, World";
int indexToCutAt = myFunc(str1);
// We can t change str1, so we ll need to store the new string.
String str2 = str1.substring(0,indexToCutAt);
问题回答

如果我错了,我就认为,你只是想听从<条码>的头三个特性。 为此,你可以做:

str.substring(0, 3);

。 所有阵列都有长处,可检索如下:

char[] myChars = new Char[5];
return myChars.length; // returns 5

但是,你只是处理下列事项:<条码>、标准物体,一般而言,而不是<条码>。

<>strong>tl;dr: Just use the String methods

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

str3 = str.substring(0, 3);

Have a look at , substring(int beginIndex) and substring(int beginIndex, int endIndex) , This will help :)

So in your function myFunc(str2); return the position you want to break say pos ,

因此,你想写这样的东西。

int pos = myFunc(str2);
String str3 = str2.substring(0,pos);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签