Remove Words and Set Length

S

Shapper

Hello,

I have a string with words separated by dashes:

"this-is-an-example-and-it-is-possible-to-make-it-work"

I need to:
1 - Remove all words with less than 3 letters;
2 - Make the string less than 20 characters.
But never breaking a word or ending in a dash.

How can I do this?

Thank You,
Miguel
 
R

Rick Lones

Shapper said:
Hello,

I have a string with words separated by dashes:

"this-is-an-example-and-it-is-possible-to-make-it-work"

I need to:
1 - Remove all words with less than 3 letters;
2 - Make the string less than 20 characters.
But never breaking a word or ending in a dash.

How can I do this?

Thank You,
Miguel

If you remove all the 2-letter words and all the dashes you are still left with
"thisexampleandpossiblemakework", which is 30 characters. So your example does
not seem to have a solution or else the problem is not well stated. What would
be the expected output from this example input?
 
S

Shapper

I think I explained myself wrong.

Consider the initial string (no dashes):

"this is an example and it is possible to make it work"

Remove all words with less then 3 letters:

"this example and possible make work"

Strip to length, let's say 29 letters:

"this example and possible mak"

Remove broken words and last space if needed:

"this example and possible"

Add dashes:

"this-example-and-possible"


Basically that's it ... Maybe not in this order.

But the result would be this one ...
 
R

Rick Lones

Shapper said:
I think I explained myself wrong.

Consider the initial string (no dashes):

"this is an example and it is possible to make it work"

Remove all words with less then 3 letters:

"this example and possible make work"

Strip to length, let's say 29 letters:

"this example and possible mak"

Remove broken words and last space if needed:

"this example and possible"

Add dashes:

"this-example-and-possible"


Basically that's it ... Maybe not in this order.

But the result would be this one ...

Then you might check out the String.Split() function.
 
S

Shapper

Peter,

I am not trying to have anyone write the code for me.

I was having problem when using Regex so I am doing the following:

value = Regex.Replace(slug, @"\s+", " ").Trim();

value = String.Join("-", value.Split(' ').Where(x => x.Length >= 3));

value = value.Substring(0, value.Length <= 20 ? value.Length : 20).Trim();

I am able to remove the words and replace the spaces by dashes.

Not sure if this is the best way but it was the closer I got.

In this code my problem is to not have broken words.
 
J

Jeff Johnson

Any idea on how to avoid the trimming of the last word?

What does that mean? As stated, it sounds to me like your string is
"My-dog's-name-is-Ralph" and you're getting an array with

My
dog's
name
is
Ral
 
S

Shapper

What? No. With the following:

String value = "My dog name is ralph";
value = Regex.Replace(value, @"\s+", " ").Trim();
value = String.Join("-", value.Split(' ').Where(x => x.Length >= 3));
value = value.Substring(0, value.Length <= 12 ? value.Length : 12).Trim();

I get "dog-name-ral" and I would need "dog-name".

So it is under 12 characters, all words length is 3 or higher, it doesn't end with "-" and the last work is not "broken" ... Which it is ...
 
R

Rick Lones

Shapper said:
Any idea on how to avoid the trimming of the last word?

Yes. Don't append it to the output if it and its preceding '-' won't fit. My
suggestion has nothing to do with regular expressions, just simple string
manipulations starting from String.Split().
 
J

Jeff Johnson

What? No. With the following:

String value = "My dog name is ralph";
value = Regex.Replace(value, @"\s+", " ").Trim();
value = String.Join("-", value.Split(' ').Where(x => x.Length >= 3));
value = value.Substring(0, value.Length <= 12 ? value.Length :
12).Trim();

I get "dog-name-ral" and I would need "dog-name".

So it is under 12 characters, all words length is 3 or higher, it doesn't
end with "-" and the last work is not "broken" ... Which it is ...

I don't understand why you're using regular expressions. Split the string on
the hypen, resulting in an array of strings. Loop through those strings and
check the length of each word. If it's 3 characters or more, add it to your
string(builder) AS LONG AS it won't overflow the output string.

Those are the requirements as I understood them, and they seem pretty simple
to implement. I mean it's string manipulation and basic math, right?
 
J

Jeff Johnson

What? No. With the following:

String value = "My dog name is ralph";
value = Regex.Replace(value, @"\s+", " ").Trim();
value = String.Join("-", value.Split(' ').Where(x => x.Length >= 3));
value = value.Substring(0, value.Length <= 12 ? value.Length :
12).Trim();

I get "dog-name-ral" and I would need "dog-name".

So it is under 12 characters, all words length is 3 or higher, it doesn't
end with "-" and the last work is not "broken" ... Which it is ...

I think your main problem is that you're trying to manipulate the string "in
place" instead of tearing it apart and creating a completely different
string. (Leaving aside all considerations of string immutability,
please....)
 
S

Shapper

I think I was able to do it this way:

IList<String> words = value.Split(' ').Where(x => x.Length >= minimumWordLength).ToList();

StringBuilder slug = new StringBuilder();

foreach (String word in words)
if (slug.Length + word.Length < maximumLength)
slug.Append(word).Append("-");

slug.Remove(slug.Length - 1, 1);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top