Tag: 字符串

在Java和C中在运行时调用名为“string”的方法

我们如何在运行时调用名称为string的方法。 任何人都可以告诉我如何在Java和C中做到这一点

如何使用String.format使字符串居中?

public class Divers { public static void main(String args[]){ String format = “|%1$-10s|%2$-10s|%3$-20s|\n”; System.out.format(format, “FirstName”, “Init.”, “LastName”); System.out.format(format, “Real”, “”, “Gagnon”); System.out.format(format, “John”, “D”, “Doe”); String ex[] = { “John”, “F.”, “Kennedy” }; System.out.format(String.format(format, (Object[])ex)); } } 输出: |FirstName |Init. |LastName | |Real | |Gagnon | |John |D |Doe | |John |F. |Kennedy | 我希望输出居中。 […]

大字符串在java中分割成具有最大长度的行

String input = “THESE TERMS AND CONDITIONS OF SERVICE (the Terms) ARE A LEGAL AND BINDING AGREEMENT BETWEEN YOU AND NATIONAL GEOGRAPHIC governing your use of this site, www.nationalgeographic.com, which includes but is not limited to products, software and services offered by way of the website such as the Video Player, Uploader, and other applications […]

Java:检查数组的相等性(顺序无关紧要)

我有两个String数组,让我们说: String[] s1 = {“a”,”b”,”c”} String[] s2 = {“c”,”a”,”b”} //这些数组应该相等 我想以“最干净”的方式检查他们的平等。 我尝试使用Arrays.equals(s1,s2)但我得到了一个错误的答案。 我想这个方法关心元素的顺序,我不希望这一点很重要。 你能告诉我怎样才能以一种好的方式做到这一点?

打印字符串比较结果时得到奇怪的输出

我正面临着这条线的问题(下面评论): System.out.println(“Using == ::”+s3==s4) 输出false 。 但是, System.out.println(s3==s4)输出true 。 现在,我无法理解为什么我得到这个结果: public class string { public static void main(String[] args){ String s3=”Shantanu”; String s4=s3; String s1=new String(“java”); String s2=new String(“javaDeveloper”); System.out.println(“Using Equals Method::”+s1.equals(s2)); System.out.println(“Using Equals Method::”+s3.equals(s4)); System.out.println(“Using == ::”+s3==s4);//Problem is here in this line System.out.println(s1+”Directly printing the s2 value which is autocasted from superclass to string […]

如何检测Java字符串中的unicode字符?

假设我有一个包含Ü的字符串。 我怎样才能找到所有那些unicode字符? 我应该测试他们的代码吗? 我该怎么办? 例如,给定字符串“AÜXÜ”,我想将其转换为“AYXY”。 我想对其他unicode角色做同样的事情,我不想将它们存储在某种翻译地图中。

如何比较原始类型中的字符忽略大小写

我正在写这些代码行: String name1 = fname.getText().toString(); String name2 = sname.getText().toString(); aru = 0; count1 = name1.length(); count2 = name2.length(); for (i = 0; i < count1; i++) { for (j = 0; j < count2; j++) { if (name1.charAt(i)==name2.charAt(j)) aru++; } if(aru!=0) aru++; } 我想比较两个String的Character s忽略这个案例。 仅使用IgnoreCase不起作用。 添加’ ASCII值也不起作用。 我该怎么做呢?

Java中的字符串标记生成器

我有一个文本文件,其中包含由’|’分隔的数据。 我需要得到每个字段(用’|’分隔)并处理它。 文本文件如下所示: ABC | DEF || FGHT 我正在使用字符串标记器(JDK 1.4)来获取每个字段值。 现在问题是,我应该在DEF之后得到一个空字符串。但是,我没有得到DEF和FGHT之间的空白区域。 我的结果应该是 – ABC,DEF,“”,FGHT,但我得到ABC,DEF,FGHT

将java中的字符串拆分为相等长度的子字符串,同时保持字边界

如何在保持字边界的同时将字符串拆分为最大字符长度的相等部分? 比如说,如果我想将一个字符串“hello world”拆分成最多7个字符的相等子串,它应该返回给我 “hello ” 和 “world” 但我目前的实施回归 “hello w” 和 “orld ” 我使用以下代码从Split字符串中取代Java中相等长度的子字符串,将输入字符串拆分为相等的部分 public static List splitEqually(String text, int size) { // Give the list the right capacity to start with. You could use an array // instead if you wanted. List ret = new ArrayList((text.length() + size – 1) / size); for (int […]

用于检查字符串是否为回文的Java方法

我想检查字符串是否是回文。 我想学习一种简单的方法来使用尽可能少的字符串操作来检查相同的方法