string-parsing
How do I extract a substring from a string until the second space is encountered?
A straightforward approach would be the following: string[] tokens = str.Split(‘ ‘); string retVal = tokens[0] + ” ” + tokens[1];
Get number of characters read by sscanf?
You can use the format specifier %n and provide an additional int * argument to sscanf(): int pos; sscanf(expression, “%lf%n”, &value, &pos); Description for format specifier n from the C99 standard: No input is consumed. The corresponding argument shall be a pointer to signed integer into which is to be written the number of characters … Read more
How to separate full name string into firstname and lastname string?
This will work if you are sure you have a first name and a last name. string fullName = “Adrian Rules”; var names = fullName.Split(‘ ‘); string firstName = names[0]; string lastName = names[1]; Make sure you check for the length of names. names.Length == 0 //will not happen, even for empty string names.Length == … Read more
Long.getLong() failing, returning null to valid string
You are missing the fact that Long.getLong(String str) is not supposed to parse a String to a long, but rather to return a long value of a system property represented by that string. As others have suggested, what you actually need is Long.parseLong(String str).
How to Convert Persian Digits in variable to English Digits Using Culture?
Simply Use the code below : private string changePersianNumbersToEnglish(string input) { string[] persian = new string[10] { “۰”, “۱”, “۲”, “۳”, “۴”, “۵”, “۶”, “۷”, “۸”, “۹” }; for (int j=0; j<persian.Length; j++) input = input.Replace(persian[j], j.ToString()); return input; }
Get contents after last slash
string path = “C://hello//world”; int pos = path.LastIndexOf(“https://stackoverflow.com/”) + 1; Console.WriteLine(path.Substring(pos, path.Length – pos)); // prints “world” The LastIndexOf method performs the same as IndexOf.. but from the end of the string.
Parse time of format hh:mm:ss [closed]
As per Basil Bourque’s comment, this is the updated answer for this question, taking into account the new API of Java 8: String myDateString = “13:24:40”; LocalTime localTime = LocalTime.parse(myDateString, DateTimeFormatter.ofPattern(“HH:mm:ss”)); int hour = localTime.get(ChronoField.CLOCK_HOUR_OF_DAY); int minute = localTime.get(ChronoField.MINUTE_OF_HOUR); int second = localTime.get(ChronoField.SECOND_OF_MINUTE); //prints “hour: 13, minute: 24, second: 40”: System.out.println(String.format(“hour: %d, minute: %d, second: … Read more
How do I search for a pattern within a text file using Python combining regex & string/file operations and store instances of the pattern?
import re pattern = re.compile(“<(\d{4,5})>”) for i, line in enumerate(open(‘test.txt’)): for match in re.finditer(pattern, line): print ‘Found on line %s: %s’ % (i+1, match.group()) A couple of notes about the regex: You don’t need the ? at the end and the outer (…) if you don’t want to match the number with the angle brackets, … Read more
Convert String with Dot or Comma as decimal separator to number in JavaScript
Do a replace first: parseFloat(str.replace(‘,’,’.’).replace(‘ ‘,”))