-1

I got a question in vb.net can I identify each of the character in string for an example i got a string of "Hello!. Good Afternoon!"

from this string can i trim away the period symbol? Thank you

  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [ask] page for help clarifying this question. Do you want to Remove all the `.`? Remove the 7th char of the string? Remove any consecutive Ponctuation (ex:`"1...2"` -> `"1.2"`? What will be the expected Output on `"//1..2..--.3!!"` ? – Drag and Drop Mar 17 '17 at 10:36

2 Answers2

1

You should look at the methods of the String class, as they support different forms of string manipulation.

At its simplest, the Replace() method can be used to replace all occurrences of a period character with an empty string.

Alternatively, you can use the IndexOf() method to locate a specific string (e.g. the period) and the Remove() method to remove that character.

Phylyp
  • 1,659
  • 13
  • 16
0

According to my 8-ball Magic, you actually want to :

Remove concecutive punctuation from a string:

  1. With a Regex we are going to find all punctuation in the string.
  2. The index of Match will be into an int[].
  3. We will go iterate throught the array to find if the index is concecutive to the last punctuation index.
  4. We will delete all the punctuation starting by the last one. Because starting with the 1rst will modify the index.

Code:

string Input = "....Thalassius! vero ea--*/-*/-- tempestate+- fectus";
string Output = Input;

var regex = new Regex(@"[^\w\s]|_");    // *1.
var matches = regex.Matches(Input) ;
var MatchesIndex = matches  .Cast<Match>()
                            .Select(match => match.Index)
                            .ToArray();    // *2.

int last = 0;
List<int> toDelete = new List<int>();

for (int i = 0; i < MatchesIndex.Length; i++)    // *3.
{
    if ( MatchesIndex[i] == last + 1)
        toDelete.Add(MatchesIndex[i]);
    last = MatchesIndex[i];
}

foreach (int i in toDelete.OrderByDescending(x => x))    // *4.
    Output = Output.Remove(i, 1);

Console.WriteLine("Input : " + Input);
Console.WriteLine("Output : " + Output);

C# Snippet

You can learn more about the regex used, thanks to @John Kugelman.

Community
  • 1
  • 1
Drag and Drop
  • 2,672
  • 3
  • 25
  • 37