从字符串中删除单词 [英] Removing word from string

查看:85
本文介绍了从字符串中删除单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在遇到一些麻烦,试图从字符串中删除某些单词,我不确定该怎么做。



我将一行传递给另一个以xred或xblk开头的类。我正在尝试删除这些单词,因为它们不需要在我的程序中显示,只是在我收到它时格式化文本。



I已经尝试过line.trim(xblk)但是因为这只能修剪空白区域,所以无法正常工作。

我尝试过line.remove(xblk)但是从我能做到的聚集去除一定数量的字符,而不是字符串



这基本上就是我正在使用并试图删除

如你所见,我无法设置一定数量的数字被删除,因为有时它会更长。



xred Milawa Blue

xblk ----------------------------------



我基本上需要做的是逐行阅读。

如果该行包含xred - >这标志一个整数,所以我可以设置文本的颜色。完成后,不再需要该信息,因此需要从行中删除该信息,然后执行line.trimstart()以删除空格。

I'm having a little bit of trouble at the moment trying to remove certain words from strings, I'm not exactly sure how to do it.

I pass a line through to another class which starts with xred or xblk. I'm trying to remove those words, as they aren't required to be displayed within my program, it's just to format the text when I receive it.

I've tried line.trim("xblk") But as this only trims white space, that can't work.
I've tried line.remove("xblk") But from what I can gather that removes a certain amount of characters, rather than the string

This is basically what I'm working with and trying to remove
As you can see I can't set a certain amount of digits to be removed, as sometimes it's much longer.

xred Milawa Blue
xblk----------------------------------

What I basically need to be able to do is read through line by line.
If the line contains xred -> This flags an integer so I can set the colour for the text. Once that is done, that piece of information isn't required any more so need to strip that from the line, then do a line.trimstart(" ") to remove the white space.

推荐答案

你试试:



使用替换的程序:C#

you try with :

Program that uses Replace: C#
using System;

class Program
{
    static void Main()
    {
	const string s = "Darth Vader is scary.";
	Console.WriteLine(s);

	// Note:
	// You must assign the result of Replace to a new string.
	string v = s.Replace("scary", "not scary");
	Console.WriteLine(v);
    }
}



输出



Darth Vader很可怕。

Darth Vader并不可怕。



使用Remove的程序:C#


Output

Darth Vader is scary.
Darth Vader is not scary.

Program that uses Remove: C#

using System;

class Program
{
    static void Main()
    {
	//
	// 1. Remove all characters after an index.
	//
	// ... Seven character string.
	string test1 = "0123456";

	// ... Start removing at index 3.
	string result1 = test1.Remove(3);

	// ... Displays the first three characters.
	Console.WriteLine(result1);

	//
	// 2. Remove range of characters in string.
	//    See explanation.
	//
	string test2 = "012 345 678";
	int index1 = test2.IndexOf(' ');
	int index2 = test2.IndexOf(' ', index1 + 1);
	string result2 = test2.Remove(index1, index2 - index1);
	Console.WriteLine(result2);
    }
}





输出



012

012 678



Output

012
012 678


line.remove(xblk)应该适合你。 />
如果您确定要删除的字符串,可以将其输入 remove 方法。

例如 line.remove(我要移除的字符串);
line.remove("xblk") should work for you.
If you are sure of the string you want to remove, you can enter this into the remove method.
For e.g. line.remove("My string to be removed");


VB.NET

VB.NET
'sample array for testing
Dim lines As String() = New String() {"xred Milawa Blue", "xblk  sdfsdfsdfsd"}
For i As Integer = 0 To lines.Length - 1
	If lines(i).StartsWith("xred") OrElse lines(i).StartsWith("xblk") Then
		lines(i) = lines(i).Substring(4).Trim()
	End If
Next
'result
'lines[0] = Milawa Blue
'lines[1] = sdfsdfsdfsd



C#


C#

//sample array for testing
string[] lines = new string[]{"xred Milawa Blue", "xblk  sdfsdfsdfsd"};
for(int i=0; i<lines.Length;i++)
{
  if(lines[i].StartsWith("xred") || lines[i].StartsWith("xblk"))
  {
     lines[i] = lines[i].Substring(4).Trim();
  }
}
//result
//lines[0] = Milawa Blue
//lines[1] = sdfsdfsdfsd


这篇关于从字符串中删除单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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