split string multiple characters without space C# - c#

I have a field that is displayed inside a div and the problem is that it doesn't break line if a user puts in a string thats longer then the number of characters that can fit outside the line within a div, it basically stretches outside the div. How can I insert a space in every word 'thats in a string' that has a seuqence of 20 characters or more without a space between . For example, now I am doing something like this
string words
Regex.Replace(words, "(.{" + 20 + "})", "$1" + Environment.NewLine);
But that just inserts a line break at every 20 character oppose to a sequence without a space. I am really not that good with regular expressions so the code above is something I found.

Would a CSS solution work better?
word-wrap:break-word;
example:
http://jsfiddle.net/45Fq4/

To solve this with regex you could use #"(\S{20})" as your pattern.
\S will match any non-whitespace character which I believe fits your criteria since it will then only work if it finds 20 or more non-whitespace characters in a row.
Example usage is:
string words = "It should break after \"t\": abcdefghijklmnopqrstuvwxyz";
string output = Regex.Replace(words, #"(\S{20})", "$1" + Environment.NewLine);

this code worked for me:
string words
string outputwords = words;
int CharsWithoutSpace = 0;
for (int i = 0; i < outputwords.Length; i++)
{
if (outputwords[i] == ' ')
{
CharsWithoutSpace = 0;
}
else
{
CharsWithoutSpace++;
if (CharsWithoutSpace >= 20)
{
outputwords = outputwords.Insert(i + 1, Environment.NewLine);
CharsWithoutSpace = 0;
}
}
}
Console.WriteLine(outputwords);

Related

Remove all characters after the fourth space in a string

I want to remove all characters after the fourth space in a string.
Example:
Source: AAD BCCD QWD SDKE DJQWEK DJT
Result: AAD BCCD QWD SDKE
I tried to use 'String.indexof'. but, I couldn't.
Here is my code:
Result = source.Substring(source.IndexOf(string.Empty, source.IndexOf(string.Empty) + 3));
You could try this:
string result = string.Join(" ", source.Split(' ').Take(4));
This splits the original source string at each space character, takes the first 4 occurrences and concatenates them with a space character.
It will also work correctly in cases where there are less than 4 counts of spaces in the source string.
Maybe try this (if it's still actuall of course):
string Source = "AAD BCCD QWD SDKE DJQWEK DJT"
int space = GetNthIndex(Source, ' ', 4);
string result = sample.Substring(0, space);
You can make a loop with a counter and check each character. Pseudocode:
counter = 0;
foreach(character in string)
if(counter > 4)
exit;
else if(character == space)
counter++;
output character
else
output character

Replacing first occurence of a word enclosed in spaces in a string

// There's some similar questions on SO, but none of them seem to cover both replacing a whole word (enclosed in spaces) and its first occurence. Using both at the same time is what is causing me problems.
I want to replace the first occurence of a word surrounded by spaces and I'm running into some problems.
I have a string in range.Text that contains a long string. I want to find a words alike "#val1" "#val2" etc. and replace them with values from my values list. Here is how I do that :
while(i < valueCount && range.Text.Contains("#val"))
{
for (int j = 0; j < valueLimit; j++)
{
string pattern = $#"\b#val{ j + 1 }\b";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
Match match = regex.Match(range.Text);
if (match.Success)
{
range.Text = regex.Replace(range.Text, values[j], 1);
i++;
}
}
}
Now the problem is that for some reason match.Success is never true, even though I'm pretty sure that there's plenty of values like those I search for in it.
// Example string -
"1\t#val1\r2\t#val2\r3\t#val3\r4\t#val4\r5\t#val5\r6\t#val6\r7\t#val7\r8\t#val8\r9\t#val9\r10\t#val10\r11\t#val11\r12\t#val12\r13\t#val13\r14\t#val14\r15\t#val15\r\r"
The \t s and \r s I expect to be ignored, but spaces are what is important to me. Otherwise I'll have #val110 replaced when loop is at #val11 or #val10. Two vals will never be separated with just a tab. They will always be enclosed in two spaces in the long string.
The issue appears to be the leading \b in your pattern. With that in place, the match always fails.
The trailing one is essential so that #val1 doesn't also incorrectly match #val10, but I'm not seeing what the leading one is for, and it's causing the match to fail.
Try changing:
string pattern = $#"\b#val{ j + 1 }\b";
to
string pattern = $#"#val{ j + 1 }\b";
Other than that, the code seems to achieve what you describe.
Hope this helps

Capture two blocks in a string

I have a string that's in this format:
Message: Something bad happened in This.Place < Description> Some sort of information here< /Description>< Error> Some other stuff< /Error>< Message> Some message here.
I can't seem to figure out how to match everything in the Description block and also everything in the Message block using regex.
My question is in two parts: 1.) Is regex the right choice for this?
2.) If so, how can I match those two blocks and exclude the rest?
I can match the first part with a simple < Description>.*< /Description>, but can't match < Message>. I've tried excluding everything inbetween by trying to use what's described here http://blog.codinghorror.com/excluding-matches-with-regular-expressions/
With all the disclaimers about parsing xml in regex, it's still good do know how to do this with regex.
For instance, if you had your back against the wall, this would works for the < Description> tag (adapt it for the other tag).
(?<=< Description>).*?(?=< /Description>)
Some things you need to know:
The (?<=< Description>) is a lookbehind that asserts that at that position in the string, what precedes is < Description>. So if you change the spaces in your tag, all bets are off. To handle potential typing errors (depending on the origin of your text), you can insert optional spaces: (?<=< *Description *>) where the * repeats the space character zero or more times. The lookbehind is only an assertion, it does not consume any characters.
The .*? lazily eats up all characters until it can find what follows...
Which is the (?=< /Description>) lookahead that asserts that at that position in the string, what follows is < /Description>
In code, this becomes something like:
description = Regex.Match(yourstring, "(?<=< *Description *>).*?(?=< */Description *>)").Value;
This is how I'd parse it. Caveat: I've written the regex assuming the format shown in the example you've provided is pretty rigid; if the data varies a little (say, there isn't always a space after the '<' characters), you'll need to tweak it a little. But this should get you going.
var text = "Message: Something bad happened in This.Place < Description> Some"+
" sort of information here< /Description>< Error> Some other stuff"+
"< /Error>< Message> Some message here.";
var regex = new Regex(
"^.*?<\\sDescription\\>(?<description>.*?)<\\s/Description\\>"+
".*?<\\sMessage\\>(?<message>.*?)$",
RegexOptions.IgnoreCase | RegexOptions.Singleline
);
var matches = regex.Match(text);
if (matches.Success) {
var desc = matches.Groups["description"].Value;
// " Some sort of information here"
var msg = matches.Groups["message"].Value;
// " Some message here."
}
It was fairly difficult to try to remove the non-XML-formatted data from the text, so IndexOf and Substring ended up being what I used. IndexOf will find the index of a specified character or string, and Substring captures characters based on a starting point and a count of how many it should capture.
int descriptionBegin = 0;
int descriptionEnd = 0;
int messageBegin = 0;
int messageEnd = 0;
foreach (string j in errorList)
{
descriptionBegin = j.IndexOf("<Description>") + 13; // starts after the opening tag
descriptionEnd = j.IndexOf("</Description>") - 13; // ends before the closing tag
messageBegin = j.IndexOf("<Message>") + 9; // starts after the opening tag
messageEnd = j.IndexOf("</Message>") - 9; // ends before the closing tag
descriptionDiff = descriptionEnd - descriptionBegin; // amount of chars between tags
messageDiff = messageEnd - messageBegin; // amount of chars between tags
string description = j.Substring(descriptionBegin, descriptionDiff); // grabs only specified amt of chars
string message = j.Substring(messageBegin, messageDiff); // grabs only specified amt of chars
}
Thanks #Lucius for the suggestion.
#Darryl that actually looks like it might work. Thanks for the thorough answer...I might try that out for other stuff in the future (non-XML of course :))

Length of string WITHOUT spaces (C#)

Quick little question...
I need to count the length of a string, but WITHOUT the spaces inside of it.
E.g. for a string like "I am Bob", string.Length would return 8 (6 letters + 2 spaces).
I need a method, or something, to give me the length (or number of) just the letters (6 in the case of "I am Bob")
I have tried the following
s.Replace (" ", "");
s.Replace (" ", null);
s.Replace (" ", string.empty);
to try and get "IamBob", which I did, but it didn't solve my problem because it still counted "" as a character.
Any help?
This returns the number of non-whitespace characters:
"I am Bob".Count(c => !Char.IsWhiteSpace(c));
Demo
Char.IsWhiteSpace:
White space characters are the following Unicode characters:
Members of the SpaceSeparator category, which includes the characters SPACE (U+0020), OGHAM SPACE MARK (U+1680), MONGOLIAN VOWEL SEPARATOR (U+180E), EN QUAD (U+2000), EM QUAD (U+2001), EN SPACE (U+2002), EM SPACE (U+2003), THREE-PER-EM SPACE (U+2004), FOUR-PER-EM SPACE (U+2005), SIX-PER-EM SPACE (U+2006), FIGURE SPACE (U+2007), PUNCTUATION SPACE (U+2008), THIN SPACE (U+2009), HAIR SPACE (U+200A), NARROW NO-BREAK SPACE (U+202F), MEDIUM MATHEMATICAL SPACE (U+205F), and IDEOGRAPHIC SPACE (U+3000).
Members of the LineSeparator category, which consists solely of the LINE SEPARATOR character (U+2028).
Members of the ParagraphSeparator category, which consists solely of the PARAGRAPH SEPARATOR character (U+2029).
The characters CHARACTER TABULATION (U+0009), LINE FEED (U+000A), LINE TABULATION (U+000B), FORM FEED (U+000C), CARRIAGE RETURN (U+000D), NEXT LINE (U+0085), and NO-BREAK SPACE (U+00A0).
No. It doesn't.
string s = "I am Bob";
Console.WriteLine(s.Replace(" ", "").Length); // 6
Console.WriteLine(s.Replace(" ", null).Length); //6
Console.WriteLine(s.Replace(" ", string.Empty).Length); //6
Here is a DEMO.
But what are whitespace characters?
http://en.wikipedia.org/wiki/Whitespace_character
You probably forgot to reassign the result of Replace. Try this:
string s = "I am bob";
Console.WriteLine(s.Length); // 8
s = s.Replace(" ", "");
Console.WriteLine(s.Length); // 6
A pretty simple way is to write an extension method that will do just that- count the characters without the white spaces. Here's the code:
public static class MyExtension
{
public static int CharCountWithoutSpaces(this string str)
{
string[] arr = str.Split(' ');
string allChars = "";
foreach (string s in arr)
{
allChars += s;
}
int length = allChars.Length;
return length;
}
}
To execute, simply call the method on the string:
string yourString = "I am Bob";
int count = yourString.CharCountWithoutSpaces();
Console.WriteLine(count); //=6
Alternatively, you can split the string an way you want if you don't want to include say, periods or commas:
string[] arr = str.Split('.');
or:
string[] arr = str.Split(',');
this is fastest way:
var spaceCount = 0;
for (var i 0; i < #string.Lenght; i++)
{
if (#string[i]==" ") spaceCount++;
}
var res = #string.Lenght-spaceCount;
Your problem is probably related to Replace() method not actually changing the string, rather returning the replaced value;
string withSpaces = "I am Bob";
string withoutSpaces = withSpaces.Replace(" ","");
Console.WriteLine(withSpaces);
Console.WriteLine(withoutSpaces);
Console.WriteLine(withSpaces.Length);
Console.WriteLine(withoutSpaces.Length);
//output
//I am Bob
//IamBob
//8
//6
You can use a combination of Length and Count functions on the string object. Here is a simple example.
string sText = "This is great text";
int nSpaces = sText.Length - sText.Count(Char.IsWhiteSpace);
This will count single or multiple (consistent) spaces accurately.
Hope it helps.

How to find the number of occurrences of a letter in only the first sentence of a string?

I want to find number of letter "a" in only first sentence. The code below finds "a" in all sentences, but I want in only first sentence.
static void Main(string[] args)
{
string text; int k = 0;
text = "bla bla bla. something second. maybe last sentence.";
foreach (char a in text)
{
char b = 'a';
if (b == a)
{
k += 1;
}
}
Console.WriteLine("number of a in first sentence is " + k);
Console.ReadKey();
}
This will split the string into an array seperated by '.', then counts the number of 'a' char's in the first element of the array (the first sentence).
var count = Text.Split(new[] { '.', '!', '?', })[0].Count(c => c == 'a');
This example assumes a sentence is separated by a ., ? or !. If you have a decimal number in your string (e.g. 123.456), that will count as a sentence break. Breaking up a string into accurate sentences is a fairly complex exercise.
This is perhaps more verbose than what you were looking for, but hopefully it'll breed understanding as you read through it.
public static void Main()
{
//Make an array of the possible sentence enders. Doing this pattern lets us easily update
// the code later if it becomes necessary, or allows us easily to move this to an input
// parameter
string[] SentenceEnders = new string[] {"$", #"\.", #"\?", #"\!" /* Add Any Others */};
string WhatToFind = "a"; //What are we looking for? Regular Expressions Will Work Too!!!
string SentenceToCheck = "This, but not to exclude any others, is a sample."; //First example
string MultipleSentencesToCheck = #"
Is this a sentence
that breaks up
among multiple lines?
Yes!
It also has
more than one
sentence.
"; //Second Example
//This will split the input on all the enders put together(by way of joining them in [] inside a regular
// expression.
string[] SplitSentences = Regex.Split(SentenceToCheck, "[" + String.Join("", SentenceEnders) + "]", RegexOptions.IgnoreCase);
//SplitSentences is an array, with sentences on each index. The first index is the first sentence
string FirstSentence = SplitSentences[0];
//Now, split that single sentence on our matching pattern for what we should be counting
string[] SubSplitSentence = Regex.Split(FirstSentence, WhatToFind, RegexOptions.IgnoreCase);
//Now that it's split, it's split a number of times that matches how many matches we found, plus one
// (The "Left over" is the +1
int HowMany = SubSplitSentence.Length - 1;
System.Console.WriteLine(string.Format("We found, in the first sentence, {0} '{1}'.", HowMany, WhatToFind));
//Do all this again for the second example. Note that ideally, this would be in a separate function
// and you wouldn't be writing code twice, but I wanted you to see it without all the comments so you can
// compare and contrast
SplitSentences = Regex.Split(MultipleSentencesToCheck, "[" + String.Join("", SentenceEnders) + "]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
SubSplitSentence = Regex.Split(SplitSentences[0], WhatToFind, RegexOptions.IgnoreCase | RegexOptions.Singleline);
HowMany = SubSplitSentence.Length - 1;
System.Console.WriteLine(string.Format("We found, in the second sentence, {0} '{1}'.", HowMany, WhatToFind));
}
Here is the output:
We found, in the first sentence, 3 'a'.
We found, in the second sentence, 4 'a'.
You didn't define "sentence", but if we assume it's always terminated by a period (.), just add this inside the loop:
if (a == '.') {
break;
}
Expand from this to support other sentence delimiters.
Simply "break" the foreach(...) loop when you encounter a "." (period)
Well, assuming you define a sentence as being ended with a '.''
Use String.IndexOf() to find the position of the first '.'. After that, searchin a SubString instead of the entire string.
find the place of the '.' in the text ( you can use split )
count the 'a' in the text from the place 0 to instance of the '.'
string SentenceToCheck = "Hi, I can wonder this situation where I can do best";
//Here I am giving several way to find this
//Using Regular Experession
int HowMany = Regex.Split(SentenceToCheck, "a", RegexOptions.IgnoreCase).Length - 1;
int i = Regex.Matches(SentenceToCheck, "a").Count;
// Simple way
int Count = SentenceToCheck.Length - SentenceToCheck.Replace("a", "").Length;
//Linq
var _lamdaCount = SentenceToCheck.ToCharArray().Where(t => t.ToString() != string.Empty)
.Select(t => t.ToString().ToUpper().Equals("A")).Count();
var _linqAIEnumareable = from _char in SentenceToCheck.ToCharArray()
where !String.IsNullOrEmpty(_char.ToString())
&& _char.ToString().ToUpper().Equals("A")
select _char;
int a =linqAIEnumareable.Count;
var _linqCount = from g in SentenceToCheck.ToCharArray()
where g.ToString().Equals("a")
select g;
int a = _linqCount.Count();

Categories