When I have a string that I want to cut into a new string from a certain Index to a certain Index, which function do I use?
If the string was:
ABCDEFG
This would mean retrieving BCD when the two indexes specified were 1 and 3.
If endIndex points to the last character that you want to have included in the extracted substring:
int length = endIndex - startIndex + 1;
string extracted = s.Substring(startIndex, length);
If endIndex points to the first character following the desired substring (i.e. to the start of the remaining text):
int length = endIndex - startIndex;
string extracted = s.Substring(startIndex, length);
See String.Substring Method (Int32, Int32) for the official description on Microsoft Docs.
Since C# 8.0, in .NET Core and .NET 5+ only, you can use Indices and ranges
string extracted = s[startIndex..endIndex];
where the position at endIndex is excluded. This corresponds to my second example with Substring where endIndex points to the first character following the desired substring (i.e. to the start of the remaining text).
If endIndex is intended to point to the last character that you want to have included, just add one to endIndex:
string extracted = s[startIndex..(endIndex + 1)];
This becomes possible with the new Range feature of C# 8.0.
An extension method on string that uses Range to achieve this is:
public static class StringExtensions
{
public static string SubstringByIndexes(this string value, int startIndex, int endIndex)
{
var r = Range.Create(startIndex, endIndex + 1);
return value[r];
/*
// The content of this method can be simplified down to:
return value[startIndex..endIndex + 1];
// by using a 'Range Expression' instead of constructing the Range 'long hand'
*/
}
}
Note: 1 is added to endIndex when constructing the Range that's used as the end of the range is exclusive, rather than inclusive.
Which can be called like this:
var someText = "ABCDEFG";
var substring = someText.SubstringByIndexes(1, 3);
Giving a value of BCD in substring.
Unfortunately, C# doesn't natively have what you need. C# offers Substring(int startIndex, int length) instead. To achieve Substring(int startIndex, int endIndex), you will need custom implementation. Following extension method can make reusability easier/cleaner:
public static class Extensions
{
public static string Substring2(this string value, int startIndex, int endIndex)
{
return value.Substring(startIndex, (endIndex - startIndex + 1));
}
}
There is two way to substring string..
1 )
public string Substring(
int startIndex
)
Retrieves a substring from this instance. The substring starts at a specified character position.
2)
public string Substring(
int startIndex,
int length
)
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
Related
Problem statement:
Using just the ElementAt, Length, and Substring string methods and the + (concatenate)
operator, write a function that accepts a string s, a start position p, and a length l, and returns s with the characters starting in position p for a length of l removed. Don’t forget that strings start at position 0. Thus (“abcdefghijk”, 2, 4) returns “abghijk”. Don’t use any “remove” or similar built-in string gadget.
I tried to do this
static string rstring(string str, int p, int l)
{
string end= "";
for (int i=0 ; i<p; i++){
end+= str[i];
}
for (int i=p+l ; i<str.length i++){
end+= str[i];
}
return end;
}
I tried to do this but i couldn't figure out to use ElementAT and substring. Any help will be appricated.
You're using [] which is essentially the same thing as ElementAt, and if you look at your loops they're basically doing the same thing as Substring, albeit less efficiently because you're building up and throwing away a bunch of intermediate strings.
That said, I don't see why you'd use both -- you'd just use one or the other.
If you don't want to use Substring() or Remove() or any other string maninpulation I would use a simple loop (this doesn't include, but should include error handling).
for (int i=0; i < str.Length; i++)
{
if (i < p || i > l+1) end += str[i];
}
This basically just accepts a string, a start and an end and then returns a new string according to those parameters.
This is probably the easiest way to explain Substrings. They accept a start and end and then give you back a new string according to your start and end.
public string ReturnSubstring(string str, int start, int end)
{
return str.Substring(start, end);
}
var firstHalf = ReturnSubstring(myString, 0, 3);
var secondHalf = ReturnSubstring(myString, 6, myString.Length);
var newString = firstHalf + secondHalf;
I am writing a program that writes to a file that requires specific positions:
it looks something like:
writer.WriteLine("{0,-3}{1,-5}{2,-30}", data1, data2, data3);
The positions that it starts is correct however, if data1 exceeds 3 character, it pushes the format by the exceeded amount,
Is there a way to make data1 cap at 3 characters and ignore any excess characters using the writeline format?
If I understood correctly, you should "safely substring" (Substring throws an exception if you choose a length greater than the string length) your strings to the desidered lenght.
public static string SafeSubstring(this string text, int start, int length)
{
return text.Length <= start ? string.Empty
: text.Length - start <= length ? text.Substring(start)
: text.Substring(start, length);
}
Then, for example:
writer.WriteLine("{0,-3}{1,-5}{2,-30}",
data1.SafeSubstring(0, 3),
data2.SafeSubstring(0, 3),
data3.SafeSubstring(0, 3));
Use string.Substring. This code trims the string if it's longer than 3 characters:
Console.WriteLine("{0,-3}{1,-5}{2,-30}",
data1.Substring(0,data1.Length > 3 ? 3 : data1.Length),
data2, data3);
You can't solve this with formatting alone, but you can implement a (extension) method:
public static string ToLength(this object value, int length) {
if (length == 0)
return "";
else if (null == value)
return new string(' ', Math.Abs(length));
string v = value.ToString();
if (v.Length >= Math.Abs(length))
return v.Substring(0, Math.Abs(length));
else if (length < 0)
return v.PadRight(-length);
else
return v.PadLeft(length);
}
Then use it
writer.WriteLine("{0}{1}{2}",
data1.ToLength(-3),
data2.ToLength(-5),
data3.ToLength(-30));
I've implemented extension method for object, not string since you may want to put any object to stream.
I tried to create a function for print all the articles of a billing with some max length vars, but when this max length is exceeded Index out of range errors appears or in another cases total and quantity Doesn't appear in line:
Max Length variables:
private Int32 MaxCharPage = 36;
private Int32 MaxCharArticleName = 15;
private Int32 MaxCharArticleQuantity = 4;
private Int32 MaxCharArticleSellPrice = 6;
private Int32 MaxCharArticleTotal = 8;
private Int32 MaxCharArticleLineSpace = 1;
This is my current function:
private IEnumerable<String> ArticleLine(Double Quantity, String Name, Double SellPrice, Double Total)
{
String QuantityChunk = (String)(Quantity).ToString("#,##0.00");
String PriceChunk = (String)(SellPrice).ToString("#,##0.00");
String TotalChunk = (String)(Total).ToString("#,##0.00");
// full chunks with "size" length
for (int i = 0; i < Name.Length / this.MaxCharArticleName; ++i)
{
String Chunk = String.Empty;
if (i == 0)
{
Chunk = QuantityChunk + new String((Char)32, (MaxCharArticleQuantity + MaxCharArticleLineSpace - QuantityChunk.Length)) +
Name.Substring(i * this.MaxCharArticleName, this.MaxCharArticleName) +
new String((Char)32, (MaxCharArticleLineSpace)) +
new String((Char)32, (MaxCharArticleSellPrice - PriceChunk.Length)) +
PriceChunk +
new String((Char)32, (MaxCharArticleLineSpace)) +
new String((Char)32, (MaxCharArticleTotal - TotalChunk.Length)) +
TotalChunk;
}
else
{
Chunk = new String((Char)32, (MaxCharArticleQuantity + MaxCharArticleLineSpace)) +
Name.Substring(i * this.MaxCharArticleName, this.MaxCharArticleName);
}
yield return Chunk;
}
if (Name.Length % this.MaxCharArticleName > 0)
{
String chunk = Name.Substring(Name.Length / this.MaxCharArticleName * this.MaxCharArticleName);
yield return new String((Char)32, (MaxCharArticleQuantity + MaxCharArticleLineSpace)) + chunk;
}
}
private void AddArticle(Double Quantity, String Name, Double SellPrice, Double Total)
{
Lines.AddRange(ArticleLine(Quantity, Name, SellPrice, Total).ToList());
}
For example:
private List<String> Lines = new List<String>();
private void Form1_Load(object sender, EventArgs e)
{
AddArticle(2.50, "EGGS", 0.50, 1.25); // Problem: Dont show the numbers like (Quantity, Sellprice, Total)
//AddArticle(100.52, "HAND SOAP /w EP", 5.00, 502.60); //OutOfRangeException
AddArticle(105.6, "LONG NAME ARTICLE DESCRIPTION", 500.03, 100.00);
//AddArticle(100, "LONG NAME ARTICLE DESCRIPTION2", 1500.03, 150003.00); // OutOfRangeException
foreach (String line in Lines)
{
Console.WriteLine(line);
}
}
Console Output:
LINE1: EGGS
LINE2:105.6LONG NAME ARTIC 500.03 100.00
LINE3: LE DESCRIPTION
Desired output:
LINE:2.50 EGGS 0.50 1.25
LINE:100. HAND SOAP /w EP 5.00 502.60
LINE: 52
LINE:105. LONG NAME ARTIC 500.03 100.00
LINE: 60 LE DESCRIPTION
LINE:100. LONG NAME ARTIC 1,500. 150,003.
LINE: 00 LE DESCRIPTION2 03 00
You are attempting to take values (string values, ultimately) and extract specified lengths from them (chunks), where the each individual chunk of a given group goes on one line, and the next chunk(s) go on the next line(s). There are several challenges in your posted code. One of the biggest is that you may not understand what the / operator does. It is, of course, used for division, but it is integer division, meaning you get a whole number as the result. E.g. 3 / 15 gives you 0, whereas 17 / 15 would give you 1. This means your loop never runs unless the length of the value is greater than the specified chunk limit.
Another possible issue is that you only perform this check against Name, not the other items (though you may have omitted the code for them for brevity reasons).
Your current code is also creating a lot of unnecessary strings, which will lead to performance degradation. Remember that in C# strings are immutable - meaning they cannot be changed. When you "change" the value of a string, you are actually creating another copy of the string.
The crux of your requirement is how to "chunk" the values in such a way that the correct output is achieved. One way to do this is to create an extension method that will take a string value and "chunkify" it for you. One such example is found here: Split String Into Array of Chunks. I've modified it slightly to use a List<T>, but an array would work just as well.
public static List<string> SplitIntoChunks(this string toSplit, int chunkSize)
{
int stringLength = toSplit.Length;
int chunksRequired = (int)Math.Ceiling(decimal)stringLength / (decimal)chunkSize);
List<string> chunks = new List<string>();
int lengthRemaining = stringLength;
for (int i = 0; i < chunksRequired; i++)
{
int lengthToUse = Math.Min(lengthRemaining, chunkSize);
int startIndex = chunkSize * i;
chunks.Add(toSplit.Substring(startIndex, lengthToUse));
lengthRemaining = lenghtRemaining - lengthToUse;
}
return chunks;
}
Say you have a string named myString. You would use the above method like this: string[] chunks = myString.SplitIntoChunks(15);, and you would receive an array of 15 character strings (depending on the size of the string).
A quick walk-through of the code (as there is not much explanation on the page). The size of the chunk is passed into the extension method. The length of the string is recorded, and the number of chunks for that string is determined using the Math.Ceiling function.
Then a for loop is constructed with the number of chunks required as the limit. Inside the loop, the length of the chunk is determined (using the lower of either the chunk size or the remaining length of the string), the starting index is calculated based on the chunk size and the loop index, and then the chunk is extracted via Substring. Finally remaining length is calculated, and once the loop ends the chunks are returned.
One way to use this extension method in your code would look like this. The extension method will need to be in a separate, static class (I suggest building a library that has a class dedicated solely to extension methods, as they come in quite handy). Note that I haven't had time to test this, and it's a bit kludgy for my tastes, but it should at least get you going in the right direction.
private IEnumerable<string> ArticleLine(double quantity, string name, double sellPrice, double total)
{
List<string> quantityChunks = quantity.ToString("#,##0.00").SplitIntoChunks(maxCharArticleQuantity);
List<string> nameChunks = name.SplitIntoChunks(maxCharArticleName);
List<string> sellPriceChunks = sellPrice.ToString("#,##0.00").SplitIntoChunks(maxCharArticleSellPrice);
List<string> totalChunks = total.ToString("#,##0.00").SplitIntoChunks(maxCharArticleTotal);
int maxLines = (new List<int>() { quantityChunks.Count,
nameChunks.Count,
sellPriceChunks.Count,
totalChunks.Count }).Max();
for (int i = 0; i < maxLines; i++)
{
lines.Add(String.Format("{0}{1}{2}{3}",
quantityChunks.Count > i ?
quantityChunks[i].PadLeft(maxCharArticleQuantity) :
String.Empty.PadLeft(maxCharArticleQuantity),
nameChunks.Count > i ?
nameChunks[i].PadLeft(maxCharArticleName) :
String.Empty.PadLeft(maxCharArticleName, ' '),
sellPriceChunks.Count > i ?
sellPriceChunks[i].PadLeft(maxCharArticleSellPrice) :
String.Empty.PadeLeft(maxCharArticleSellPrice),
totalChunks.Count > i ?
totalChunks[i].PadLeft(maxCharArticleTotal) :
String.Empty.PadLeft(maxCharArticleTotal));
}
return lines;
}
The above code does a couple of things. First, it calls SplitIntoChunks on each of the four variables.
Next, it uses the Max() extension method (you'll need to add a reference to System.Linq if you don't already have one, plus a using directive). to determine the maximum number of lines needed (based on the highest count of the four lists).
Finally, it uses a 4for loop to build the lines. Note that I use the ternary operator (?:) to build each line. The reason I went this route is so that the lines would be properly formatted, even if one or more of the 4 values didn't have something for that line. In other words:
quantityChunks.Count > i
If the count of items in quantityChunks is greater than i, then there is an entry for that item for that line.
quanityChunks[i].PadLeft(maxCharArticleQuantity, ' ')
If the condition evaluates to true, we use the corresponding entry (and pad it with spaces to keep alignment).
String.Empty.PadLeft(maxCharArticleQuantity, ' ')
If the condition is false, we simply put in spaces for the maximum number of characters for that position in the line.
Then you can print the output like this:
foreach(string line in lines)
{
Console.WriteLine(line);
}
The portions of the code where I check for the maximum number of lines and the use of a ternary operator in the String.Format feel very kludgy to me, but I don't have time to finesse it into something more respectable. I hope this at least points you in the right direction.
EDIT
Fixed the PadLeft syntax and removed the character specified, as space is used by default.
i have a string of length 98975333 and i need to remove first 5 letters in it. Can anyone suggest the best way to do this keeping performance in mind?
I tried
str.Substring(5,str.Length);
str.Remove(0,5);
which gives me result in 0.29 sec
but i want something even faster than the above.
Problem Using StringBuilder
-> i need to substring a part of the string and to do this i need to write
StringBuilder2.ToString().Substring(anyvaluehere)"
here the conversion of StringBuilder to string by ".ToString()" takes time and in this case i cant use StringBuilder
If you are working with long strings, always use StringBuilder. This class provides you fast adding and removing characters, faster than String.Concat or it's syntactic sugar "a" + "b". Moreover StringBuilder.ToString() method has special implementation for best performance as possible.
Sorry, c# strings are not arrays; they are immutable so extracting a (possibly very long) substring involves a copy.
However, most [string utilities] accept start and end indices, for instance IndexOf and CompareInfo.Compare all take a startIndexoverload.
Perhaps if you tell us what you want to do afterward we could suggest alternatives?
Update
Here are some ways you can write performant string parsing with the immutable strings in c#. Say for instance that you need to deserialize XML data inside the string, and need to skip the first N characters. You could do something like this:
public static object XmlDeserializeFromString<T>(this string objectData, int skip)
{
var serializer = new XmlSerializer(typeof(T));
using (var reader = new StringReader(objectData))
{
for (; skip > 0 && reader.Read() != -1; skip--)
;
return (T)serializer.Deserialize(reader);
}
}
As you can see from the source. StringReader.Read() does not make a copy of the unread portion of the string, it keeps an internal index to the remaining unread portion.
Or say you want to skip the first N characters of a string, then parse the string by splitting it at every "," character. You could write something like this:
public static IEnumerable<Pair<int>> WalkSplits(this string str, int startIndex, int count, params char[] separator)
{
if (string.IsNullOrEmpty(str))
yield break;
var length = str.Length;
int endIndex;
if (count < 0)
endIndex = length;
else
{
endIndex = startIndex + count;
if (endIndex > length)
endIndex = length;
}
while (true)
{
int nextIndex = str.IndexOfAny(separator, startIndex, endIndex - startIndex);
if (nextIndex == startIndex)
{
startIndex = nextIndex + 1;
}
else if (nextIndex == -1)
{
if (startIndex < endIndex)
yield return new Pair<int>(startIndex, endIndex - 1);
yield break;
}
else
{
yield return new Pair<int>(startIndex, nextIndex - 1);
startIndex = nextIndex + 1;
}
}
}
And then use the start and end indices of the Pair to further parse the string, or extract small substrings to feed to further parsing methods.
(Pair<T> is a small struct I created similar to KeyValuePair<TKey, TValue> but with identically typed first and second values. I can provide if needed.)
Using a StringBuilder to produce and manipulate the string will help you save on resources:
StringBuilder sb = new StringBuilder();
sb.Append("text"); //to add text in front
sb.Insert(50,"text"); // to insert text
sb.Remove(50,4); // to remove text
sb.ToString(); // to produce the string
If you have a fixed length of string that you wish to store elsewhere, you can make a char array and use StringBuilder's CopyTo() method:
e.g.
char[] firstfive = new char[5];
sb.CopyTo(0,firstfive,0,5);
Edit:
Actually, the OP figured this out himself, but I'm including it on the post for reference:
To get a portion of the StringBuilder as string:
sb.ToString(intStart,intLength)
Use String.Remove() i.e
String newStr = "";
newStr = str.Remove(0,5); //This will delete 5 characters starting from 0 index
Or
newStr = str.Remove(5); //Assumes the starting position as 0 and will ddelete 5 chars from that
Read more Here
Does anyone know of an implementation of the php function mb_strcut in C#?
http://php.net/manual/en/function.mb-strcut.php
mb_strcut() extracts a substring from a string similarly to mb_substr(), but operates on bytes instead of characters. If the cut position happens to be between two bytes of a multi-byte character, the cut is performed starting from the first byte of that character. This is also the difference to the substr() function, which would simply cut the string between the bytes and thus result in a malformed byte sequence.
Thanks Dash could have not written the below without your help
public static string LimitByteLength(string input, int startByte, int byteLength)
{
var maxLength = startByte + byteLength;
return
new string(
input.SkipWhile((c, i) => GetByteCount(input.Substring(0, i + 1)) <= startByte)
.TakeWhile((c, i) => GetByteCount(input.Substring(0, i + 1)) <= maxLength).ToArray());
}
private static int GetByteCount(string input)
{
return Encoding.Unicode.GetByteCount(input);
}