c# Substring characters in a string - c#

I have string value below,
string value = "034 TH4493";
In first side,
var result = value.Substring(2,value.Length - 2);
In second side,
var result2 = value.Substring(0, 2);
result1 must be
"34TH4493"
result2 must be
34
However its not working for me and I can not solve the problem. Do I need to use another solution or what's missing in the above code ?
Thanks.

var result = value.Substring(2, value.Length - 2);
There you're actually telling it to start at index position 2 (the 4 in "034 TH4493") and then to add as many characters as the length of "034 TH4493" (10, the space counts) minus 2, which would equal 8, thus: "4 TH4493".
What you want is to tell it to remove the space by replacing it with nothing, then start at index 1, so that the "0" at index 0 is discarded, then count for all other characters except the one you're ignoring:
var result = value.Replace(" ", "").Substring(1, value.Length - 2); // -2 because "value" holds both the space and the first 0, rather than just the 0
As you may imagine by now, var result2 = value.Substring(0, 2); is actually grabbing the "03" (index 0, two characters), when you'd actually want var result2 = value.Substring(1, 2).
Alternatively, you could split the string, then grab whatever you want:
var result = value.Replace(" ", "").Substring(1, value.Length - 2);
var values = value.Split(' '); // Split at the space character
var result2 = values[0];
// or
var result2 = value.Split(' ')[0];
In cases like these, where you're unsure of what's going on, it helps to add breakpoints (F9 key with the default settings), so the application pauses when that line of code is reached, and you can explore the current values by hovering the cursor over the variables, or checking in the "Locals" tab.
EDIT: I ended mixing up the values you wanted for result and result2, should be fixed now...

string v2 = value.TrimStart('0');
var result1 = v2.Replace(" ","");
var result2 = v2.Split(' ')[0];

You can find them as;
var result1 = value.Substring(1).Replace(" ", "");
var result2 = value.Substring(1, 2);
Need to replace your white space because it doesn't magically disappear. If you wanna get "34TH4493" instead of 34TH4493 as a string, you can format it like;
result1 = string.Format("\"{0}\"", result1);

You must consider that the index for first character is zero not 1 for string.
First case
So first results starts from 3rd character but you want from second character to starting index would be 1 and ending index would be one less than the length.
var result = value.Substring(1,value.Length - 1);
Second case
For second you started from zero and want the to start from second character so started index should be 1 instead of 0.
var result2 = value.Substring(1, 2);
You must read the documentation of Substring where startIndex is "The zero-based starting character position of a substring in this instance"

Related

Taking parts out of a string, how?

So I have a server that receives a connection with the message being converted to a string, I then have this string split between by the spaces
So you have a line:
var line = "hello world my name is bob";
And you don't want "world" or "is", so you want:
"hello my name bob"
If you split to a list, remove the things you don't want and recombine to a line, you won't have extraneous spaces:
var list = line.Split().ToList();
list.Remove("world");
list.Remove("is");
var result = string.Join(" ", list);
Or if you know the exact index positions of your list items, you can use RemoveAt, but remove them in order from highest index to lowest, because if you e.g. want to remove 1 and 4, removing 1 first will mean that the 4 you wanted to remove is now in index 3.. Example:
var list = line.Split().ToList();
list.RemoveAt(4); //is
list.RemoveAt(1); //world
var result = string.Join(" ", list);
If you're seeking a behavior that is like string.Replace, which removes all occurrences, you can use RemoveAll:
var line = "hello is world is my is name is bob";
var list = line.Split().ToList();
list.RemoveAll(w => w == "is"); //every occurence of "is"
var result = string.Join(" ", list);
You could remove the empty space using TrimStart() method.
Something like this:
string text = "Hello World";
string[] textSplited = text.Split(' ');
string result = text.Replace(textSplited[0], "").TrimStart();
Assuming that you only want to remove the first word and not all repeats of it, a much more efficient way is to use the overload of split that lets you control the maximum number of splits (the argument is the maximum number of results, which is one more than the maximum number of splits):
string[] arguments = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); // split only once
User.data = arguments.Skip(1).FirstOrDefault();
arguments[1] does the right thing when there are "more" arguments, but throw IndexOutOfRangeException if the number of words is zero or one. That could be fixed without LINQ by (arguments.Length > 1)? arguments[1]: string.Empty
If you're just removing the first word of a string, you don't need to use Split at all; doing a Substring after you found the space will be more efficient.
var line = ...
var idx = line.IndexOf(' ')+1;
line = line.Substring(idx);
or in recent C# versions
line = line[idx..];

Split text into two sentences in C#

I want to divide a text into sentences.the sentence contains whitespace characters
For example:
Orginal sentence: 100 10 20 13
the result:
first sentence:100 10 20
second sentence:13
I tried split but the result was :
first:100
second:10
third:20
fourth:13
How can I do that?
You want all before the last space and the rest? You can use String.LastIndexOf and Substring:
string text = "100 10 20 13";
string firstPart = text;
string lastPart;
int lastSpaceIndex = text.LastIndexOf(' ');
if(lastSpaceIndex >= 0)
{
firstPart = text.Substring(0, lastSpaceIndex);
lastPart = text.Substring(lastSpaceIndex).TrimStart();
}
You can use Linq for this;
// This splits on white space
var split = original.Split(' ');
// This takes all split parts except for the last one
var first = split.Take(split.Count() - 1);
// And rejoins it
first = String.Join(" ", first);
// This gets the last one
var last = split.Last();
Note: This is assuming that you want the first result to be every word except for the last and the second result to be only the last... If you have different requirements please clarify your question

Substring IndexOf in c#

I have a string that looks like this: "texthere^D123456_02". But I want my result to be D123456.
this is what i do so far:
if (name.Contains("_"))
{
name = name.Substring(0, name.LastIndexOf('_'));
}
With this I remove at least the _02, however if I try the same way for ^ then I always get back texthere, even when I use name.IndexOf("^")
I also tried only to check for ^, to get at least the result:D123456_02 but still the same result.
I even tried to name.Replace("^" and then use the substring way I used before. But again the result stays the same.
texthere is not always the same length, so .Remove() is out of the question.
What am I doing wrong?
Thanks
When call Substring you should not start from 0, but from the index found:
String name = "texthere^D123456_02";
int indexTo = name.LastIndexOf('_');
if (indexTo < 0)
indexTo = name.Length;
int indexFrom = name.LastIndexOf('^', indexTo - 1);
if (indexFrom >= 0)
name = name.Substring(indexFrom + 1, indexTo - indexFrom - 1);
string s = "texthere^D123456_02";
string result= s.Substring(s.IndexOf("^") + 1);//Remove all before
result = result.Remove(result.IndexOf("_"));//Remove all after
Use the String.Split method :
var split1 = name.Split('^')[1];
var yourText = split1.Split('_')[0];
Or you could use RegExp to achieve basically the same.
Your easiest solution would be to split the string first, and then use your original solution for the second part.
string name = "texthere^D123456_02";
string secondPart = name.Split('^')[1]; // This will be D123456_02
Afterwards you can use the Substring as before.
With Regular Expression
string s = "texthere^D123456_02";
Regex r1 = new Regex(#"\^(.*)_");
MatchCollection mc = r1.Matches(s);
Console.WriteLine("Result is " + mc[0].Groups[1].Value);
An alternative to what's already been suggested is to use regex:
string result = Regex.Match("texthere^D123456_02", #"\^(.*)_").Groups[1].Value; // D123456
use regex.
Regex regex = new Regex(#"\^(.*)_");
Match match = regex.Match(name);
if(match.Success)
{
name= match.Groups[1].Value
}
An easier way would be to use Split
string s = "texthere^D123456_02";
string[] a = s.Split('^', '_');
if (a.Length == 3) // correct
{
}
Well, if you use the same code you posted, it's doing the right thing, you start to retrieve characters from the char 0 and stop when it finds "^", so what you will get is "texthere".
If you want only the value, then use this:
name = name.Substring(0, name.LastIndexOf('_')).Substring(name.IndexOf("^") + 1);
It will first remove whatever is after the "_" and whatever is before "^".
Substring takes a position and a length, so you need to actually figure out where your caret position is and where the underscore is to calculate the length
var name = "texthere^D123456_02";
if(name.Contains('_'))
{
var caretPos = name.IndexOf('^') + 1; // skip ahead
var underscorePos = name.IndexOf('_');
var length = underscorePos - caretPos;
var substring = name.Substring(caretPos, length);
Debug.Print(substring);
};
Try this and let me know how it goes
string inputtext = "texthere^D123456_02";
string pattern = #".+\^([A-Z]+[0-9]+)\_[0-9]+";
string result = Regex.Match(inputtext, pattern).Groups[1].Value;
String name = "texthere^D123456_02"
print name.split('_', '^')[1]
This splits your string at all occurrences of _ and ^ and returns the list of strings after the split. Since the string you need D123456 would be at the 1st index, (i.e. the 2nd position), I have printed out that.
If you are just wanting the "d123456" it's simple with just String.Split() there is no need for anything else. Just define the index you want afterwards. There are overloads on Split() for this very reason.
//...
var source = "texthere^D123456_02";
var result = source.Split(new char[] {'^', '_'}, StringSplitOptions.RemoveEmptyEntries)[1];
Console.WriteLine(result);
//Outputs: "D123456"
Hope this helps.

Remove the last three characters from a string

I want to remove last three characters from a string:
string myString = "abcdxxx";
Note that the string is dynamic data.
read last 3 characters from string [Initially asked question]
You can use string.Substring and give it the starting index and it will get the substring starting from given index till end.
myString.Substring(myString.Length-3)
Retrieves a substring from this instance. The substring starts at a
specified character position. MSDN
Edit, for updated post
Remove last 3 characters from string [Updated question]
To remove the last three characters from the string you can use string.Substring(Int32, Int32) and give it the starting index 0 and end index three less than the string length. It will get the substring before last three characters.
myString = myString.Substring(0, myString.Length-3);
String.Substring Method (Int32, Int32)
Retrieves a substring from this instance. The substring starts at a
specified character position and has a specified length.
You can also using String.Remove(Int32) method to remove the last three characters by passing start index as length - 3, it will remove from this point to end of string.
myString = myString.Remove(myString.Length-3)
String.Remove Method (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
myString = myString.Remove(myString.Length - 3, 3);
I read through all these, but wanted something a bit more elegant. Just to remove a certain number of characters from the end of a string:
string.Concat("hello".Reverse().Skip(3).Reverse());
output:
"he"
The new C# 8.0 range operator can be a great shortcut to achieve this.
Example #1 (to answer the question):
string myString = "abcdxxx";
var shortenedString = myString[0..^3]
System.Console.WriteLine(shortenedString);
// Results: abcd
Example #2 (to show you how awesome range operators are):
string s = "FooBar99";
// If the last 2 characters of the string are 99 then change to 98
s = s[^2..] == "99" ? s[0..^2] + "98" : s;
System.Console.WriteLine(s);
// Results: FooBar98
myString.Remove(myString.Length-3);
string test = "abcdxxx";
test = test.Remove(test.Length - 3);
//output : abcd
You can use String.Remove to delete from a specified position to the end of the string.
myString = myString.Remove(myString.Length - 3);
Probably not exactly what you're looking for since you say it's "dynamic data" but given your example string, this also works:
? "abcdxxx".TrimEnd('x');
"abc"
If you're working in C# 8 or later, you can use "ranges":
string myString = "abcdxxx";
string trimmed = myString[..^3]; // "abcd"
More examples:
string test = "0123456789", s;
char c;
c = test[^3]; // '7'
s = test[0..^3]; // "0123456"
s = test[..^3]; // "0123456"
s = test[2..^3]; // "23456"
s = test[2..7]; // "23456"
//c = test[^12]; // IndexOutOfRangeException
//s = test[8..^3]; // ArgumentOutOfRangeException
s = test[7..^3]; // string.Empty
str= str.Remove(str.Length - 3);
myString.Substring(myString.Length - 3, 3)
Here are examples on substring.>>
http://www.dotnetperls.com/substring
Refer those.
string myString = "abcdxxx";
if (myString.Length<3)
return;
string newString=myString.Remove(myString.Length - 3, 3);
Easy. text = text.remove(text.length - 3). I subtracted 3 because the Remove function removes all items from that index to the end of the string which is text.length. So if I subtract 3 then I get the string with 3 characters removed from it.
You can generalize this to removing a characters from the end of the string, like this:
text = text.remove(text.length - a)
So what I did was the same logic. The remove function removes all items from its inside to the end of the string which is the length of the text. So if I subtract a from the length of the string that will give me the string with a characters removed.
So it doesn't just work for 3, it works for all positive integers, except if the length of the string is less than or equal to a, in that case it will return a negative number or 0.
Remove the last characters from a string
TXTB_DateofReiumbursement.Text = (gvFinance.SelectedRow.FindControl("lblDate_of_Reimbursement") as Label).Text.Remove(10)
.Text.Remove(10)// used to remove text starting from index 10 to end
items.Remove(items.Length - 3)
string.Remove() removes all items from that index to the end. items.length - 3 gets the index 3 chars from the end
You can call the Remove method and pass the last 3 characters
str.Substring(str.Length-3)
Complete code can be
str.Remove(str.Substring(str.Length-3));

Extract matching text using regular expression

I have to extract summary from newspaper article . The summary is extracted based on given Keyword and according to below mentioned rules .
Summary should be of 200 characters.
Start printing from that sentence in article as soon as keyword
appears in that sentence and print upto 200 characters
If the matching sentence occurs towards ending of article such that
summary is coming out to be less than 200 characters , then move
back from matching sentence towards previous sentences uptill
finally 200 charcters containing matching sentence are printed
finally.
What I have done untill now is ...
var regex = new Regex(keyword+#"(.{0,200})");
foreach (Match match in regex.Matches(input))
{
var result = match.Groups[1].Value;
Console.WriteLine(result);
// work with the result
}
The above code successfully reaches the first matching sentence but starts printing AFTER the keyword upto 200 characters rather than beginning of matching sentence.
Also there is no backtracking if end of article is reached before 200 characters are printed.
Please guide me how should I proceed . Even if somebody doesn't know complete solution , PLEASE do help me out in sub parts of question .
var nextIndex = input.IndexOf(keyword);
while (nextIndex != -1)
{
var index = nextIndex;
// To start the 200chars result from right after the keyword, do instead:
// var index = nextIndex + keyword.Length;
// If you want to stop after you reached the end of the text once:
// var end = false;
if (index + 200 >= input.Length)
{
index = input.Length - 200;
// If you want to stop after you reached the end of the text once:
// var end = true;
}
var result = index < 0 ? input : input.Substring(index, 200);
Console.WriteLine(result);
// If you want to stop after you reached the end of the text once:
// if (end) { break; }
nextIndex = input.IndexOf(keyword, nextIndex + 1);
}
And if you want to search to be case insensitive, just add StringComparison.OrdinalIgnoreCase as another parameter in both IndexOfs.
Use this instead,
var regex = new Regex( #"(" + keyword+ #".{0,200})");
This will make sure that the keyword is also included. Otherwise you can also use this
var result = match.Value;
Further you have specified {0,200} so it will match any instance which is of size between 0 and 200 so it will match any number of characters until the end of article is reached. Let me exactly know what you want to achieve in this regard.
If you want the expression to return the result from the start of the sentence, try doing this
var regex = new Regex( #"\.(.+?" + keyword+ #".*)");
But in this case, you will have to manually remove the excess characters as this regular expression tends to fetch more characters then you expected. It will fetch characters from the beginning of the sentence containing the keyword till the end of paragraph.
Is using regex a requirement? Here's a rough alternative if it's not:
var index = input.IndexOf(keyword) + keyword.Length;
var remaining = input.Length - index;
index = remaining >= 200 ? index : index -= 200 - remaining;
Console.WriteLine(input.Substring(index, 200));

Categories

Resources