| View previous topic :: View next topic |
| Author |
Message |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Wed Feb 18, 2009 4:58 pm Post subject: How do you remove muiltiple characters in a string [vb.net] |
|
|
I am making a file parser and I need to know how to remove muiltiple characters. So pretend I have a string that contains
I want to make it so it only has only one 'a' and one 'b' so it will look like this
Does anyone know how to do this?
_________________
CEF will always stay alive. |
|
| Back to top |
|
 |
rapion124 Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Mar 2007 Posts: 1095
|
Posted: Wed Feb 18, 2009 5:53 pm Post subject: |
|
|
I don't know how to do it in VB.net, but I know how to do it in C++.
| Code: |
DWORD FilterString(PTSTR szStringToFilter, PTSTR szReturnBuffer, SIZE_T dwStringSize, SIZE_T dwBufferSize)
/*
This routine filters a string so that only 1 of each character is kept. The return value is the number of characters copied to the buffer.
*/
{
unsigned int i, j, k;
k = 0;
ZeroMemory(szReturnBuffer, dwBufferSize);
for (i = 0; i < dwStringSize; i++)
{
for (j = 0; j < k; j++)
{
if (szStringToFilter[i] == szReturnBuffer[j])
{
break;
}
}
if (j == (dwBufferSize - 1))
{
szReturnBuffer[k] = szStringToFilter[i];
k++;
}
}
return k;
} |
|
|
| Back to top |
|
 |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Wed Feb 18, 2009 6:01 pm Post subject: |
|
|
Thanks anyways.
Also how can you remove all lines that don't have special characters?
_________________
CEF will always stay alive. |
|
| Back to top |
|
 |
BanMe Master Cheater
Reputation: 0
Joined: 29 Nov 2005 Posts: 375 Location: Farmington NH, USA
|
Posted: Wed Feb 18, 2009 8:23 pm Post subject: |
|
|
jorg hi you could just use strcspn to find occurances of charecters
this is a representation of the logic.. easily tranferable to VB or w/e
firstchar = w/e it takes to get first char in VB.net
size_t i = strcspn(string+1,firstchar);
if(i)
*(char*)string+i = 0x00;
repeat strcspn(string+i,firstchar) to find next occurance and zero it out
do this till i = strlen(string+i)
hopefully this helps..
regards BanMe
_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you. |
|
| Back to top |
|
 |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Wed Feb 18, 2009 8:26 pm Post subject: |
|
|
| BanMe wrote: | jorg hi you could just use strcspn to find occurances of charecters
this is a representation of the logic.. easily tranferable to VB or w/e
firstchar = w/e it takes to get first char in VB.net
size_t i = strcspn(string+1,firstchar);
if(i)
*(char*)string+i = 0x00;
repeat strcspn(string+i,firstchar) to find next occurance and zero it out
do this till i = strlen(string+i)
hopefully this helps..
regards BanMe |
Ahh I sort of get this.
_________________
CEF will always stay alive. |
|
| Back to top |
|
 |
NINTENDO Grandmaster Cheater Supreme
Reputation: 0
Joined: 02 Nov 2007 Posts: 1371
|
Posted: Thu Feb 19, 2009 7:51 am Post subject: |
|
|
I tried to translate c# into vb.net using codechanger.com but I dont know if it failed
| Code: |
private void Form1_Load(object sender, EventArgs e)
{
string message = StringBuilder("abcef", "abc".ToCharArray());
MessageBox.Show(message);
}
private string StringBuilder(string input, char[] CharsToRemove)
{
foreach (char c in CharsToRemove)
{
input = input.Replace(c.ToString(), "");
}
return input;
} |
| Code: | Private Sub Form1_Load(sender As Object, e As EventArgs)
Dim message As String = StringBuilder("abcef", "abc".ToCharArray())
MessageBox.Show(message)
End Sub
Private Function StringBuilder(input As String, CharsToRemove As Char()) As String
For Each c As Char In CharsToRemove
input = input.Replace(c.ToString(), "")
Next
Return input
End Function
'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Built and maintained by Todd Anglin and Telerik
'======================================================= |
_________________
Intel over amd yes. |
|
| Back to top |
|
 |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Thu Feb 19, 2009 5:52 pm Post subject: |
|
|
It works but I want it to check the string for muiltiple characters and remove all of the muiltiple chars except for one of them so..
Will become
_________________
CEF will always stay alive. |
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Fri Feb 20, 2009 9:10 pm Post subject: |
|
|
| Code: |
//C#:
private void removeDuplicates(ref string s)
{
for (int i = 0; i < s.Length; i++)
{
for (int j = 1; j < s.Length - i; j++)
{
if (s[i] == s[i+j]) //duplicate character; we need to remove it
s = s.Remove(i+j, 1);
}
}
}
|
| Code: |
'VB:
Private Sub removeDuplicates(ByRef s As String)
For i As Integer = 0 To s.Length - 1
For j As Integer = 1 To s.Length - i - 1
If s(i) = s(i + j) Then
'duplicate character; we need to remove it
s = s.Remove(i + j, 1)
End If
Next
Next
End Sub
|
I suppose.
_________________
|
|
| Back to top |
|
 |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Fri Feb 20, 2009 9:16 pm Post subject: |
|
|
| samuri25404 wrote: | | Code: |
//C#:
private void removeDuplicates(ref string s)
{
for (int i = 0; i < s.Length; i++)
{
for (int j = 1; j < s.Length - i; j++)
{
if (s[i] == s[i+j]) //duplicate character; we need to remove it
s = s.Remove(i+j, 1);
}
}
}
|
| Code: |
'VB:
Private Sub removeDuplicates(ByRef s As String)
For i As Integer = 0 To s.Length - 1
For j As Integer = 1 To s.Length - i - 1
If s(i) = s(i + j) Then
'duplicate character; we need to remove it
s = s.Remove(i + j, 1)
End If
Next
Next
End Sub
|
I suppose. |
FINALLY!
_________________
CEF will always stay alive. |
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Fri Feb 20, 2009 10:18 pm Post subject: |
|
|
It's not very efficient, but it'll be fine for small strings.
Couldn't think of anything else off the top of my head.
| Jorg hi wrote: |
Thanks anyways.
Also how can you remove all lines that don't have special characters?
|
Define special characters?
| Code: |
//C#:
bool bGoodLine;
List<string> Lines = new List<string(streamReader.ReadToEnd().Split(Environment.NewLine)));
for (int i = 0; i < Lines.Count; i++)
{
GoodLine = false;
for (int j = 0; j < Lines[i].Length; j++)
{
if (!Char.IsAlpha(Lines[i][j]) && !Char.IsDigit(Lines[i][j]))
bGoodLine = true;
}
if (!bGoodLine)
{
Lines.RemoveAt(i);
i--;
}
}
|
_________________
|
|
| Back to top |
|
 |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Sat Feb 21, 2009 8:45 am Post subject: |
|
|
Characters like basically every character except for numbers, letters, and the period character.
Wait a sec, can you make it so it can remove same strings on lines so
I'm going to use vbcrlf as a line splitter.
| Code: | general
orange
general
orange
pie
|
will be...
?
_________________
CEF will always stay alive. |
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Feb 21, 2009 10:06 am Post subject: |
|
|
| Jorg hi wrote: | Characters like basically every character except for numbers, letters, and the period character.
Wait a sec, can you make it so it can remove same strings on lines so
I'm going to use vbcrlf as a line splitter.
| Code: | general
orange
general
orange
pie
|
will be...
? |
First thing:
[I made some changes that should be used for the other code too; I was tired when I made it last night]
| Code: |
//C#:
//using System.IO;
//using System.Collections.Generic;
private string removeDuplicates(string Path)
{
try
{
StreamReader sr = new StreamReader(Path);
List<string> Text = sr.ReadToEnd().Replace("\r", String.Empty).Split("\n");
for (int i = 0; i < Text.Count; i++)
{
for (int j = 1; j < s.Count- i; j++)
{
if (Text [i] == Text [i+j]) //duplicate string; we need to remove it
{
s = s.RemoveAt(i+j);
j--;
}
}
}
}
catch (Exception e)
{ /*do something here*/ }
}
|
| Code: |
'VB:
'Imports System.IO
'Imports System.Collections.Generic
Private Function removeDuplicates(ByVal Path As String) As String
Try
Dim sr As New StreamReader(Path)
Dim Text As List(Of String) = sr.ReadToEnd().Replace(vbCr, [String].Empty).Split(vbLf)
For i As Integer = 0 To Text.Count - 1
For j As Integer = 1 To s.Count - i - 1
If Text(i) = Text(i + j) Then
'duplicate string; we need to remove it
s = s.RemoveAt(i + j)
j -= 1
End If
Next
Next
Catch e As Exception
'do something here
End Try
End Function
|
Second thing:
| Code: |
//C#:
bool bGoodLine;
List<string> Lines = new List<string>(streamReader.ReadToEnd().Replace("\r", String.Empty).Split("\n")));
for (int i = 0; i < Lines.Count; i++)
{
GoodLine = false;
for (int j = 0; j < Lines[i].Length; j++)
{
if (!Char.IsAlpha(Lines[i][j]) && !Char.IsDigit(Lines[i][j]) && Lines[i][j] != '.')
bGoodLine = true;
}
if (!bGoodLine)
{
Lines.RemoveAt(i);
i--;
}
}
|
The converter thing didn't like that, so whatever.
_________________
|
|
| Back to top |
|
 |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Sat Feb 21, 2009 2:03 pm Post subject: |
|
|
I'll have to find a way to convert this +rep.
_________________
CEF will always stay alive. |
|
| Back to top |
|
 |
BanMe Master Cheater
Reputation: 0
Joined: 29 Nov 2005 Posts: 375 Location: Farmington NH, USA
|
|
| Back to top |
|
 |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Sat Feb 21, 2009 3:23 pm Post subject: |
|
|
ahh thanks.
_________________
CEF will always stay alive. |
|
| Back to top |
|
 |
|