Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 270 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I access a char in string in at specific number?

#1
I tried this code but it is giving me errors. So how can I access a character in a string in kotlin? In java, it can be done by the `charAt()` method.

private fun abc(x: String) {
var i: Int = 0
while (x[i].toString() != "+") {
var y: Char = x[i]
i++
}
}
Reply

#2
The beauty of Kotlin is that you can do it in few ways, eg.

1. You can simply access it by index:

while (x[i] != '+') {
i++
}

2. Converting to `CharArray`

val chars: CharArray = x.toCharArray()

while (chars[i] != '+') {
i++
}

3. You can also use idiomatic Kotlin (preferred):

- **forEach**

x.forEach { c ->
if (c == '+') return@forEach
}

- **forEachIndexed** if you care about index

x.forEachIndexed { index, c ->
if (c == '+') {
println("index=$index")
return@forEachIndexed
}
}

In both cases, your character is accessed with `c`
Reply

#3
The equivalent of Javas [String.charAt()](

[To see links please register here]

) in Kotlin is [String.get()](

[To see links please register here]

). Since this is implemented as an operator, you can use `[index]` instead of `get(index)`. For example


val firstChar: Char = "foo"[0]

or if you prefer

val someString: String = "bar"
val firstChar: Char = someString.get(0)
Reply

#4
Could you please try this method instead?

private fun abc(x: String) {
$p = 1;
do {
$p++
}while (x[p]!= "+")
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through