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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7569599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:33:03  来源:igfitidea点击:

How can I get multiple characters from a string?

javastringchar

提问by Ger Crowley

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

我正在尝试从数字列表中获取第 5、6 和 7 位数字。

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.

例如,我想从变量中获取年份dateofbirth,并将其另存为一个名为 的单独变量dob,作为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.charAtto get a few digits in a row?

s.charAt在连续获得几位数字之后,我必须在括号中输入什么?

回答by Michael Berry

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!

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

回答by smp7d

You want to use String.substring(untested):

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

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

回答by Daniel Brockman

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.

s.substring(5)将为您提供从 index 开始的所有内容5。您还可以提供第二个参数来指示您希望子字符串在哪里结束。

回答by Petteri Hietavirta

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

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

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

回答by jornb87

Integer.parseInt(s.subString(4))

回答by francesco.s

you do:

你做:

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

回答by leifg

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
  1. s.charAt(5) 会给你第 6 个字符(不是 9)的 ASCII 码。
  2. 没有办法得到几个字符

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

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

回答by SJuan76

From the JRE documentation: getChars

从 JRE 文档: getChars