How to get the X-th position of integer from a string? - string

I have a string that says "12345".
I want to take the 3rd element from the string to convert it to integer (which in this case, the 3rd element would be '3').
How would I do so ?
I heard that you can use Integer.parseInt(s) but this will return the whole integer of the String.
I just want one element from it, and at the x-th position.

I think the easiest way would be to simply use String.SubString. Something like:
string number = "12345";
string element = number.SubString(2, 1);
Where 2 is the position of the third character (remember it's 0 indexed) and 1 is the number of characters to return. You can then turn that into an integer if you want.

you can do some thing like that...
int number = Integer.parseInt(givenString.charAt(xPosition));
Hope it helps.

The [charAt() method] (http://www.w3schools.com/jsref/jsref_charat.asp) should get you what you need

Related

Python. Why the length of the list changes after turning it from int to string?

I have a bunch of users in a list called UserList.
And I do not want the output to have the square brackets, so I run this line:
UserList = [1,2,3,4...]
UserListNoBrackets = str(UserList).strip('[]')
But if I run:
len(UserList) #prints22 (which is correct).
However:
len(UserListNoBrackets) #prints 170 (whaaat?!)
Anyway, the output is actually correct (I'm pretty sure). Just wondering why that happens.
Here:
UserListNoBrackets = str(UserList).strip('[]')
UserListNoBrackets is a string. A string is a sequence of characters, and len(str) returns the numbers of characters in the string. A comma is a character, a white space is a character, and the string represention of an integer has has many characters as there are digits in the integer. So obviously, the length of your UserListNoBrackets string is much greater than the length of you UserList list.
You probably need str.join
Ex:
user_list = [1,2,3,4...]
print(",".join(map(str, user_list)))
Note:
Using map method to convert all int elements in list to string.

Remove part of string (regular expressions)

I am a beginner in programming. I have a string for example "test:1" and "test:2". And I want to remove ":1" and ":2" (including :). How can I do it using regular expression?
Hi andrew it's pretty easy. Think of a string as if it is an array of chars (letters) cause it actually IS. If the part of the string you want to delete is allways at the end of the string and allways the same length it goes like this:
var exampleString = 'test:1';
exampleString.length -= 2;
Thats it you just deleted the last two values(letters) of the string(charArray)
If you cant be shure it's allways at the end or the amount of chars to delete you'd to use the version of szymon
There are at least a few ways to do it with Groovy. If you want to stick to regular expression, you can apply expression ^([^:]+) (which means all characters from the beginning of the string until reaching :) to a StringGroovyMethods.find(regexp) method, e.g.
def str = "test:1".find(/^([^:]+)/)
assert str == 'test'
Alternatively you can use good old String.split(String delimiter) method:
def str = "test:1".split(':')[0]
assert str == 'test'

Removing first character from a string in octave

I wanted to know how to remove first character of a string in octave. I am manipulating the string in a loop and after every loop, I want to remove the first character of the remaining string.
Thanks in advance.
If it's just a one-line string then:
short_string = long_string(2:end)
But if you have a cell array of strings then either do it as above if you have a loop already, otherwise you can use this shorthand to do it in one line:
short_strings = cellfun(#(x)(x(2:end)), long_strings, 'uni', false)
Or else if you have a matrix of strings (i.e. all the same length), then you can vectorize it as:
short_strings = long_strings(:, 2:end)

How to reduce a String to an Integer summing its characters

I'd like to take a String e.g. "1234" and convert it to an Integer which represents the sum of all the characters.
I thought perhaps treating the String as a List of characters and doing a reduce / inject, would be the simplest mechanism. However, In all my attempts I have not managed to succeed in getting the syntax correct.
I attempted something along these lines without success.
int sum = myString.inject (0, { Integer accu, Character value ->
return accu + Character.getNumericValue(value)
})
Can you help me determine a simple syntax to resolve this problem (I can easily solve it in an java like verbose way with loops etc)
Try:
"1234".collect { it.toInteger() }.sum()
Solution by #dmahapatro
"1234".toList()*.toInteger().sum()

Adding blank spaces into a string in C#

I have this code
String coNum = customerOrderLine.coNum.PadLeft(10 - customerOrderLine.coNum.Length);
I know that customerOrderLine.coNum = "123456" So I should end up with coNum being having 4 empty spaces at the front of it but I end up with it being "123456". How do I fix this? I tried PadRight in case that was the mistake and it also failed to work. I have to have the 4 empty spaces at the beginning to pass it into the API I am working on or it will fail.
PadLeft takes a total length as a parameter, so I think you want
String coNum = customerOrderLine.coNum.PadLeft(10);
This is because you have incorrectly specified the totalWidth parameter of the Pad* method.
From docs:
The number of characters in the resulting string, equal to the number
of original characters plus any additional padding characters.[...] If totalWidth is equal to the length of this instance, the method
returns a new string that is identical to this instance.
PadLeft does not specify a default character to pad with; your second argument should be the character to use for the pad, i.e.:
String coNum = customerOrderLine.coNum.PadLeft(10, ' ');
Edit: Also the first argument should be total desired length, not number of pad characters to add, per #Matthew's answer.

Resources