Click here to Skip to main content
15,915,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Comments
Bh@gyesh 16-Jun-14 0:30am    
Hi,
If I understood you correctly then following may be your solution :

string test="xred Milawa Blue";
int index1 = test.IndexOf("xred");
int index2 = test.IndexOf('xred', index1 + 1);
string result2 = test.Remove(index1, index2 - index1);

Thanks.

you try with :

Program that uses Replace: C#
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);
    }
}

Output

Darth Vader is scary.
Darth Vader is not scary.

Program that uses Remove: C#
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);
    }
}


Output

012
012 678
 
Share this answer
 
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");
 
Share this answer
 
VB.NET
VB
'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
 
Share this answer
 
v2
something like this
VB
dim urstring as string = "no such xblk"
dim newstring as string = urstring.replace("xblk", "")
 
Share this answer
 
Thanks for all the answers everyone, I kept at it and figured out a way. Not sure if it's correct, but it seems to be working in my case.

if line.contains("xblk") then line = line.replace("xblk", " ")

I then trim the white space from the start and hey presto, nice clean line :)
 
Share this answer
 
Comments
DamithSL 16-Jun-14 1:15am    
this will fail if lines like "sdfsdf xblk sdfsdfsd xblksss"
you will get result for above as "sdfsdf sdfsdfsd sss"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900