I need to parse a string from NHRM__Location__c to just Location. I feel really dumb because this is not my first rodeo. I do not want to add the code in an array. NHRM is a namespace that may change depending on who's using it so at some point that will be a key in the app.config file.
I was thinking of just splitting the string on __ and then loading the rest of the word into a character array and trimming that guy down but I feel like I may be waaaay over complicating things.
I have put this into a .Net Fiddle for you using this code:
using System;
public class Program
{
public static void Main()
{
string stringToSplit = "NHRM__Location__c"; // 2 underscores in each location
char[] sep = new char[] {'_', '_'};
string newString = stringToSplit.Split(sep)[2];
Console.WriteLine(newString);
}
}
I know this is similar to the 1st answer, but I just wanted to prove that [2] does work, well at least for me in .Net Fiddle.
I hope this helps!
Update
System.String
Return Value - An array whose elements contain the substrings from this instance that are delimited by one or more characters in separator
So if I were to change the code to [4] then it would return just c.. and [0] would return NHRM.. the array values do not include the separator
Use this:
string sample = "NHRM__Location__c";
Console.WriteLine(sample.Split('_')[2]);
Related
using System;
using System.IO;
namespace Test_Arrays_and_Files
{
class Program
{
static void Main(string[] args)
{
string tFile = #"C:\Programming\GLO\DC\dcw.txt";
string read = File.ReadAllText(tFile);
string[] test = read.Split(',');
int[] ints = Array.ConvertAll(test, int.Parse);
Console.WriteLine(ints[0]);
}
}
}
Input Data:
Text File Contents:(1 value per line,)
35,
35,
40,
40,
40,
getting System.FormatException: Input string was not in correct format
please help and sorry for bad post I'm new here
There are two issues in your code,
Your string is ending with , which is creating empty record after the split. This is the reason you are getting the error.
Your delimiter could be $",{Environment.NewLine}" not only ','.
So to convert given string to int array, first Trim() the input string by , and then split by $",{Environment.NewLine}".
Like,
using System.Linq;
...
var result = str.Trim(',') //Remove leading and trailing comma(s), You can use `TrimEnd()` as well
.Split($",{Environment.NewLine}", StringSplitOptions.RemoveEmptyEntries) //Split by given delimiter.
.Select(int.Parse)
.ToArray(); //Convert string[] to int[]
Try online
You're only getting it because the file has a comma at the end, which means Split ends up churning out an empty string in the very last position. int.Parse will choke on the empty string
Plenty of ways you could solve it, one is to tell Split not to return you empties, by changing the split line of your code to:
string[] test = read.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries);
You could instead trim the comma off the end, but using the above approach would mean your parsing would survive a blank line in the middle of the file too so overall it's more robust
Generally when parsing strings it's more robust to use TryParse than Parse. TryParse takes in the number variable to set the result to and returns you a Boolean telling if the parsing succeeded
int[] ints = Array.ConvertAll(test, GetIntOrMinusOne);
//put a method helper
private int GetIntOrMinusOne(string s){
if(int.TryParse(s, out var t)
return t;
return -1;
}
For this we need to get a bit more involved with the ConvertAll call. Instead of telling ConvertAll to call a "method that converts a string to an int" like int.Parse, we need to write our own mini method that tries to parse and if it fails return something like -1, then nominate that as the method to call to do the conversion, not int.Parse
It's important to note that this would introduce -1 into the resulting int[] array wherever there was bad data in the string array.. In your later processing you would then do some check to avoid them (such as skipping them)
You can shorten that code above by turning the method into a lambda:
int[] ints = Array.ConvertAll(test, s => int.TryParse(s, out var t) ? t : -1);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
a lambda; "just the important parts of a method"
But I'm not sure that you'll have come across lambdas yet, judging by the style of the rest of the code
I am currently trying to use Codecademy to learn how to use C# yet the last 'test' for the very second lesson asks us to convert a string to a list. I looked at a forum and it said you had to use loops which was not taught in the course yet, yet I wanted to use a loop anyway, how could I create the list with the for loop which possibly needs fixing? (And maybe help to check whether or not the other code is correct as it asks to convert bool to string and a random data type to another which I chose byte for.) Thanks.
bool pick = true;
byte number = 5;
string myTest = "Ping Pong";
Console.WriteLine(Convert.ToString(pick));
Console.WriteLine(Convert.ToInt32(number));
for ((char(myTest));)
{
Console.WriteLine(i);
}
You use string.ToCharArray to convert the string into an array of characters.
However you don't need to do that just to iterate over it, string implements IEnumerable<char>, so you can iterate over it directly.
Also for ((char(myTest));) makes absolutely no sense.
An string is already a character array and because System.String implements IEnumerable<char> you can just loop it:
/*
References you need:
using System;
using System.Collections.Generic;
using System.Linq;
*/
string myTest = "Ping Pong";
//looping a string
foreach (char character in myTest)
{
Console.WriteLine(character);
}
//Explicit converting a string to list of chars
var listChars = new List<char>();
listChars = myTest.ToList();
foreach (char character in listChars)
{
Console.WriteLine(character);
}
I have a string which is basically a url something like APIPAth/resources/customers/SSNNumber/authorizations/contracts.
The SSNNumber can be of any value. Its the actual SSN number which I want to remove from the string and the string should look like APIPAth/resources/customers/authorizations/contracts.
I can't find a proper solution in which without hardcoding the word and removing the string
I tried using Find and Replace but I think the function would require the particular word
Looking at the URL you provided, it appears you only want to get rid of digits. You could accomplish that with this line:
var output = Regex.Replace(input, #"[\d]", string.Empty);
There are many ways to skin this cat depending on what stays static.
One way would be to split the url by separators and join them back
var url = #"APIPAth/resources/customers/SSNNumber/authorizations/contracts";
var items = new List<string>(url.Split('/'));
items.RemoveAt(3);
url = string.Join("/", items);
Another way would be to use Regex
var url = #"APIPAth/resources/customers/SSNNumber/authorizations/contracts";
url = Regex.Replace(url, #"/customers/[^/]+/authorizations/", "/customers/authorizations/")
If you elaborate on what you expect in a generic solution, i.e. what part stays static, then I can help you out better
If SSN in number then it has to have form of this 000 00 0000 means 9 digits consequents.
Took a string parse it by / and you get an array of elements lets say parsed
for(int i=0; i<parsed.length; i++){
if(parsed[i].length === 9){
...keep this i...
}
}
remove this parsed[i] from whole parsed and concat with /
I have an issue with a string containing the plus sign (+).
I want to split that string (or if there is some other way to solve my problem)
string ColumnPlusLevel = "+-J10+-J10+-J10+-J10+-J10";
string strpluslevel = "";
strpluslevel = ColumnPlusLevel;
string[] strpluslevel_lines = Regex.Split(strpluslevel, "+");
foreach (string line in strpluslevel_lines)
{
MessageBox.Show(line);
strpluslevel_summa = strpluslevel_summa + line;
}
MessageBox.Show(strpluslevel_summa, "summa sumarum");
The MessageBox is for my testing purpose.
Now... The ColumnPlusLevel string can have very varied entry but it is always a repeated pattern starting with the plus sign.
i.e. "+MJ+MJ+MJ" or "+PPL14.1+PPL14.1+PPL14.1" as examples.
(It comes form Another software and I cant edit the output from that software)
How can I find out what that pattern is that is being repeated?
That in this exampels is the +-J10 or +MJ or +PPL14.1
In my case above I have tested it by using only a MessageBox to show the result but I want the repeated pattering stored in a string later on.
Maybe im doing it wrong by using Split, maybe there is another solution.
Maybe I use Split in the wrong way.
Hope you understand my problem and the result I want.
Thanks for any advice.
/Tomas
How can I find out what that pattern is that is being repeated?
Maybe i didn't understand the requirement fully, but isn't it easy as:
string[] tokens = ColumnPlusLevel.Split(new[]{'+'}, StringSplitOptions.RemoveEmptyEntries);
string first = tokens[0];
bool repeatingPattern = tokens.Skip(1).All(s => s == first);
If repeatingPattern is true you know that the pattern itself is first.
Can you maybe explain how the logic works
The line which contains tokens.Skip(1) is a LINQ query, so you need to add using System.Linq at the top of your code file. Since tokens is a string[] which implements IEnumerable<string> you can use any LINQ (extension-)method. Enumerable.Skip(1) will skip the first because i have already stored that in a variable and i want to know if all others are same. Therefore i use All which returns false as soon as one item doesn't match the condition(so one string is different to the first). If all are same you know that there is a repeating pattern which is already stored in the variable first.
You should use String.Split function :
string pattern = ColumnPlusLevel.Split("+")[0];
...but it is always a repeated pattern starting with the plus sign.
Why do you even need String.Split() here if the pattern always only repeats itself?
string input = #"+MJ+MJ+MJ";
int indexOfSecondPlus = input.IndexOf('+', 1);
string pattern = input.Remove(indexOfSecondPlus, input.Length - indexOfSecondPlus);
//pattern is now "+MJ"
No need of string split, no need to use LinQ
String has a method called Split which let's you split/divide the string based on a given character/character-set:
string givenString = "+-J10+-J10+-J10+-J10+-J10"'
string SplittedString = givenString.Split("+")[0] ///Here + is the character based on which the string would be splitted and 0 is the index number
string result = SplittedString.Replace("-","") //The mothod REPLACE replaces the given string with a targeted string,i added this so that you can get the numbers only from the string
I am working on writing a method to remove the namespace from a System.Type.FullName (not XML).
I started off googling and didn't get too far so switched to trying to write a Regex I could use with a Regex.Replace(). But I am far from a master of the Regex arts, so I present myself humbly before the regex gods.
Given the following inputs:
name.space.class
name.space.class<other.name.space.class1>
name.space.class<other.name.space.class1, shortSpace.class2>
I need to remove the namespaces so I get:
class
class<class1>
class<class1, class2>
Alternatively, if anyone knows of an existing library that has this functionality, all the better!
Note: I know System.Type has a Namespace property that I could use to remove the namespace (ie System.Type.FullName - System.Type.Namespace), but my method takes a type name as a string and needs to work with type names that the run-time does not know about (can't resolve).
How about this...
[.\w]+\.(\w+)
...and substiuting with $1. See it in action on regex101.
From looking at some C# examples it seems you would do
string output = Regex.Replace(input, #"[.\w]+\.(\w+)", "$1");
Try this:
public static string RemoveNamespaces(string typename)
{
return string.Join("",
Regex.Split(typename,
#"([^\w\.])").Select(p =>
p.Substring(p.LastIndexOf('.') + 1)));
}
I wouldn't even consider using regexes for this. Imperative code is pretty trivial here, although it requires a bit of string-fu:
public string RemoveNamespace(string typename)
{
if(typename.Contains("<")
{
var genericArguments =
typename.
// in reality, we need a substring before
// first occurence of "<" and last occurence of ">"
SubstringBetween("<", ">").
Split(',').
Select(string.Trim).
Select(RemoveNamespace);
return
RemoveNamespace(typename.SubstringBefore("<")) +
"<" +
string.Join(", ", genericArguments) +
">";
}
else
{
return typename.Trim().SubstringAfterLastOccurenceOf(".");
}
}
Sounds like a good situation to use positive lookahead:
(\w+[.+])+(?=\w+)
This pattern will match any number of words separated by periods or plusses, except the last one in a sequence (the short name of the type). Replacing the matches by the empty string will remove all namespace prefixes.
Why not split by dot(.) and take only the last string