"How do I do this? "
Let's say I have this string. How do I remove only one space from the end? The code shown below gives me an error saying the count is out of range.
string s = "How do I do this? ";
s = s.Remove(s.Length, 1);
You just have to use this instead :
string s = "How do I do this? ";
s = s.Remove(s.Length-1, 1);
As stated here:
Remove(Int32) Returns a new string in which all the characters in the current
instance, beginning at a specified position and continuing through the
last position, have been deleted.
In an array, positions range from 0 to Length-1, hence the compiler error.
The indexing in C# are zero-based.
s = s.Remove(s.Length - 1, 1);
Just do a substring from the first character (chars are 0-based in string) and get number of chars less the string length by 1
s = s.Substring(0, s.Length - 1);
This is a little safer, just in case the last character is not a space
string s = "How do I do this? ";
s = Regex.Replace(s, #" $", "")
You have to write something in the lines of
string s = "How do I do this?
s = s.Remove(s.Length-1, 1);
Reason being that in C# when referring to indexes in arrays the first element is always at position 0 and end at Length - 1. The Length generally tells you how long a string is but doesn't map to the actual array index.
Another way to do it is;
string s = "How do I do this? ";
s=s.SubString(0,s.Length-1);
Additional :
If you would like do some additional checking for the last character being a space or anything,you can do it in this way;
string s = "How do I do this? a";//Just for example,i've added a 'a' at the end.
int index = s.Length - 1;//Get last Char index.
if (index > 0)//If index exists.
{
if (s[index] == ' ')//If the character at 'index' is a space.
{
MessageBox.Show("Its a space.");
}
else if (char.IsLetter(s[index]))//If the character at 'index' is a letter.
{
MessageBox.Show("Its a letter.");
}
else if(char.IsDigit(s[index]))//If the character at 'index' is a digit.
{
MessageBox.Show("Its a digit.");
}
}
This gives you a MessageBox with message "Its a letter".
One more thing that might be helpful,if you want to create a string with equal no. of spaces between each word,then you can try this.
string s = "How do I do this? ";
string[] words = s.Split(new char[] {' '},StringSplitOptions.RemoveEmptyEntries);//Break the string into individual words.
StringBuilder sb = new StringBuilder();
foreach (string word in words)//Iterate through each word.
{
sb.Append(word);//Append the word.
sb.Append(" ");//Append a single space.
}
MessageBox.Show(sb.ToString());//Resultant string 'sb.ToString()'.
This gives you "How do I do this? " (equal spaces between words).
Related
So I have a string, and in it, I want to replace last 3 chars with a dot. I did something but my result is not what I wanted it to be.
Here is my code:
string word = "To je";
for (int k = word.Length; k > (word.Length) - 3; k--)
{
string newWord = word.Replace(word[k - 1], '.');
Console.WriteLine(newWord);
}
The output I get is:
To j.
To .e
To.je But the output I want is: To... How do I get there? So the program is doing something similar to what I actually want it to do, but not quite. I've really been struggling with this and any help would be appreciated.
Look at this:
string newWord = word.Replace(word[k - 1], '.');
You're always replacing a single character from word... but word itself doesn't change, so on the next iteration the replacement has "gone".
You could use:
word = word.Replace(word[k - 1], '.');
(And then move the output to the end, just writing out word.)
However, note that this will replace all occurrences of any of the last three characters with a ..
The simplest way to fix all of this is to use Substring of course, but if you really want to loop, you could use a StringBuilder:
StringBuilder builder = new StringBuilder(word);
for (int k = word.Length; k > (word.Length) - 3; k--)
{
builder[k - 1] = '.';
}
word = builder.ToString();
You're replacing all instances of the character at each of those three last positions with a period. You only want to replace that one character at the end. "aaaaa" shouldn't become "....." but rather "aa...".
You're printing out newWord after calculating an intermediate value and then never doing anything with it, leaving word unchanged. You'll want to assign it back to word, after correctly adjusting the character in question.
Of course the far easier solution (both for you, and for the computer) is to simply concat a substring of the string you have that excludes the last three characters with three periods.
Assuming the string is always at least 3 characters, you could substring everything but the last three characters and then append the three dots (periods) to the end of that string.
string word = "To je";
string newWord = word.Substring(0, word.Length - 3); // Grabs everything but the last three chars
newWord += "..."; // Appends three dots at the end of the new string
Console.WriteLine(newWord);
Note: this assumes that the input string word is at least three characters. If you were to supply a shorter string, you would need to supply an extra check on the string's length.
If you don't need the original word afterwards
Using Jon Skeets method
string word = "To je";
word = word.Substring(0,word.Length - 3);
word += "...";
Console.WriteLine(word);
as #jon-skeet said, you could do this with substrings. Here are 3 ways that you could do this with substring.
You could use String.Concat
string word = "To je";
word = String.Concat(word.Substring(0,word.Length-3),"...");
Console.WriteLine(word);
You could use the + operator
string word2 = "To je";
word2 = word2.Substring(0,word.Length-3) + "...";
Console.WriteLine(word2);
You could use String.Format
string word3 = "To je";
word3 = String.Format("{0}...",word3.Substring(0,word.Length-3));
Console.WriteLine(word3);
I'm a bit late to the party but all of the other solutions posted so far do not elegantly handle the case that the string is shorter than the requested number of replacements, or an arbitrary number of replacements. Here is a general function for replacing the last n characters at the end of a string with a user specified value:
static String replace_last_n(String s, int nchars, char replacement='.')
{
nchars = Math.Min(s.Length, nchars > 0 ? nchars : 0);
return s.Substring(0, s.Length - nchars) + new String(replacement, nchars);
}
Console.WriteLine(replace_last_n("wow wow wow", 3));
Console.WriteLine(replace_last_n("wow", 3, 'x'));
Console.WriteLine(replace_last_n("", 3));
Console.WriteLine(replace_last_n("w", 3));
Console.WriteLine(replace_last_n("wow", 0));
Console.WriteLine(replace_last_n("wow", -2));
Console.WriteLine(replace_last_n("wow", 33, '-'));
Output:
wow wow ...
xxx
.
wow
wow
---
if (word.Length > 3)
Console.WriteLine(word.substring(0, word.Length - 3) + "...");
or something along those lines, no need for a loop!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove characters after specific character in string, then remove substring?
I have a string variabel that contains a text like this:
"Hello My name is B and I love soccer. I live in California."
I want to cut the text after the first '.' so the text displays
"Hello My name is B and I love soccer."
How can I do it in the simpliest way?
I tried:
Char Mychar = '.';
Stringvariabel.trimstart(Mychar);
But I guess it's wrong.
Char Mychar = '.';
Stringvariabel = Stringvariabel.Split(Mychar).First() + Mychar.toString();
If you're only interested in the first sentence, then just grab a substring starting at the beginning and ending at the '.'.
Stringvariabel.Substring(0, Stringvariabel.IndexOf('.') + 1);
You can use string.Split to get the result:
string input = "Hello My name is B and I love soccer. I live in California. ";
string result = string.Format("{0}.", input.Split('.').First());
Make use of IndexOf function will do work for you..
string input = "Hello My name is B and I love soccer. I live in California. ";
int i = input .IndexOf('.');
string result = s.Substring(0,i+1);
One convenient way is to use string.Split and ask for just the first part:
var firstPart = input.Split(new[] { '.' }, 1).First();
This is quite efficient because it won't continue processing the string after the first dot, but it will remove the dot (if it exists) and you will not be able to tell if there was a dot in the first place.
The other option is string.IndexOf and a conditional:
var index = input.IndexOf(".");
if (index != -1) {
input = input.SubString(0, index);
}
TrimStart removes characters from the start of a string that are in the list you give it. It would only remove a . if it appears at the very start.
You can find the first . and take a substring up to that point:
stringVar.Substring(0, stringVar.IndexOf('.') + 1);
You can do something like below
stringVariable.Split('.')[0]
or
stringVariable.SubString(0, stringVariable.IndexOf(".") + 1)
Hope this Helps!!
The simplest way would be to take the substring up until the first occurrence of the character.
public string TrimAtFirstChar(string s, char c)
{
int index = s.IndexOf(c);
if(index == -1) //there is no '.' in the string
return s;
return s.Substring(0, index)
}
Alternately, to avoid worrying about the case where there is no '.', you could use stringvariable.Split('.')[0].
Take a look at the String.Split method.
var myString = "Hello My name is B and I love soccer. I live in California. ";
var firstPart = myString.Split('.')[0];
var splitLine = yourString.Split('.');
if (splitLine != null && splitLine.Count > 0)
return splitLine[0];
Without split, only using Substring and IndexOf (which is more efficient when the text is very large):
int index = text.IndexOf(".") + 1;
String result = text;
if(index > 0)
result = text.Substring(0, index);
http://ideone.com/HL6GwN
Please be aware that String.Split() can have an impact, cause you create an array that contains all substrings delimited by the given separator, but you are only interested in the first occurence. So using IndexOf() and Substring() makes much more sense.
string input = "Hello My name is B and I love soccer. I live in California. ";
var index = input.IndexOf(".");
var result = index > 0
? input.Substring(0, index)
: input;
variablename.Substring(0, variablename.IndexOf('.') + 1);
Im gone substring a description text after a certain count with .Substring(0, 100). I dont want to break in the middle of a word, if thats the case i would like to get to the first whitespace before the word.
I figure i check if the next character is not a " " its in the middle of the word.
My biggest issue is how do step backwards to get the index of the " " (whitespace).
this is what i got so far
string description = "a long string";
description = Regex.Replace(description, #"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)>", String.Empty);
var newString = (description.Count() > 101) ? description.Substring(0, 101) : description;
//i tried something like this
var whatIsNext = newString.IndexOf(" ", 100, -20);
I think you're looking for String.LastIndexOf:
Reports the zero-based index position of the last occurrence of a specified Unicode character within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string.
You want something like this:
int index = s.LastIndexOf(' ', 100);
Use LastIndexOf and apply it to your substring
// The string we are searching.
string value = "Dot Net Perls";
//
// Find the last occurrence of ' '.
int index1 = value.LastIndexOf(' ');
Link Info
var whatIsNext = newString.Substring(0, newString.LastIndexOf(' '));
I'm not an expert in regular expressions and today in my project I face the need to split long string in several lines in order to check if the string text fits the page height.
I need a C# regular expression to split long strings in several lines by "\n", "\r\n" and keeping 150 characters by line maximum. If the character 150 is in the middle of an word, the entire word should be move to the next line.
Can any one help me?
It's actually a quite simple problem. Look for any characters up to 150, followed by a space. Since Regex is greedy by nature it will do exactly what you want it to. Replace it by the Match plus a newline:
.{0,150}(\s+|$)
Replace with
$0\r\n
See also: http://regexhero.net/tester/?id=75645133-1de2-4d8d-a29d-90fff8b2bab5
var regex = new Regex(#".{0,150}", RegexOptions.Multiline);
var strings = regex.Replace(sourceString, "$0\r\n");
Here you go:
^.{1,150}\n
This will match the longest initial string like this.
if you just want to split a long string into lines of 150 chars then I'm not sure why you'd need a regular expression:
private string stringSplitter(string inString)
{
int lineLength = 150;
StringBuilder sb = new StringBuilder();
while (inString.Length > 0)
{
var curLength = inString.Length >= lineLength ? lineLength : inString.Length;
var lastGap = inString.Substring(0, curLength).LastIndexOfAny(new char[] {' ', '\n'});
if (lastGap == -1)
{
sb.AppendLine(inString.Substring(0, curLength));
inString = inString.Substring(curLength);
}
else
{
sb.AppendLine(inString.Substring(0, lastGap));
inString = inString.Substring(lastGap + 1);
}
}
return sb.ToString();
}
edited to account for word breaks
This code should help you. It will check the length of the current string. If it is greater than your maxLength (150) in this case, it will start at the 150th character and (going backwards) find the first non-word character (as described by the OP, this is a sequence of non-space characters). It will then store the string up to that character and start over again with the remaining string, repeating until we end up with a substring that is less than maxLength characters. Finally, join them all back together again in a final string.
string line = "This is a really long run-on sentence that should go for longer than 150 characters and will need to be split into two lines, but only at a word boundary.";
int maxLength = 150;
string delimiter = "\r\n";
List<string> lines = new List<string>();
// As long as we still have more than 'maxLength' characters, keep splitting
while (line.Length > maxLength)
{
// Starting at this character and going backwards, if the character
// is not part of a word or number, insert a newline here.
for (int charIndex = (maxLength); charIndex > 0; charIndex--)
{
if (char.IsWhiteSpace(line[charIndex]))
{
// Split the line after this character
// and continue on with the remainder
lines.Add(line.Substring(0, charIndex+1));
line = line.Substring(charIndex+1);
break;
}
}
}
lines.Add(line);
// Join the list back together with delimiter ("\r\n") between each line
string final = string.Join(delimiter , lines);
// Check the results
Console.WriteLine(final);
Note: If you run this code in a console application, you may want to change "maxLength" to a smaller number so that the console doesn't wrap on you.
Note: This code does not take into effect any tab characters. If tabs are also included, your situation gets a bit more complicated.
Update: I fixed a bug where new lines were starting with a space.
Here is what I'm trying to accomplish. I have an object coming back from
the database with a string description. This description can be up to 1000
characters long, but we only want to display a short view of this. So I coded
up the following, but I'm having trouble in actually removing the number of
words after the regular expression finds the total count of words. Does anyone
have good way of dispalying the words which are less than the Regex.Matches?
Thanks!
if (!string.IsNullOrEmpty(myObject.Description))
{
string original = myObject.Description;
MatchCollection wordColl = Regex.Matches(original, #"[\S]+");
if (wordColl.Count < 70) // 70 words?
{
uxDescriptionDisplay.Text =
string.Format("<p>{0}</p>", myObject.Description);
}
else
{
string shortendText = original.Remove(200); // 200 characters?
uxDescriptionDisplay.Text =
string.Format("<p>{0}</p>", shortendText);
}
}
EDIT:
So this is what I got working on my own:
else
{
int count = 0;
StringBuilder builder = new StringBuilder();
string[] workingText = original.Split(' ');
foreach (string word in workingText)
{
if (count < 70)
{
builder.AppendFormat("{0} ", word);
}
count++;
}
string shortendText = builder.ToString();
}
It's not pretty, but it worked. I would call it a pretty naive way of doing this. Thanks for all of the suggestions!
I would opt to go by a strict character count rather than a word count because you might happen to have a lot of long words.
I might do something like (pseudocode)
if text.Length > someLimit
find first whitespace after someLimit (or perhaps last whitespace immediately before)
display substring of text
else
display text
Possible code implementation:
string TruncateText(string input, int characterLimit)
{
if (input.Length > characterLimit)
{
// find last whitespace immediately before limit
int whitespacePosition = input.Substring(0, characterLimit).LastIndexOf(" ");
// or find first whitespace after limit (what is spec?)
// int whitespacePosition = input.IndexOf(" ", characterLimit);
if (whitespacePosition > -1)
return input.Substring(0, whitespacePosition);
}
return input;
}
One method, if you're using at least C#3.0, would be a LINQ like the following. This is provided you're going strictly by word count, not character count.
if (wordColl.Count > 70)
{
foreach (var subWord in wordColl.Cast<Match>().Select(r => r.Value).Take(70))
{
//Build string here out of subWord
}
}
I did a test using a simple Console.WriteLine with your Regex and your question body (which is over 70 words, it turns out).
You can use Regex Capture Groups to hold the match and access it later.
For your application, I'd recommend instead simply splitting the string by spaces and returning the first n elements of the array:
if (!string.IsNullOrEmpty(myObject.Description))
{
string original = myObject.Description;
string[] words = original.Split(' ');
if (words.Length < 70)
{
uxDescriptionDisplay.Text =
string.Format("<p>{0}</p>", original);
}
else
{
string shortDesc = string.Empty;
for(int i = 0; i < 70; i++) shortDesc += words[i] + " ";
uxDescriptionDisplay.Text =
string.Format("<p>{0}</p>", shortDesc.Trim());
}
}
Are you wanting to remove 200 characters or start truncating at the 200th character? When you call original.Remove(200) you are indexing the start of the truncation at the 200th character. This is how you use Remove() for a certain number of characters to remove:
string shortendText = original.Remove(0,200);
This starts at the first character and removes 200 starting with that one. Which I imagine that's not what you're trying to do since you're shortening a description. That's merely the correct way to use Remove().
Instead of using Regex matchcollections why not just split the string? It's a lot easier and straight forward. You can set the delimiter to a space character and split that way. Not sure if that completely fixes your need but it just might. I'm not sure what your data looks like in the description. But you split this way:
String[] wordArray = original.Split(' ');
From there you can determine the word count with wordArray's Length property value.
If I was you I would go by characters as you may have many one letter words or many long words in your text.
Go through until characters <= your limit, then either find the next space and then add these characters to a new string (possibly using the SubString method) or take these characters and add a few full stops, then make a new string The later could be unproffessional I suppose.