Tag: 字符串

字符串的断言失败消息包含子串

我最近在文本生成软件的文本输出上做了很多function测试,发现自己写了很多 assertTrue(actualString.contains(wantedString)); 但是,失败时的消息是非描述性的 Expected [true], but was [false] 另一种方法是包含自定义失败消息 String failMsg = String.format(“Wanted string to contain: %s, Actual string: %s”, wantedString, actualString); assertTrue(failMsg, actualString.contains(wantedString)); 但是一直手动执行此操作感觉有点乏味。 有没有更好的办法?

字符串中的换行符未写入文件

我正在尝试编写一个程序来处理从文件中读入的unicode字符串。 我想到了两种方法 – 一种是我读取包含换行符的整个文件,执行几次正则表达式替换,然后将其写回另一个文件; 另一个我在文件中逐行读取并匹配各行并替换它们并将其写出来的地方。 我无法测试第一种方法,因为字符串中的换行符不会写为文件的换行符。 以下是一些示例代码: String output = “Hello\nthere!”; BufferedWriter oFile = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(“test.txt”), “UTF-16”)); System.out.println(output); oFile.write(output); oFile.close(); print语句输出 你好 那里! 但文件内容是 你好! 为什么我的换行不写入文件?

对字符串不变性的困惑

我有以下代码: – public class StaticImplementer { private static String str= “ABC”; public static void main(String args[]) { str = str + “XYZ”; } } 问题: – 这里String str是静态的,那么这个对象将存储在内存中? 由于String是不可变的,“XYZ”的对象将被存储在哪里? 最终对象将在哪里存储? 垃圾收集将如何完成?

Java – 在线上初始化字符串数组内存分配?

String[] stringArray = new String[] { “abc”, “xyz” }; 执行此行有两个字符串文字在PERMGEN空间中创建“abc”和“xyz”。 由于初始化有一个新的运算符,我不确定是否在HEAP上分配了任何内存。 多谢你们。

在JOptionPane中显示数组(字符串)的内容

我这里有三个要求输入的类,应该在对话框中显示一个字符串数组。 第1类: public class Card { public int rank; public int suit; public String rankName; public String suitName; public String cardName; public Card () { rank = 0; suit = 0; rankName = “”; suitName = “”; cardName = “”; } public int getRank() { return rank; } public void setRank(int rank) { this.rank = […]

Java字节到字符串

所以我想将DatagramPacket的getData()函数返回的字节数组转换为字符串。 我知道将整个字节数组转换为字符串的方法就是: String str = new String(bytes); 但是,这会在最后打印出空字符。 所以如果字节数组是 [114, 101, 113, 117, 101, 115, 116, 0, 0, 0] 0打印出控制台上的空框。 所以我基本上只想打印出来: [114, 101, 113, 117, 101, 115, 116] 所以我做了这个function: public void print(DatagramPacket d) { byte[] data = d.getData(); for(int i=0; i< d.getLength(); i++) { if(data[i] != 0) System.out.println( data[i] ); } } 但不幸的是,这是打印出实际数字而不是字母。 那么如何将每个字节转换为字符串并将其打印出来。 或者,如果有另一种方法来打印字节数组而不是最后的空值那么那也没关系。

有没有更好的方法将Double转换为精确的十进制字符串?

我尝试了很多方法使用String.format()和DecimalFormater将Double转换为精确的十进制字符串,到目前为止我发现了这个: String s = “-.0000000000000017763568394002505”; Double d = Double.parseDouble(s) * 100; System.out.println((new java.text.DecimalFormat(“###########################.################################################################################”)).format(d)); ( https://ideone.com/WG6zeC ) 我并不是我的解决方案的粉丝,因为制作一个不能涵盖所有案例的巨大模式对我来说很难看。 你还有另一种方法吗? 我真的想要所有的小数, 没有科学的符号 , 没有尾随零 。 谢谢 PS:我需要这个没有外部库和Java 1.7。

使用reflection更改布尔字符串值不起作用

我正在尝试Javareflection和内联字符串,并得出结果,我觉得令人困惑。 import java.lang.reflect.Field; public class HappyDebugging { public static void main(String[] args) throws Exception { defineTrueFalse(); System.out.println(“true is ” + true); // why is it “true is true”? System.out.println(“false is ” + false); System.out.println(true); System.out.println(false); System.out.println(“true”); System.out.println(“false”); System.out.println(“true is ” + Boolean.valueOf(true)); System.out.println(“false is ” + Boolean.valueOf(false)); System.out.println(“true is ” + Boolean.valueOf(“true”)); System.out.println(“false is ” […]

当我将文本从java servlet加载到JTextPane时,为什么要松开换行符呢?

我尝试使用java servlet加载包含多行文本的文本文件的内容。 当我在浏览器中测试servlet时它运行正常。 文本加载了新的行号。 但是当我将它加载到我的swing应用程序中的字符串然后使用textpane.setText(text); 新线路消失了。 我尝试了很多我在网上找到的解决方案,但仍然无法做到正确。 Servlet代码: 从文件中读取文本(简化): File file = new File(path); StringBuilder data = new StringBuilder(); BufferedReader in = new BufferedReader(new FileReader(file)); String line; while ((line = in.readLine()) != null) { data.append(line); data.append(“\n”); } in.close(); 发送文字: PrintWriter out = response.getWriter(); out.write(text)); 这是一个平台问题吗? Servlet是在Linux上编写和编译的,但是我在Windows上运行它(在JBoss上)。 文本文件也存储在我的机器上。

计算字符串的频率

我基本上想要搜索字符串的频率。 例如,如果我传入单词“I”,那么下面这句话中的单词的频率:“ 我去了海滩, 我看到三个人”应该是2.我已经构建了这样的方法一个文本(任意长度),由白色空格将其分割成一个数组,并循环遍历数组,搜索每个索引是否与单词匹配。 然后,我递增频率计数器并将数字作为字符串返回。 这是方法: private int freq() { String text = “I went to the beach and I saw three people”; String search = “I”; String[] splitter = text.split(“\\s+”); int counter = 0; for (int i=0; i<splitter.length; i++) { if (splitter[i]==search) { counter++; } else { } } return counter; } } 这不在方法之外: […]