A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 魏振龙 中级黑马   /  2012-7-11 10:34  /  2251 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

能不能把一个字符串以其中的空格为分隔符分成一个字符串数组,有没有直接实现的语句

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

9 个回复

倒序浏览
本帖最后由 许庭洲 于 2012-7-13 14:12 编辑

string  s1="Welcome to China!";
string[] s2=Regex.Split(s1, @"\s+");//返回的是没有空格的字符串s2数组。
回复 使用道具 举报
string[] letters = "a b c d".Split(new char[]{' '});
这样就可以按空格分隔了

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

回复 使用道具 举报
string myString = "how are you";
string[] myWords = myString.Split(' ');

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

回复 使用道具 举报
using System;
publicclass SplitTest {
publicstaticvoid Main() {
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
foreach (string s in split) {
if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
// The example displays the following output to the console://       This//       is//       a//       list//       of//       words//       with//       a//       bit//       of//       punctuation//       and//       a//       tab//       character

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

回复 使用道具 举报
这个直接应用Split函数就可以了。
  1. string str = "hei ma cheng xu yuan";
  2. string[] words = str.Split(' ');
复制代码

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

回复 使用道具 举报

string myString = "yi ding yao jin hei ma";
string[] myWords = myString.Split(' ');

评分

参与人数 1技术分 +1 收起 理由
郑文 + 1

查看全部评分

回复 使用道具 举报
杨康 中级黑马 2012-7-13 14:02:13
8#
import java.util.regex.*;
public class Demo
{
        public static void main(String []args)
        {
                  String str = "how are you";

                  String[] arr = str.split(" +");
                        
                for(String s : arr)
                {
                 System.out.println(s);
                 }
        }
}

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
调用目标字符串的Split()方法
string[] str=目标字符串.Split(' ');

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
java的str.split(" ");
这个得到的就是以空格为分隔符分成一个字符串数组。

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马