在Java中将日期是格式为"20110913"的字符串转换为"2011-09-13"的最快方法是什么?
我正在做一个相对简单的文件读取,并且我在输入值和从平面文件读取并存储在数组中的数据值之间的验证中遇到了异常.我已经将错误追踪到了firstName现场.我将此字段定义为等于array[0].While array[0]在我的输出中完美显示,fieldName不...它是空白或空.
这是我的代码,带有输出样本.
我不确定Stringbuilder这里是否最适合使用?或者其中一个深层String方法?我试过的一切似乎都没有用.我错过了什么...我知道这是我看不见的明显的东西.
public class DisplaySelectedNumbers
{
public static void main(String[] args)
{
final String FN = " ";
final String LN = " ";
final String PHONE = "0000000000";
String delimiter = ",";
String s = FN + delimiter + LN + delimiter + PHONE + System.getProperty("line.separator");
final int RECSIZE = s.length();
String[] array = new String[3];
Scanner kb = new Scanner(System.in);
Path file = Paths.get("PhoneList.txt");
String fName …Run Code Online (Sandbox Code Playgroud) public class newString {
public static void main (String args[]){
String title = "Book";
String title1;
title1 = title;
for(int i = 0; i < title.length(); i++){
for (int x = 0; x<title1.length(); x++){
if (title.charAt(i+x) == title1.charAt(x)){
System.out.print(title.charAt(0,1));
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白我在这里做错了什么。我需要做的是定义一个名为“title”的字符串,其中包含“Book”,我这样做了,然后创建了一个名为“title1”的第二个字符串。我需要创建代码来将 title 的内容存储到 title1 中,但只能存储所有其他字符。例如:title1 中应该有“Bo”。我究竟做错了什么?
为什么字符串连接与pip行在java中没有返回任何内容(空字符串)?
String product="";
for(Tar t:tars){
product.concat(t.product.concat("|"));
}
System.out.println(product);
Run Code Online (Sandbox Code Playgroud)
结果就是什么(空字符串).
我们几乎到处都使用串联字符串进行日志记录.
例如:catch (Exception e) {
logger.error("setUserSession()" + e.getMessage());
}
但我们怀疑这些语句是否因字符串变异而导致内存泄漏.有人建议使用String builder.但是我从Java中的toString()中的StringBuilder vs String连接等讨论中理解的是Java Compiler将使用String builder来转换字符串连接.
为了测试这个,我试图反编译以下java代码(使用JD-GUI);
logger.error("string1" + "string2");
logger.error("setUserSession()" + e.getMessage());
Run Code Online (Sandbox Code Playgroud)
而反编译器产生以下代码;
logger.error("string1string2");
logger.error("setUserSession()" + e.getMessage());
Run Code Online (Sandbox Code Playgroud)
但是我期待第二个语句中的String builder就像;
logger.error(new StringBuilder().append("setUserSession()").append(e.getMessage());
Run Code Online (Sandbox Code Playgroud)
所以我有两个疑问:
我在项目中看到了java代码,它从各种参数构造一个文件名.java代码是这样的
String file = null;
String fileDirSeperator = System.getProperty("file.separator");
String pwd = System.getProperty("user.dir");
file = pwd + fileDirSeperator + "properties" + fileDirSeperator + folder_name + file_name;
Run Code Online (Sandbox Code Playgroud)
我想知道我是否应该StringBuffer代替你String.我知道将任何内容附加到String会创建新对象,所以我想我应该StringBuffer.
我可以知道你的建议吗?
也,
file = pwd + fileDirSeperator + "properties" + fileDirSeperator + folder_name + file_name;
Run Code Online (Sandbox Code Playgroud)
在上面的文件名构造中,是否会创建6个对象(每个变量一个)?
我试图从for循环的结果中创建一个变量.首先是这可能吗?如果有可能我该怎么办?到目前为止这是我的代码:
Random r = new Random();
Random n = new Random();
int p = (int)(n.nextInt(14));
for (int i = 0; i < p; i++) {
char c = (char) (r.nextInt(26) + 'a');
System.out.println(c);}
String word = ("output of forloop goes here");
Run Code Online (Sandbox Code Playgroud)
我想使用for循环将所有随机生成的字母放入一个单词中.我该怎么做呢?
从以下3种不同的实现中,哪一个看起来像一个基于可读性,性能等的好候选者?
实施例#1:
@Override
public String toString() {
return "GuiTemplateCriteriaImpl [appTitle=" + appTitle
+ ", button1Text=" + button1Text + ", button2Text="
+ button2Text + ", defaultMessageText=" + defaultMessageText
+ ", rootFolder=" + rootFolder + ", supportedFileExt="
+ supportedFileExt + ", list1ToolTipText=" + list1ToolTipText
+ ", list2ToolTipText=" + list2ToolTipText + "]";
}
Run Code Online (Sandbox Code Playgroud)
实施例#2:
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("GuiTemplateCriteriaImpl")
.append("[")
.append(" appTitle=" + appTitle)
.append(", button1Text=" + button1Text)
.append(", button2Text=" + button2Text)
.append(", defaultMessageText=" + defaultMessageText)
.append(", …Run Code Online (Sandbox Code Playgroud) 据我所知+ operator,Java中的字符串被重载,当我们使用+运算符时,它会自动选择StringBuffer或StringBuilder来连接具有更好性能的字符串.唯一的例外是我们+在循环中使用运算符.在这种情况下,Java不使用StringBuffer和StringBuilder的好处.真的吗?我将连接40个字符串(大约4500个字符),我会经常重复它,所以我想确定它.
我在工作中的一些代码中找到了一个部分,其中使用这种格式创建了一堆长字符串:
String s = "" +
"...\n" +
"...\n" +
Run Code Online (Sandbox Code Playgroud)
出于好奇,决定快速测试一下StringBuilder是否会产生明显的变化。
public class TestStringConcat {
public static void main(String[] args) {
int arraySize = 1000000;
String[] strings1 = new String[arraySize];
String[] strings2 = new String[arraySize];
System.out.println("Testing concat version");
long startTime = System.currentTimeMillis();
for (int i = 0; i < arraySize; i++) {
strings1[i] = "" +
"A big long multiline string"; //35 lines of string omitted
}
long endTime = System.currentTimeMillis();
System.out.println("Time to concat strings: " + (endTime - startTime)); …Run Code Online (Sandbox Code Playgroud) 例如,当n = 5,我需要生成一个字符串"012345"。
我可以通过for从0to运行循环n并将数字附加到字符串来实现。
for(int i = 0; i <= n; i++) {
s += i;
}
Run Code Online (Sandbox Code Playgroud)
有没有没有for循环的简单方法?也许使用流?
问题很简单,怎样才能更好地避免非占用内存的使用呢?例如,假设我们有 aString s = "Test"并且我们想添加1它,使其变为Test1。我们都知道s获取一个内存位置,如果我们使用StringBuilder,Test1将会得到一个新的内存地址,否则它将保留在s原来的位置,如果我们使用呢concat?
在我的代码中,我使用如下所示的字符串连接.我正在进行代码审查,所以我需要进行性能检查.请任何人建议哪一个更好用.
quoteTreeMap.put(index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT + lastIndex,buffer.toString());
Run Code Online (Sandbox Code Playgroud)
所以我们反复使用它
index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT + lastIndex
Run Code Online (Sandbox Code Playgroud)
只有lastIndex这个值会改变,剩下的就是所有人都一样.
如何编写代码以提供更好的性能?我能宣布吗?
index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT
Run Code Online (Sandbox Code Playgroud)
这就像顶部一样
String one=""index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT"
Run Code Online (Sandbox Code Playgroud)
我可以用它one+lastIndex吗?
对于ref:
StringBuffer buffer = new StringBuffer();
buffer.append(CommonUtil.QUOTE_TREE_GROSS_PREM);
buffer.append(CommonUtil.QUOTE_TREE_SPACE);
buffer.append(CommonUtil.QUOTE_TREE_EQUALS);
buffer.append(CommonUtil.QUOTE_TREE_SPACE);
buffer.append(chcCalBreakDownObj.getPremium().getGrossPrem());
quoteTreeMap.put(index + CommonUtil.QUOTE_TREE_DOT + fundIndex + CommonUtil.QUOTE_TREE_DOT + lastIndex, buffer.toString());
lastIndex++;
clearBuffer(buffer);
Run Code Online (Sandbox Code Playgroud)
我正在进行代码审查,所以我需要进行性能检查.请任何人建议哪一个更好用.
java ×13
string ×8
performance ×2
arrays ×1
char ×1
java-stream ×1
object ×1
random ×1
stringbuffer ×1
tostring ×1