javascript 删除空格后的所有字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28480045/
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-28 09:02:35  来源:igfitidea点击:

Remove all string after a space

javascriptc#jquery

提问by Gonzalo Rivera

I have this variable

我有这个变量

c#

C#

string text = "val1  val2";

javastript

条码

var text = "val1  val2"

I need a example to remove everything after the space so that the result is string textModified = "val1";

我需要一个例子来删除空格后的所有内容,以便结果是 string textModified = "val1";

Can u help me?

你能帮我吗?

回答by Amit Joki

In both the cases, just split on " "and take the element on index 0. The below is for javascript

在这两种情况下,只需拆分" "并获取 index上的元素0。以下是针对javascript 的

alert(text.split(" ")[0]); // Javascript

And this is for c#

这是用于c#

Console.WriteLine(text.Split(' ')[0]);

回答by Wilfredo P

One example would be:

一个例子是:

c#

C#

string text = "val1  val2";
string textModified = text.Split(' ')[0];
//Another way:
string textModified = text.IndexOf(' ');
var textModified = text.Substring(0, index);

Js

JS

    var text = "val1  val2";
    var textModified = text.Split(' ')[0];

    // Another way:
    var index = text.indexOf(' ');
    var textModified = text.substring(0, index);

回答by fubo

here another approach with SubString

这里是 SubString 的另一种方法

string text = "val1 val2";
int Space = text.IndexOf(' ');
string textModified = text.Substring(0, Space > 0 ? Space : text.Length);

and Linq

和林克

string text = "val1 val2";
string textModified = new string(text.TakeWhile(x => x != ' ').ToArray());

回答by Alex K.

Rather than splitting:

而不是分裂:

var word = text.substr(0, (text + " ").indexOf(" "));

(Swap to .Substring/ .IndexOfin C#)

(在 C# 中切换到.Substring/ .IndexOf



For fun, C# and JS (ES5) both:

为了好玩,C# 和 JS (ES5) 都:

var output = ""; var i = 0;
text += " ";
while (text[i] != ' ')
    output += text[i++];

回答by Habib

You can use String.Removeto remove everything after space. Use String.IndexOfto find space in the string.

您可以使用String.Remove删除空格后的所有内容。使用String.IndexOf查找字符串中的空间。

string text = "val1  val2";
string newText = text.Remove(text.IndexOf(' '));

You can also check if the string contains a space or not like:

您还可以检查字符串是否包含空格,例如:

if (text.Contains(' '))
{
    string newText = text.Remove(text.IndexOf(' '));
}

This will save you from the exception in case of absence of space in the string.

如果字符串中没有空格,这将使您免于出现异常。

As for JavaScript: you can do:

至于 JavaScript:你可以这样做:

console.log(text.substr(0,text.indexOf(' ')));

Should add checking for presence of space as well.

还应该添加检查空间是否存在。

回答by B Carey

I don't like to through away data you may need in the future so how about just breaking up the string into components. Since you know a space is a separator just set space " " as the delimiter for breaking up the line. The following is an example:

我不喜欢删除您将来可能需要的数据,所以将字符串分解为组件如何。由于您知道空格是分隔符,因此只需将空格“”设置为分隔行的分隔符。下面是一个例子:

string[] read_Items = new string[15];
string text = "val1  val2"
int item_cnt = 0;

// set delimiter for a single space  
char[] delimiters = new char[] { ' ' };


// Remove and double spaces and replace them with single spaces
 while (test.Contains("  "))
    test = test.Replace("  ", " ");

// Break up line into individual items
foreach (string word in test.Split(delimiters))
{
    read_Items[item_cnt] = word;
    item_cnt++;
    if (item_cnt > 14)
        break;
 }

Now you have an array of items you can size this to fit any number of items in a line. Note: If you needed to have more then one delimiter say you also needed to split on '=', ':'your delimiter definition would be:

现在您有一个项目数组,您可以调整它的大小以适合一行中的任意数量的项目。注意:如果您需要多于一个分隔符,那么您还需要在 '=', ':' 上拆分,您的分隔符定义将是:

char[] delimiters = new char[] { ' ', '=', ':' };