使用字符串函数将para拆分为单行。 [英] Split the para into single lines using string functions.

查看:86
本文介绍了使用字符串函数将para拆分为单行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我在面试中面对一个项目。他们给出这样的句子。



他们要求将这个段分成单行。



输入是



STR =欢迎来到我们公司,这家公司是跨国公司,我们在世界各地都有很多分支机构。



这是一段。但我只输入三行。这里逗号应该终止



输出应该是

1)欢迎来到我们公司



2)这家公司是跨国公司



3)我们在世界各地都有很多分店


他们要求为此使用二维数组并使用字符串函数。但我不知道如何使用字符串函数进行拆分。我只是为这个程序使用for循环(但这不正确。)。他们不接受这一点。请任何人帮我怎么做?

解决方案

你可以尝试如下。



 使用系统; 

class 计划
{
静态 void Main()
{
string s = 欢迎来到我们公司,这家公司是跨国公司,我们在世界各地都有很多分支机构;
//
// 在空格上拆分字符串。
// ...这将分隔所有单词。
//
string [] words = s.Split(' );
foreach 字符串 单词)
{
Console.WriteLine(word);
}
}
}





输出



欢迎来到我们公司



这家公司是跨国公司



我们在世界各地都有很多分店



如需了解更多信息,请查看:



< a href =http://www.dotnetperls.com/split> http://www.dotnetperls.com/split [ ^ ]



我希望这会对你有所帮助。


实际的打破字符串位很简单:String.Split会这样做。

复杂性是为每一行添加一个编号前缀,并在没有循环的情况下输出它们 - 这是不太可能,因为即使Linq方法(可以很方便地做)也包含一个循环 - 它只是一个隐藏的循环。



但是,这会有效:

  string  s =  欢迎来到我们公司,这家公司是跨国公司,我们在世界各地都有很多分支机构。; 
string [] lines = s.Split(' );
string result = string .Join( \ n\\\
,lines.Select((l,index)= > string .Format( {0}){ 1},index + 1 ,l.Trim())));
Console.WriteLine(result);


  string  STR =  欢迎来到我们公司,这家公司是跨国公司,我们在世界各地都有很多分支机构 

string [] n = STR.Split(' );

string first = n [ 0 ]。ToString();

string second = n [ 1 ]。ToString();

string third = n [ 2 ]。ToString();


Yesterday i face one program in my interview. They give sentence like this.

They ask to split this para into single lines.

Input is

STR = Welcome to our company, This company is the MNC company , We have many branches all over world.

It is a one paragraph. But i just enter only three lines. Here the comma should be terminated

The output should be
1) Welcome to our company

2) This company is the MNC company

3) We have many branches all over world

they asked to use two dimension array for this and use string functions. But i don't know how to split using string function. I just use for loop for this program(But that is not correct.). They did not accept that. Please any one help me how to do this?

解决方案

You can try like as below.

using System;

class Program
{
    static void Main()
    {
    string s = "Welcome to our company, This company is the MNC company , We have many branches all over world";
    //
    // Split string on spaces.
    // ... This will separate all the words.
    //
    string[] words = s.Split(',');
    foreach (string word in words)
    {
        Console.WriteLine(word);
    }
    }
}



Output

Welcome to our company

This company is the MNC company

We have many branches all over world

For more info check this:

http://www.dotnetperls.com/split[^]

I hope this will help to you.


The actual "breaking the string up" bit is simple: String.Split will do that.
The complexity is adding a numbered prefix to each line, and outputting them without a loop - which is not really possible, because even Linq methods (which could do it quite handily) contain a loop - it's just a hidden one.

But, this would have worked:

string s = "Welcome to our company, This company is the MNC company , We have many branches all over world.";
string[] lines = s.Split(',');
string result = string.Join("\n\n", lines.Select((l, index) => string.Format("{0}) {1}", index + 1, l.Trim())));
Console.WriteLine(result);


string STR = "Welcome to our company, This company is the MNC company , We have many branches all over world" 

string[] n = STR.Split(',');

string first=n[0].ToString();

string second=n[1].ToString();

string third=n[2].ToString();


这篇关于使用字符串函数将para拆分为单行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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