回答
試試這個,使用正則表達式來分割輸入字符串,每當一個或多個序列空間被找到,然後轉換導致String[]
到int[]
:
String numberString = " 1 2 34 7 ";
String[] numbers = numberString.split("\\s+");
int[] ints = new int[numbers.length];
for (int i = 0; i < ints.length; i++)
ints[i] = Integer.parseInt(numbers[i]);
現在ints
數組包含的預期值:
System.out.println(Arrays.toString(ints));
> [1, 2, 34, 7]
非常感謝你!有用!!謝謝!!! – 2012-04-22 01:32:05
$string = "3 6 4 879";
preg_match_all('/\d+(?=\s*)/', $string, $arr);
這是PHP的一種方式。
但他使用java – 2012-04-22 01:01:38
謝謝你的幫助! – 2012-04-22 01:32:38
可以用拆分用於您的問題,例如:
程序上空間分割[C#]
static void Main()
{
string s = "1 3 2 7";
//
// Split string on spaces.
// ... This will separate all the words.
//
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
輸出
我認爲他希望他們作爲整數,所以你需要解析這些字符串在C#中int。 – Jack 2012-04-21 22:52:46
這個樣本很好,正確的分裂,並且不需要一個轉換器... – aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2012-04-21 22:58:29
但是@Leaf C.問題是你對我的樣本需要轉換。 – aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2012-04-21 23:09:47
Scanner sc = new Scanner(mString);
while (sc.hasNextInt()){
mArrayList.add(sc.nextInt())
}
然後,只需轉換的ArrayList回陣列。
感謝您的幫助! – 2012-04-22 01:33:59
- 1. MYSQL在字符串中找到INT
- 2. INT到字符串連接
- 3. 字符串到int parseInt
- 4. 字符串到int數組
- 5. Java字符串到Int
- 6. Java字符串到Int
- 7. PHP int到字符串
- 8. int到字符串,char * itoa
- 9. 在字符串中找不到子串
- 10. 在字符串中查找字符串
- 11. 在字符串中查找字符串
- 12. 試圖解析int字符串的輸入字符串int int
- 13. 字符串到斯卡拉Int中
- 14. 在字符串中找到一個數組中的字符串
- 15. 找到字符串中的@字符
- 16. 找到字符串
- 17. 找到字符串
- 18. 找到字符串
- 19. 從字符串到字符串流到矢量<int>
- 20. 在純c中的字符串(char *)中查找int
- 21. RegEx在字符串中找到字符串後提取名稱
- 22. 批處理文件在字符串中找到子字符串
- 23. 如何在字符串中找到子字符串?
- 24. 如何在字符串中找到字符串
- 25. 如何找到字符串放在字符串中的位置?
- 26. mysql/php:在字符串中找到起始字符串
- 27. 如何在Objective-c字符串中找到子字符串
- 28. 在字符串中找到多個子字符串
- 29. 如何在字符串中找到字符串'NULL'?
- 30. 在行中找到第n個字符串或子字符串
你在用什麼語言? – Jack 2012-04-21 22:38:36
我正在使用java – 2012-04-22 00:26:23