如何使用特殊格式将字符串拆分为子字符串? [英] How Do I Split A String To Substring With Special Format?

查看:69
本文介绍了如何使用特殊格式将字符串拆分为子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个以下格式的字符串,需要分别提取每个单词及其标签怎么做?



string =word1(tag1)word2(tag2 )word3(tag3)....

需要提取子字符串,例如:

str [0] =word1,str [1] =word2 ,str [3] =word3,....

tag [0] =tag1,tad [1] =tag2,tag [3] =tag3,. ...

I have several string with following format that need to extract each word and its tag separately How doing this?

string= " word1 (tag1) word2 (tag2) word3 (tag3) ... ."
needing to extracted substring such as:
str[0]="word1" , str[1]="word2", str[3]="word3" , ....
tag[0]="tag1" , tad[1]="tag2", tag[3]="tag3" , ....

推荐答案

可以有更好的方法来做到这一点,但你可以采取以下方法 -

There can be better way to do this but you may take following approach-
string originalString= "word1 (tag1) word2 (tag2) word3 (tag3)";
string[] tempArray=originalString.Split(' ');
string[] words=new string[((int)(tempArray.length/2))];
string[] tags=new string[((int)(tempArray.length/2))];

int indexWords=0;
int indexTags=0;

foreach(string str in tempArray)
{
  if(Array.IndexOf(tempArray,str)%2==0)
  {
    tags[indexTags]=str.Replace('(','').Replace(')','');
    indexTags++;
  }
  else
  {
    words[indexWords]=str;
    indexWords++;  
  }    
}





希望,它有帮助:)



Hope, it helps :)


使用正则表达式:

Use a Regex:
(?<str>.+?)(?<tag>\(.+?\))



应该将值分成str,tag对。

如果你然后使用Trim删除任何空格,然后通过循环或两个Linq分组到列表中声明。



注意:在上面的正则表达式中,反斜杠用于告诉正则表达式第二组内的那些是litteral open和close括号:但是,虽然它们在预览中可见,但它们在页面上不可见...

正则表达式字符串应如下所示:


Should separate the values into str, tag pairs.
If you then use Trim to remove any whitespace, and group then into lists via a loop or two Linq statements.

Note: in the Regex above, back slashes are used to tell the Regex that the ones inside the second group are litteral open and close brackets: However, while they are visible in the preview, they aren't visible on the page...
The Regex string should look like this:

(
?
<
s
t
r
>
.
+
?
)
(
?
<
t
a
g
>
\
(
.
+
?
\
)
)

删除所有换行符(我会将其报告为bug)。现在他们是可见。 :叹气:

With all the line breaks removed (I'll report it as a bug).And now they are visible. :sigh:


string [] words;

words = new string [100];

int i,j,k = 0,l = 0;

string str =word1(tag1)word2(tag2)word3(tag3);

words = str.Split('');



words = str.Split('');





for(i = 0; i< words.Length; i ++)

{

if(i%2 == 0)

{



Console.WriteLine(str+[++(k)+]+=+ words [i]);

k = k + 1;

}

}



for(j = 0; j< words.Length; j ++)

{

if( j%2!= 0)

{



Console.WriteLine(tag+[++(l) +]+=+字[j]);

l = l + 1;

}

}
string[] words;
words = new string[100];
int i,j,k=0,l=0;
string str = "word1 (tag1) word2 (tag2) word3 (tag3)";
words = str.Split(' ');

words = str.Split(' ');


for (i = 0; i < words.Length; i++)
{
if (i % 2 == 0)
{

Console.WriteLine("str" + "[" + "" + (k) + "]" + "=" + words[i]);
k=k+1;
}
}

for (j = 0; j < words.Length; j++)
{
if(j%2!=0)
{

Console.WriteLine("tag" + "[" + "" + (l) + "]" + "=" + words[j]);
l=l+1;
}
}


这篇关于如何使用特殊格式将字符串拆分为子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆