From following string String1 I want to remove everything before "am" and everything after "Uhr"
string String1 = "Angebotseröffnung am 27.03.2014, 11:00 Uhr, Ort: Vergabestelle, siehe a).";
So at the end I have this string. "am 27.03.2014, 11:00 Uhr".
I am using this code but I know this is not a good approach. Can some one help me with better options.
String1 = String1.Replace("Angebotseröffnung", "");
String1 = String1.Replace("Ort", "");
String1 = String1.Replace("Vergabestelle", "");
String1 = String1.Replace("siehe", "");
String1 = String1.Replace("a)", "");
Try something like this:
var string1 = "Angebotseröffnung am 27.03.2014, 11:00 Uhr, Ort: Vergabestelle, siehe a).";
var startIndex = string1.IndexOf("am"); // 18
var endIndex = string1.IndexOf("Uhr") + 3; // 42
// Get everything between index 18 and 42
var result = string1.Substring(startIndex, endIndex - startIndex);
// result: "am 27.03.2014, 11:00 Uhr"
Another option is to use Regex.Match.
string output = Regex.Match(String1, "am.*Uhr").Value;
But it will work only if you definitely have am and Uhr in your string.
Depending on your input you may require am.*?Uhr or (?:a|p)m.*?Uhr regex.
If the format is strict and always contains Angebotseröffnung am and Uhr this is the most efficient:
string String1 = "Angebotseröffnung am 27.03.2014, 11:00 Uhr, Ort: Vergabestelle, siehe a).";
string result = null;
string startPattern = "Angebotseröffnung am ";
int start = String1.IndexOf(startPattern);
if (start >= 0)
{
start += startPattern.Length;
string endPattern = " Uhr";
int end = String1.IndexOf(endPattern, start);
if (end >= 0)
result = String1.Substring(start, end - start + endPattern.Length);
}
if you know that the format will always be the same =>
var r = new Regex("am \d{1,2}.\d{1,2}.\d{4} Uhr");
var result = r.Match(input);
With StringBuilder, Replace works the same way as with strings but it doesn't need its result to be assigned
class Program
{
static void Main()
{
const string samplestring = "hi stack Over Flow String Replace example.";
// Create new StringBuilder from string
StringBuilder str = new StringBuilder(samplestring );
// Replace the first word
// The result doesn't need assignment
str.Replace("hi", "hello");
Console.WriteLine(b);
}
}
Output Will be -->
hello stack Over Flow String Replace example
if the string will always contain am and Uhr, and will always have am before Uhr:
string String1 = "Angebotseröffnung am 27.03.2014, 11:00 Uhr, Ort: Vergabestelle, siehe a).";
int indexOfAm = String1.IndexOf("am");
int indexOfUhr = String1.IndexOf("Uhr");
string final = String1.Substring(indexOfAm, (indexOfUhr + 3) - indexOfAm);
Try this
String1 = String1.Substring(String1.IndexOf("am"), (String1.IndexOf("Uhr") - String1.IndexOf("am"))+3);
Related
str="Brand : TROLLBEADS";
int length = str.Length;
length = length - 6;
str = str.Substring(6, length);
i want to display "TROLLBEADS"
and want to discard other remaining string
You can split the string using : delimiter if it is fixed
var result = str.Split(':')[1].Trim();
or if your string can have multiple : in that case
var result = str.Substring(str.IndexOf(":") + 1).Trim();
Split is nice, but a bit too much. You can just specify the start position for substring.
string str="Brand : TROLLBEADS";
string val = str.Substring(str.IndexOf(":") + 1);
Console.WriteLine(val);
I have a list of strings in format like this:
Web.WebClient.Areas.Scada.Services.IScadaManualOverrideService,Web.WebClient.TDMSWebApp
I need only the part from comma sign to the first dot sign.
For example above it should return this string: IScadaManualOverrideService
Anyone has an idea how can I do this and get substrings if I have list of strings like first one?
from comma sign to the first dot sign
You mean from dot to comma?
You can split the string by comma first, then split by dot and take the last:
string text = "Web.WebClient.Areas.Scada.Services.IScadaManualOverrideService,Web.WebClient.TDMSWebApp";
string result = text.Split(',')[0].Split('.').Last(); // IScadaManualOverrideService
Splitting strings is not what can be called effective solution. Sorry can't just pass nearby.
So here is another one
string text = "Web.WebClient.Areas.Scada.Services.IScadaManualOverrideService,Web.WebClient.TDMSWebApp";
var end = text.IndexOf(',');
var start = text.LastIndexOf('.', end) + 1;
var result = text.Substring(start, end - start);
Proof woof woof.
Bullet-proof version (ugly)
string text = "IScadaManualOverrideService";
//string text = "Services.IScadaManualOverrideService";
//string text = "IScadaManualOverrideService,";
//string text = "";
var end = text.IndexOf(',');
var start = text.LastIndexOf('.', (end == -1 ? text.Length - 1 : end)) + 1;
var result = text.Substring(start, (end == -1 ? text.Length : end) - start);
Insert this if hacker attack is expected
if(text == null)
return "Stupid hacker, die!";
string s = "Web.WebClient.Areas.Scada.Services.IScadaManualOverrideService,Web.WebClient.TDMSWebApp";
string s1 = s.Substring(0, s.IndexOf(","));
string s2 = s1.Substring(s1.LastIndexOf(".") + 1);
string input = "Web.WebClient.Areas.Scada.Services.IScadaManualOverrideService,Web.WebClient.TDMSWebApp";
int commaIndex = input.IndexOf(',');
string remainder = input.Substring(0, commaIndex);
int dotIndex = remainder.LastIndexOf('.');
string output = remainder.Substring(dotIndex + 1);
This can be written a lot shorter, but for the explanation i think this is more clear
sampleString.Split(new []{','})[0].Split(new []{'.'}).Last()
string s = "Web.WebClient.Areas.Scada.Services.IScadaManualOverrideService,Web.WebClient.TDMSWebApp";
string subStr = new string(s.TakeWhile(c => c != ',').ToArray());
string last = new string(subStr.Reverse().TakeWhile(c => c != '.').Reverse().ToArray());
Console.WriteLine(last); // output: IScadaManualOverrideService
public String GetDirectory(String Path)
{
Console.WriteLine("Directorul: ");
var start = Path.IndexOf(":") + 6;
var match2 = Path.Substring(start, Path.IndexOf(".") - start);
return Path;
}
I need to get the path string between the 2 characters in this string:
"C:\Documents\Text.txt"
I want it to show the text between ':' and '.' at the end so :"\Documents\Text"
int start_index = Path.IndexOf(':')+1;
int end_index = Path.LastIndexOf('.');
int length = end_index-start_index;
string directory = Path.Substring(start_index,length);
Linq is always fascinating:
string s = string.Join("",
Path.SkipWhile(p => p != ':')
.Skip(1)
.TakeWhile(p => p != '.')
);
You can use string operations, but you can also use the System.IO.Path functions for a - in my personal opinion - more elegant solution:
string path = #"C:\Documents\Text.txt";
string pathRoot = Path.GetPathRoot(path); // pathRoot will be "C:\", for example
string result = Path.GetDirectoryName(path).Substring(pathRoot.Length - 1) +
Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(path);
Console.WriteLine(result);
you should return your match2 instead of the path since path will remain C:\Documents\Text.txt
public String GetDirectory(String Path)
{
Console.WriteLine("Directorul: ");
var start = Path.IndexOf(":") + 6;
var match2 = Path.Substring(start, Path.IndexOf(".") - start);
return match2;
}
patch = patch.Substring(patch.IndexOf(':') + 1, patch.IndexOf('.') - 2);
I have the following code:
var str = "ABC";
var n = 7;
var output = String.Format("{0,n}", str);
This should output the string
" ABC"
How should I change the line above?
Format strings are just strings too - you can define the format separately:
int n = 3;
string format = string.Format("{{0,{0}}}", n);
//or more readable: string format = "{0," + n + "}";
var output = string.Format(format, str);
Edit:
After your update it looks like what you want can also be achieved by PadLeft():
var str = "ABC";
string output = str.PadLeft(7);
Just write:
var lineLength = String.Format("{0," + n + "}", str);
var s="Hello my friend";
string leftSpace = s.PadLeft(20,' '); //pad left with special character
string rightSpace = s.PadRight(20,' '); //pad right with special character
I want to eliminate B if i scan Serial number bar code: 21524116476CA2006765B
Output: 21524116476CA2006765
string foo = "21524116476CA2006765B";
string bar = foo.Substring(0, Math.Max(foo.Length - 1, 0));
If you can't be certain that the input barcode will always end with a B, do something like this:
char[] barcodeEnd = { 'B' };
string input = "21524116476CA2006765B";
string output = input.TrimEnd(barcodeEnd);
PUt the first line somewhere it'll only get run once, so you aren't constantly creating new char[] arrays. You can add more characters to the array if you're looking for other specific characters.
string s = "21524116476CA2006765B";
string b = s.Substring(0, s.Length - 1);
string value= "21524116476CA2006765B";
string bar = value.Substring(0, 19);
string str = "Your String Whatever it is";
str = str.Substring(0, s.Length - 1);