如何从字符串中获取多个字符?

发布于 2024-12-07 04:50:47 字数 323 浏览 0 评论 0原文

我正在尝试从数字列表中获取第五、第六和第七位数字。

例如,我想从变量dateofbirth中获取年份,并将其保存为一个名为dob的单独变量,作为int

这是我所拥有的:

int dateofbirth = 17031989
String s = Integer.toString(dateofbirth);
int dob = s.charAt(5);

我必须在 s.charAt 之后的括号中放入什么才能获得连续的几个数字?

I'm trying to get the 5th, 6th and 7th digits from a list of digits.

E.g. I want to get the year out of the variable dateofbirth, and save it as a separate variable called dob, as an int.

Here is what I have:

int dateofbirth = 17031989
String s = Integer.toString(dateofbirth);
int dob = s.charAt(5);

What would I have to put in the parentheses after s.charAt to get a few digits in a row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

漫雪独思 2024-12-14 04:50:47

不需要字符串转换:

int dateofbirth = 17031989;
System.out.println(dateofbirth%10000); //1989

如果您确实想将其作为字符串进行转换,则 substring() 方法将是你的朋友。您还需要使用 Integer.parseInt() 将字符串转换回整数。将字符值作为整数将给出该字符的 ASCII 值,而不是表示该字符的整数!

No need for the string conversion:

int dateofbirth = 17031989;
System.out.println(dateofbirth%10000); //1989

If you did want to do it as a string, then the substring() method would be your friend. You'd also need to use Integer.parseInt() to convert the string back into an integer. Taking a character value as an integer will give you the ASCII value of that character, not an integer representing that character!

与之呼应 2024-12-14 04:50:47

s.substring(5) 将为您提供从索引 5 开始的所有内容。您还可以给出第二个参数来指示您希望子字符串结束的位置。

s.substring(5) will give you everything starting from index 5. You could also give a second argument to indicate where you want the substring to end.

反目相谮 2024-12-14 04:50:47

您想要使用 String.substring(未经测试):

int dateofbirth = 17031989;
String s = Integer.toString(dateofbirth);
String year = s.substring(4, 8);
int yearInt = Integer.parseInt(year);

You want to use String.substring (untested):

int dateofbirth = 17031989;
String s = Integer.toString(dateofbirth);
String year = s.substring(4, 8);
int yearInt = Integer.parseInt(year);
毁梦 2024-12-14 04:50:47

如果您正在处理日期,您可以使用 SimpleDateFormat 和 Calendar 来提取年份:

SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy"); 
Calendar cal = Calendar.getInstance();
cal.setTime(formatter.parse(Integer.toString(dateofbirth)));
int year = cal.get(Calendar.YEAR);

If you are handling dates, you could use SimpleDateFormat and Calendar to pull out the year:

SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy"); 
Calendar cal = Calendar.getInstance();
cal.setTime(formatter.parse(Integer.toString(dateofbirth)));
int year = cal.get(Calendar.YEAR);
苦妄 2024-12-14 04:50:47

你做:

String s = String.valueOf(dateofbirth);
int yourInt = Integer.parseInt(s.substring(startIndex,length))

you do:

String s = String.valueOf(dateofbirth);
int yourInt = Integer.parseInt(s.substring(startIndex,length))
才能让你更想念 2024-12-14 04:50:47

这种方法不起作用有几个原因:

  1. s.charAt(5) 将为您提供第 6 个字符(不是 9)的 ASCII 代码。
  2. 没有方法可以获取多个字符

如果您想从此字符串中提取 1989,您应该使用 substring,然后使用 Integer.valueOf() 将此字符串转换为整数

This approach won't work for several reasons:

  1. s.charAt(5) will give you the ASCII code of the 6th character (which is not 9).
  2. There is no method to get several chars

If you want to extract 1989 from this string you should use substring and then convert this string into an Integer using Integer.valueOf()

掩于岁月 2024-12-14 04:50:47
Integer.parseInt(s.subString(4))
Integer.parseInt(s.subString(4))
回心转意 2024-12-14 04:50:47

来自 JRE 文档: getChars

From the JRE documentation: getChars

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文